boost日期库虽然强大,使用起来不太方便,在此记录下日期转换代码。
boost日期格式转换代码如下:
bool FromString(boost::posix_time::ptime& pt, std::string datetime, std::string format)
{
std::stringstream ss(datetime);
//std::locale responsible for releasing memory.
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_input_facet(format)));//out
if ( ss >> pt )
{
return true;
}
return false;
}
bool ToString(const boost::posix_time::ptime& pt, std::string& datetime, std::string format)
{
std::stringstream ss;
//std::locale responsible for releasing memory.
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet(format)));//out
if ( ss << pt )
{
datetime = ss.str();
return true;
}
return false;
}
std::time_t ToUtcTime(const boost::posix_time::ptime& pt)
{
boost::posix_time::time_duration offset(
boost::posix_time::second_clock::local_time() - boost::posix_time::second_clock::universal_time());
boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
boost::posix_time::ptime putc = pt - offset;
boost::posix_time::time_duration diff(putc - epoch);
return diff.total_seconds();
}
//FromUtcTime => boost::posix_time::from_time_t(std::time_t time);
//FromTM => ptime ptime_from_tm(tm timetm)
//ToTM => tm to_tm(time_duration)
日期格式文档如下:
Date Facet Format Flags
Format Specifier | Description |
---|---|
Example | |
| Abbreviated weekday name |
| |
| Long weekday name |
| |
| Abbreviated month name |
| |
| Full month name |
| |
| The preferred date and time representation for the current locale. |
| |
| The century number (year/100) as a 2-digit integer. |
| |
| Day of the month as decimal 01 to 31. When used to parse input, the leading zero is optional. |
| |
| Equivalent to %m/%d/%y |
| |
| Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. When used to parse input, the leading space is optional. |
| |
| This has the same format and value as %y, except that if the ISO week number belongs to the previous or next year, that year is used instead. |
| |
| Like %G, but without century. |
| |
| Equivalent to %b |
| |
| Day of year as decimal from 001 to 366 for leap years, 001 - 365 for non-leap years. |
| |
| Month name as a decimal 01 to 12 |
| |
| The day of the week as a decimal, range 1 to 7, Monday being 1. |
| |
| The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. In 2005, Jan 1st falls on a Saturday, so therefore it falls within week 00 of 2005 (week 00 spans 2004-Dec-26 to 2005-Jan-01. This also happens to be week 53 of 2004). |
| |
| The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. |
| |
| Weekday as decimal number 0 to 6 |
| |
| Week number 00 to 53 where Monday is first day of week 1 |
| |
| Implementation defined date format from the locale. |
| |
| Two digit year |
| |
| Four digit year |
| |
| Default date format |
| |
| ISO format |
| |
| ISO extended format |
|
Format Specifier | Description |
---|---|
Example | |
| Placeholder for the sign of a duration. Only displays when the duration is negative. |
| |
| Placeholder for the sign of a duration. Always displays for both positive and negative. |
| |
| Fractional seconds are always used, even when their value is zero |
| |
| Fractional seconds are used only when their value is not zero. |
| |
| The hour as a decimal number using a 24-hour clock (range 00 to 23). |
| The hour as a decimal number using a 12-hour clock (range 01 to 12). |
| The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. |
| The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. |
| The minute as a decimal number (range 00 to 59). |
| The number of hours in a time duration as a decimal number (range 0 to max. representable duration); single digits are preceded by a zero. |
| Either `AM' or `PM' according to the given time value, or the corresponding strings for the current locale. |
| Like %p but in lowercase: `am' or `pm' or a corresponding string for the current locale. |
| The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to `%I:%M:%S %p' |
| The time in 24-hour notation (%H:%M) |
| Seconds with fractional seconds. |
| |
| Seconds only |
| |
| The time in 24-hour notation (%H:%M:%S) |
| ISO time zone (output only). This flag is ignored when using the time_facet with a ptime. |
| |
| ISO extended time zone (output only). This flag is ignored when using the time_facet with a ptime. |
| |
| Abbreviated time zone (output only). This flag is ignored when using the time_facet with a ptime. |
| |
| Full time zone name (output only). This flag is ignored when using the time_facet with a ptime. |
| |
| Posix time zone string (available to both input and output). This flag is ignored when using the time_facet with a ptime. For complete details on posix time zone strings, see posix_time_zone class. |
| |
| Implementation defined date/time format from the locale. |
| |
| ISO format |
| |
| Extended ISO format |
| |
| Default format used when outputting ptime and local_date_time. |
| |
| Default format used when inputting ptime and local_date_time. |
| |
| Default time_duration format for output. Sign will only be displayed for negative durations. |
| |
| Default time_duration format for input. |
|