How can I get the Currenttime in Milliseconds in EA - page 2

 
Yfi #:
Thank you guys so much 

Here is the code I use for getting milliseconds unix timestamp

//+------------------------------------------------------------------+
//| tc_TimeGMT()                                                     |
//+------------------------------------------------------------------+
const ulong tc_TimeGMT(const bool resync = false)
{
    // Environment

                const static uchar  __env_type      = (   ((::MQLInfoInteger(MQL_PROFILER) != NULL)
                                                                                                || (::MQLInfoInteger(MQL_TESTER) != NULL)
                                                                                                || (::MQLInfoInteger(MQL_FORWARD) != NULL)
                                                                                                || (::MQLInfoInteger(MQL_OPTIMIZATION) != NULL)
                                                                                                || (::MQLInfoInteger(MQL_FRAME_MODE) != NULL)) ? 0x01 : NULL)
                                                                                        + ((::MQLInfoInteger(MQL_DEBUG) != NULL) ? 0x02 : NULL);

    // Static init
    
        static ulong    offset  = NULL;

        
    // Return result
    
        if( (offset != NULL) 
         && (!resync) )
        { return((datetime)(offset + _GetTickCount64(__env_type))); }

    
    // Get time offset

        _GetTickCount64(__env_type);
        const datetime tm_now = ::TimeGMT();
        while( (tm_now == (::TimeGMT()))
            && (!_StopFlag) 
            && (__env_type == NULL) );
        const ulong tick_cnt = _GetTickCount64(__env_type);
        offset = (::TimeGMT() * 1000) - tick_cnt;

    // Return
    return((datetime)(offset + tick_cnt));
};
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| _GetTickCount64()                                                |
//+------------------------------------------------------------------+
const ulong _GetTickCount64(const uchar __env_type = NULL)
{
    // Static init

        static ulong offset_mc = NULL;


    // Return from precache

        if(offset_mc != NULL)
        { return((::GetMicrosecondCount() + offset_mc) / 1000); }


    // Local init

        ulong start_ms  = NULL;
        ulong start_mc  = NULL;
        ulong stop_ms   = 1;
        ulong stop_mc   = 1;
        ulong micros    = NULL;


    // Sync

        // Sync to system time by seconds
        const datetime tm = ::TimeGMT();
        while( (tm == (::TimeGMT()))
            && (!_StopFlag) 
            && (__env_type == NULL) );

        // Sync milliseconds by microseconds
        start_ms    = ::GetTickCount() + 250;
        micros      = ::GetMicrosecondCount();
        start_mc    = (micros % 100000000) + 250000;
        micros      = micros - (micros % 100000000);
        while( ((stop_mc - start_mc) != ((stop_ms - start_ms) * 1000))
            && (!_StopFlag) )
        {
            stop_mc = (::GetMicrosecondCount() % 100000000);
            stop_ms = ::GetTickCount();
        }

        // Calculate milliseconds from sync point
        stop_mc    += micros;
        stop_ms     = (stop_ms & 0x00000000FFFFFFFF) + (::GetTickCount64() & 0xFFFFFFFF00000000);
        offset_mc   = (stop_ms * 1000) - (stop_mc - (stop_mc % 1000));

    // Return
    return((::GetMicrosecondCount() + offset_mc) / 1000);
}
//+------------------------------------------------------------------+


 
Yashar Seyyedin #:
How useful can it be to know how many milliseconds passed since 1970.01.01?

For example, if you want to control for exact time of new candle open, instead of waiting for the first tick within the candle.

 
Yashar Seyyedin #:
How useful can it be to know how many milliseconds passed since 1970.01.01?

 

LOL that's what EVERY COMPUTER SYSTEM IN THE WORLD is based into ... from your Applewatch to your phone to GPS satelite systems .. 

ALL "IT" time begins in 1970/01/01 at 00:00:00 UTC+0

https://en.wikipedia.org/wiki/Unix_time

It has become a defacto standard to measure time (since there is no way a computer knows how to count time but it can count MICROSECONDS (which is actually a microprocessor default unit) and makes for a good way to synchronize devices that are not necessarily connected to the internet.

 
jdbaigorri #:

 

LOL that's what EVERY COMPUTER SYSTEM IN THE WORLD is based into ... from your Applewatch to your phone to GPS satelite systems .. 

ALL "IT" time begins in 1970/01/01 at 00:00:00 UTC+0

https://en.wikipedia.org/wiki/Unix_time

It has become a defacto standard to measure time (since there is no way a computer knows how to count time but it can count MICROSECONDS (which is actually a microprocessor default unit) and makes for a good way to synchronize devices that are not necessarily connected to the internet.

https://en.wikipedia.org/wiki/High_Precision_Event_Timer