Getting the integer view of boost :: gregorian :: date

From the additional documentation on raising the class :: gregorian :: date here :

"Internal boost :: gregorian :: date is stored as a 32-bit integer type"

Now it will be a good, compact way, for example, to save this date in a file. But the document does not indicate any way to extract it from the object.

The question arises: is there a way to get this integer representation in order to later build another, equal object of the same class?

+4
source share
2 answers

The member function day_number()returns this.

boost::gregorian::date d(2014, 10, 18);
uint32_t number = d.day_number();

The reverse can be achieved:

gregorian_calendar::ymd_type ymd = gregorian_calendar::from_day_number(dn);
d = { ymd.year, ymd.month, ymd.day };

, Boost Serialization, . . http://www.boost.org/doc/libs/1_56_0/doc/html/date_time/serialization.html

: Live On Coliru

#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian/gregorian_io.hpp>
#include <iostream>

using namespace boost::gregorian;

int main()
{
    date d(2014, 10, 17);
    static_assert(sizeof(d) == sizeof(int32_t), "truth");

    std::cout << d << "\n";

    uint32_t dn = d.day_number();
    dn += 1;

    gregorian_calendar::ymd_type ymd = gregorian_calendar::from_day_number(dn);
    d = { ymd.year, ymd.month, ymd.day };
    std::cout << d << "\n";
}
+4

,

tm to_tm ()

date date_from_tm (tm datetm)

tm / time_t

struct tm * localtime (const time_t * timep);  time_t mktime (struct tm * tm)

time_t 64 , 32- 2038 .

/

int32_t  ye = d.year(); 
uint32_t mo = d.month(); 
uint32_t da = d.day();

// encode
int32_t l = ((ye << 9) & 0xfffffe00) | ((mo << 5) & 0x0000001d0) | (da & 0x0000001f);

// decode 
ye = (l  >> 9); 
mo = ((l >> 5) & 0x0000000f); 
da = (l        & 0x0000001f); 
0

All Articles