ToyElephant
ToyElephant

Reputation: 205

No setter method in boost local_date_time?

I am using boost date_time library to develop a filtering scheme in my application. Say I have 10000 records and each contain a timestamp (milliseconds in UTC). I want to convert this timestamp to a given timezone using boost and then return the time part in milliseconds. I am using the following code snippet :

local_date_time localDate(pt, tz_ptr);
return localDate.local_time().time_of_day().total_milliseconds();

In this code, pt is a ptime object created from timestamp (in milliseconds). The time zone pointer is constant for all 10000 records. Therefore, it is wasteful to create a local_date_time object at every iteration. If possible, I want to reuse this object. I was looking for a method like :

localDate.set(ptime pt);

But I could not find anything like that. Anyone know if something like this exist?

Upvotes: 1

Views: 232

Answers (1)

sehe
sehe

Reputation: 393084

"Therefore, it is wasteful to create a local_date_time object at every iteration"

Have you looked at the emitted assembly? I think you're prematurely optimizing.

The idea of ptime is to be "immutable value-type". Same goes for local_date_time. The complicating factor here is that the library designers have modeled timezone instances with shared ownership (for good reasons).

This is actually the only part you should worry about. I'd calculate the timezone offset up-front, just once and then just forego the local_date_time altogether.

CAVEAT If you do, make sure you account for dst/non-dst dates! The offset will vary. I'm not sure whether boost/the OS even take historical time zone changes into account.

It's probably best to use Boost DateTime's zone implementation and just do the raw conversions without the help of local_date_time

Another way to sidestep a lot of complexity is to consider using

  • boost::date_time::c_local_adjustor<ptime>
  • boost::date_time::local_adjustor<ptime>

See http://www.boost.org/doc/libs/1_62_0/doc/html/date_time/examples.html#date_time.examples.local_utc_conversion

Upvotes: 3

Related Questions