Download MetaTrader 5

seconds since 1970

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
trader88888
134
trader88888 2016.04.18 02:00 

Hello,

 I'm trying to get an output like this for the number of seconds since 1970.   1460944723

I've done my search on this form and every example doesn't give me the desired output, checked all the documentations, pulling out my hair now.  Here is what I have is below, and the response I get is "0".

 

Can anyone help me in getting my desired output? 

datetime d1="D'01.01.1970 00:00:00'";
emailBody = TimeSeconds(d1);
Fernando Carreiro
2349
Fernando Carreiro 2016.04.18 05:12  
  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"

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.04.18 07:00  
FMIC:
  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:

If it's only to output, you don't need an explicit typecasting :

PrintFormat( "Date/Time 2 as seconds: %i", dt2);   // Prints: "Date/Time 2 as seconds: 1460959102"
Keith Watford
Moderator
9624
Keith Watford 2016.04.18 07:06  
angevoyageur:

If it's only to output, you don't need an explicit typecasting :

 

I'm not sure, but doesn't it depend on whether you use #property strict or not?

with strict prints the date, without strict prints the seconds

Fernando Carreiro
2349
Fernando Carreiro 2016.04.18 07:11  
angevoyageur:

If it's only to output, you don't need an explicit typecasting :

Obviously, I know that, but the code was to serve as an example for the OP whose original code shows it being assigned to a variable (and not output). My code has output in order to show the values for debugging purposes!
Fernando Carreiro
2349
Fernando Carreiro 2016.04.18 07:12  
GumRai:

I'm not sure, but doesn't it depend on whether you use #property strict or not?

with strict prints the date, without strict prints the seconds

Well, that I did not know! I must test that! However, as stated in previous post, in this case the OP is using variable assignment!

EDIT: Just tested it! You are correct, under "strict", compilation, it does indeed print the date/time and not a value. Learned something new today.

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.04.18 07:29  
GumRai:

I'm not sure, but doesn't it depend on whether you use #property strict or not?

with strict prints the date, without strict prints the seconds

Yes but

1° Strict mode should always be used. Not strict is only there for compatibility issue. I suppose you will not remove the "strict" directive just to ouput datetime in seconds.

Using PrintFormat you can control exactly what you need as output (independently of strict directive).

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.04.18 07:31  
FMIC:
Obviously, I know that, but the code was to serve as an example for the OP whose original code shows it being assigned to a variable (and not output). My code has output in order to show the values for debugging purposes!
Don't take all so personal, my post is just an additional information. I don't care about what you already know or not.
Fernando Carreiro
2349
Fernando Carreiro 2016.04.18 07:43  
angevoyageur:

Yes but

1° Strict mode should always be used. Not strict is only there for compatibility issue. I suppose you will not remove the "strict" directive just to ouput datetime in seconds.

2° Using PrintFormat you can control exactly what you need as output (independently of strict directive).

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).

angevoyageur:
Don't take all so personal, my post is just an additional information. I don't care about what you already know or not.

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.

Fernando Carreiro
2349
Fernando Carreiro 2016.04.18 07:49  
angevoyageur:
PrintFormat( "Date/Time 2 as seconds: %i", dt2);   // Prints: "Date/Time 2 as seconds: 1460959102"

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.
trader88888
134
trader88888 2016.04.18 11:17  
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.

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

 

TimeSeconds(TimeCurrent());

 

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? 

123
To add comments, please log in or register