Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2006-05-17 18:35:52

On Tue, 25 Apr 2006 07:48:13 -0400, Scott wrote
> > #include "boost/date_time/local_time_adjustor.hpp"
> > #include "boost/date_time/c_local_time_adjustor.hpp"
> >
> > ptime local_var
> > =date_time::c_local_adjustor<ptime>::utc_to_local(utc_var);
> >
> > What do you mean by the file is in binary format?
> Thanks for the help. What I mean is I would like to read/write the
> value of the ptime in the most efficient way possible. So if the
> ptime is stored as 2 ints, I would like to (de)serialize 8 bytes (4
> bytes each int). Portability isn't an issue.
> What I would rather *not* do is use something like
> to_iso_string(ptime) and from_iso_string(ptime). The string
> representation is rather long. But if that's the only way to do it,
> guess I'll have to make do.

Well, of course, it's not the only way to do it. But it is true that the
internal representation is generally difficult to get out of the type
directly. One reason is that sometimes the internal rep is uint64_t --
sometimes it's a uint64_t and a uint32_t. Which representation is used is
determined by compile time options. So it's generally unsafe to just save the
internal value. More generally, storage of a binary value for a time is
generally error prone and subject to 'interesting effects'. For example, a
leap second is declared and the number of seconds since the chosen epoch
changes. Finally, binary values make it hard on humans -- you need software
is need to figure out what the value is. Needless to say, I'm against this
sort of storage except in the most extreme circumstances.

However, if you still really want to do this, here's some options for you:

1) Use the public interface to get the time elements (hour, min, sec, etc) and
write a function to convert to a binary value of your choosing.
2) Derive from ptime and add a function to return the internal value. The
time_rep is a protected member of the base called time_. The only "hard part"
in this is you'll want to carry the constructors you are using in ptime from
  class mytime : public ptime {

     //constructors you want from ptime here...
     ptime::time_rep_type get_rep() { return time_; }

3) Put the above accessor directly into the ptime class and maintain a
'changed version'.

There's actually some other ways, but these are probably the best options.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at