Cocos2DX sdk have any function to convert from string representation to seconds?

0 votes
Thanks for reply. I get respons in string format "2016-08-15T07:30:43.463Z". Cocos2DX sdk have any function to convert from string representation to seconds since epoch?
related to an answer for: Timer service. Is server time fixed?
asked Aug 15, 2016 in Cocos2D-X by Kirill Kotov (39 points)
edited Aug 15, 2016 by Kirill Kotov

1 Answer

0 votes
Hi Kirill,

As of now, you can get the time in string format from the SDK. But you can convert the same using methods suggested here: http://stackoverflow.com/questions/4137748/c-converting-a-time-string-to-seconds-from-the-epoch .

Please let me know if you have any other queries, I will be happy to help you.

Regards,

Rajeev
answered Aug 16, 2016 by rajeev.etc (1,660 points)
I saw that link. It would be great if response will be as string and long representation since epoch. Here is my implementation.

#if UINT_MAX >= 0xFFFFFFFF
    typedef long cs_time_type;
#else
    typedef unsigned long cs_time_type;
#endif

cs_time_type CloudService::getSecondsSiceEpoch ( const struct tm * ptm )
{
    int m = 0;
    int y = ptm->tm_year;

    if( ( m = ptm->tm_mon ) < 2 )
    {
        m += 12;
        --y;
    }

    return ( ( ( ( cs_time_type )( y - 69 ) * 365u + y / 4 - y / 100 * 3 / 4 + ( m + 2 ) * 153 / 5 - 446 + ptm->tm_mday ) * 24u + ptm->tm_hour ) * 60u + ptm->tm_min ) * 60u + ptm->tm_sec;
}


cs_time_type CloudService::convertTime( const std::string& _time )
{
    cs_time_type result = 0;

    std::tm rawTime;
    std::istringstream timeStream( _time );
    std::memset( &rawTime, 0, sizeof( std::tm ) );

    if( timeStream >> std::get_time( &rawTime, "%Y-%m-%dT%H:%M:%S" ) )
    {
        result = getSecondsSiceEpoch( &rawTime );
    }
    else
    {
        log( "Can't parse time from server!" );
    }

    return result;
}

http://stackoverflow.com/questions/530519/stdmktime-and-timezone-info
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...