Double to DateTime

Back to topics list To post a new topic, please log in or register
avatar
36
winterz 2015.02.03 17:03
 
...

Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
D1_Bar     = GlobalVariableGet("D1_Bar");
D1_Bias    = "Bearish";

Comment("D1 Bias is: "+D1_Bias+" since: "+DoubleToStr(D1_Bar,TIME_DATE|TIME_MINUTES),
        "\nH4 Bias is: "+H4_Bias,
        "\nH1 Bias is: ",H1_Bias);

...

How is it I can display under the Comment what is a Double, into a human readable DateTime?
avatar
736
honest_knave 2015.02.03 17:17 #
 

TimeToStr() is the easy option.

GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
Print(TimeToStr(GlobalVariableGet("D1_Bar")));

 

Alternatively, you can get imaginative combining the components into a string of your format choice.

avatar
36
winterz 2015.02.03 18:21 #
 

But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.

Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way? 

avatar
2018
gooly 2015.02.03 18:45 #
 

datetime is a ulong variable double variable aren't that exact so if you store a date as double and reconvert back to datetime you risk to get another date+time than the one you've originally saved,

but may be the date you save could be 'small' enough to get back the same date+time.

avatar
36
winterz 2015.02.03 18:57 #
 

Thats interesting thanks gooly.

I think I may test this under the circumstances in which I use double and datetime. I really want to store the datetime so that if MT4 crashes or restart happens, it will continue from where it left off (as it uses datetime to the hour precision - not minutes or seconds etc.).

How could I go about properly displaying this in the Comment so that I can read it then without having:  "possible loss of data due to type conversion"

avatar
736
honest_knave 2015.02.03 19:25 #
 
winterz:

How could I go about properly displaying this in the Comment so that I can read it then without having:  "possible loss of data due to type conversion"

 Typecast it:

GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
Print(TimeToStr((datetime)GlobalVariableGet("D1_Bar")));

You can find out more about typecasting, and the limitations that Gooly has raised, here

avatar
13012
WHRoeder 2015.02.03 20:47 #
 
gooly: date you save could be 'small' enough to get back the same date+time.
A double has 15-17 digits 1417305600 (Sun, 30 Nov 2014 00:00:00 GMT) has 10. No problem going from datatime -> double -> datetime. Going to an int fails on 2147483647 (Tue, 19 Jan 2038 03:14:07 GMT)
avatar
736
honest_knave 2015.02.03 20:54 #
 
winterz:

But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.

Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way? 

Just a minor point... the return value of GlobalVariableSet is indeed a datetime because it is returning the modification time of the variable. It is not setting a datetime.

I hope that makes sense - it can be confusing. 

avatar
36
winterz 2015.02.03 22:43 #
 
Thank you kindly for everyones input. It's been very helpful!
avatar
36
winterz 2015.03.04 20:37 #
 
winterz:

How is it I can display under the Comment what is a Double, into a human readable DateTime?
Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
D1_Bar     = GlobalVariableGet("D1_Bar");

...

Resurrecting this thread; slightly different question: how would I go about converting the D1_Bar (Global variable Get()) into a datetime? I am trying to use this in the third parameter of:

int  iBarShift(
   string           symbol,          // symbol
   int              timeframe,       // timeframe
   datetime         time,            // time
   bool             exact=false      // mode
   );

 

avatar
36
winterz 2015.03.04 21:58 #
 
(datetime)D1_Bar;

...
Fixed it :)
Back to topics list  

To add comments, please log in or register