seconds since 1970 - page 2

 
trader88888: Would this tell me the time in seconds to 1970 from the current time?  I think it should - my output I get though is "1" - would this be because I need to store it in some "long" variable first because most of the number is being chopped off?
TimeSeconds(TimeCurrent());
  1. FMIC already answered that (item #2)
  2. TimeCurrent() is the number of seconds since 1970.
 
trader88888:

Thank you very much, I think this makes sense (i'm very green) :-)

Would this tell me the time in seconds to 1970 from the current time?  I think it should - my output I get though is "1" - would this be because I need to store it in some "long" variable first because most of the number is being chopped off? 

I have already stated that you cannot use the function "TimeSeconds()" and that you must instead use typecasting to long. Please read the entire thread again and especially my original post. Here it is again:

  1. The date/time D'01.01.1970 00:00:00' is 0 seconds from 1970, so the correct answer is "0". What did you expect?
  2. However, the function "TimeSecconds()" returns the amount of seconds elapsed from the beginning of the minute, not since 1970.
  3. The easiest way to convert a datetime variable into seconds since 1970 is simply to typecast it:
    datetime
       dt1 = D'01.01.1970 00:00:00',
       dt2 = D'18.04.2016 05:58:22';
       
    long 
       dts1 = (long) dt1, 
       dts2 = (long) dt2;   // "long" is safer, as "int" would only be valid until 2037
       
    Print( "Date/Time 1 as seconds: ", dts1 );   // Prints: "Date/Time 1 as seconds: 0"
    Print( "Date/Time 2 as seconds: ", dts2 );   // Prints: "Date/Time 2 as seconds: 1460959102"

As for your most recent post, you should use typecasting as a long:

long SecondsSince1970 = (long) TimeCurrent();

You can also typecast in the form of a function if that makes it easier for you to visualise the code flow:

long SecondsSince1970 = long( TimeCurrent() );
 
FMIC:

I don't usually use the "PrintFormat()" function, but you will have to use "long" formatting instead of "int" if you want to be safe for dates in the future past 2037.
Are you serious ? Are you really thinking to still use mql4 code in 2037 ? But ok you are right.
 
FMIC:

In that case, if strict mode should always be used, then explicit typecasting IS NEEDED for outputting with the standard "Print()" (contrary to your original statement).

I was not taking it personal! I was just responding to your statement, which it seems now that both you and I were wrong about, as "GumRai" has pointed out.

Except I never wrote what you seem to think I wrote

My point was to say an explicit typecasting (using a new variable or not) is NOT needed just to output a date as a number of seconds, and this completely independently of strict mode.

 
angevoyageur:
Are you serious ? Are you really thinking to still use mql4 code in 2037 ? But ok you are right.

Yes, I am serious - In order to future-proof your code! You never know how your code could be re-purposed in the future, by you or any person you share it with (such as here on this forum).

Not doing it is the same thinking that caused so many Y2K problems; because people believed that their code would never be used for any long period of time beyond 1999. The result was the mass panic in a haste to patch everything, and even so, there was still many hiccups.

 
angevoyageur:

Except I never wrote what you seem to think I wrote

My point was to say an explicit typecasting (using a new variable or not) is NOT needed just to output a date as a number of seconds, and this completely independently of strict mode.

No, you don't need to "assign" it to a variable, but you do need to typecast it, as the example below (for "strict" case):

Print( "This is the current date/time in seconds: ", (long) TimeCurrent() );
 
FMIC:

No, you don't need to "assign" it to a variable, but you do need to typecast it, as the example below (for "strict" case):

You don't need an explicit typecasting with this code to prove it :

PrintFormat( "Date/Time 2 as seconds: %lli", dt2);   // Prints: "Date/Time 2 as seconds: 1460959102"

Can I know why you are insisting on this point ?

 
angevoyageur:

You don't need an explicit typecasting with this code to prove it :

Can I know why you are insisting on this point ?

I am insisting on this point because you are mixing apples and oranges.

As I stated, typecasting IS NEEDED when using "Print()" not when using "PrintFormat()". They are different functions that work differently!

 
FMIC:

I am insisting on this point because you are mixing apples and oranges.

As I stated, typecasting IS NEEDED when using "Print()" not when using "PrintFormat()". They are different functions that work differently!

Please calm down. I never said typecasting is not needed when using print. I just added something more to your first post, and from there you make all a fuss for nothing. 

So, is it ok now ?

 
angevoyageur:

Please calm down. I never said typecasting is not needed when using print. I just added something more to your first post, and from there you make all a fuss for nothing. 

I am calm, but why is it that when I contradict one your statements you justify it as saying that you knew about it but were just offering an alternative.

When you contradict me for a wrong statement, I concede that I was wrong as I have done twice today.

Reason: