Specific time zone In boost :: posix_time :: ptime

I have the following time:

2010-01-25 03:13:34.384 - GMT Time Zone
2010-01-25 11:13:34.384 - My Local

I want to convert to timestamp to ms. However, since I only get the local time string from the caller, "2010-01-25 11:13: 34.384"

If I do this:

// ts is "2010-01-25 11:13:34.384" (My Local)
boost::posix_time::ptime t(boost::posix_time::time_from_string(ts));
boost::posix_time::ptime end(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration dur = t - end;
// epoch is 1264418014384
// 2010-01-25 11:13:34.384 (GMT)  -- But I want 2010-01-25 03:13:34.384
// 2010-01-25 19:13:34.384 (My Local) -- But I want 2010-01-25 11:13:34.384
long long epoch = dur.total_milliseconds();

Is there a way to tell boost :: posix_time that the ts string it receives belongs to My Local timezone?

+3
source share
2 answers

I have this in my local tree (namespace prefix omitted):

    /// wall-clock translation to UTC
    const ptime from_wall_clock( const ptime& value,
                                 const time_zone_ptr& from )
    {
        assert( from.get());

        // interpret as local time
        const local_date_time from_local( value.date(),
            value.time_of_day(), from,
            local_date_time::NOT_DATE_TIME_ON_ERROR );

        // get UTC
        return from_local.utc_time();
    }
+2
source

Source: https://habr.com/ru/post/1729849/


All Articles