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 | |
%a |
Abbreviated weekday name |
"Mon" => Monday |
|
%A |
Long weekday name |
"Monday" |
|
%b |
Abbreviated month name |
"Feb" => February |
|
%B |
Full month name |
"February" |
|
%c ! |
The preferred date and time representation for the current locale. |
|
|
%C !# |
The century number (year/100) as a 2-digit integer. |
|
|
%d |
Day of the month as decimal 01 to 31. When used to parse input, the leading zero is optional. |
|
|
%D !# |
Equivalent to %m/%d/%y |
|
|
%e # |
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. |
|
|
%G ! |
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. |
|
|
%g ! |
Like %G, but without century. |
|
|
%h !# |
Equivalent to %b |
|
|
%j |
Day of year as decimal from 001 to 366 for leap years, 001 - 365 for non-leap years. |
"060" => Feb-29 |
|
%m |
Month name as a decimal 01 to 12 |
"01" => January |
|
%u ! |
The day of the week as a decimal, range 1 to 7, Monday being 1. |
|
|
%U |
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). |
date d(2005, Jan, 1); // Saturday // with format %U ss << d; // "00" d += day(1); // Sunday ss << d; // "01" beginning of week 1 |
|
%V !# |
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. |
|
|
%w |
Weekday as decimal number 0 to 6 |
"0" => Sunday |
|
%W |
Week number 00 to 53 where Monday is first day of week 1 |
date d(2005, Jan, 2); // Sunday // with format %W ss << d; // "00" d += day(1); // Monday ss << d; // "01" beginning of week 1 |
|
%x |
Implementation defined date format from the locale. |
date d(2005,Oct,31); date_facet* f = new date_facet("%x"); locale loc = locale(locale("en_US"), f); cout.imbue(loc); cout << d; // "10/31/2005" loc = locale(locale("de_DE"), f); cout.imbue(loc); cout << d; // "31.10.2005" |
|
%y |
Two digit year |
"05" => 2005 |
|
%Y |
Four digit year |
"2005" |
|
%Y-%b-%d |
Default date format |
"2005-Apr-01" |
|
%Y%m%d |
ISO format |
"20050401" |
|
%Y-%m-%d |
ISO extended format |
"2005-04-01" |
Format Specifier | Description |
---|---|
Example | |
%- *! |
Placeholder for the sign of a duration. Only displays when the duration is negative. |
"-13:15:16" |
|
%+ *! |
Placeholder for the sign of a duration. Always displays for both positive and negative. |
"+13:15:16" |
|
%f |
Fractional seconds are always used, even when their value is zero |
"13:15:16.000000" |
|
%F * |
Fractional seconds are used only when their value is not zero. |
"13:15:16" "05:04:03.001234" |
|
%H |
The hour as a decimal number using a 24-hour clock (range 00 to 23). |
%I ! |
The hour as a decimal number using a 12-hour clock (range 01 to 12). |
%k ! |
The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. |
%l ! |
The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. |
%M |
The minute as a decimal number (range 00 to 59). |
%O |
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. |
%p ! |
Either `AM' or `PM' according to the given time value, or the corresponding strings for the current locale. |
%P !# |
Like %p but in lowercase: `am' or `pm' or a corresponding string for the current locale. |
%r !# |
The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to `%I:%M:%S %p' |
%R ! |
The time in 24-hour notation (%H:%M) |
%s * |
Seconds with fractional seconds. |
"59.000000" |
|
%S |
Seconds only |
"59" |
|
%T ! |
The time in 24-hour notation (%H:%M:%S) |
%q |
ISO time zone (output only). This flag is ignored when using the time_facet with a ptime. |
"-0700" // Mountain Standard Time |
|
%Q |
ISO extended time zone (output only). This flag is ignored when using the time_facet with a ptime. |
"-05:00" // Eastern Standard Time |
|
%z *! |
Abbreviated time zone (output only). This flag is ignored when using the time_facet with a ptime. |
"MST" // Mountain Standard Time |
|
%Z *! |
Full time zone name (output only). This flag is ignored when using the time_facet with a ptime. |
"EDT" // Eastern Daylight Time |
|
%ZP * |
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. |
"EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00" |
|
%x %X |
Implementation defined date/time format from the locale. |
date d(2005,Oct,31); ptime pt(d, hours(20)); time_facet* f = new time_facet("%x %X"); locale loc = locale(locale("en_US"), f); cout.imbue(loc); cout << pt; // "10/31/2005 08:00:00 PM" loc = locale(locale("de_DE"), f); cout.imbue(loc); cout << pt; // "31.10.2005 20:00:00" |
|
%Y%m%dT%H%M%S%F%q |
ISO format |
"20051015T131211-0700" // Oct 15, 2005 13:12:11 MST |
|
%Y-%m-%d %H:%M:%S%F%Q |
Extended ISO format |
"2005-10-15 13:12:11-07:00" |
|
%Y-%b-%d %H:%M:%S%F %z |
Default format used when outputting ptime and local_date_time. |
"2005-Oct-15 13:12:11 MST" |
|
%Y-%b-%d %H:%M:%S%F %ZP |
Default format used when inputting ptime and local_date_time. |
"2005-Oct-15 13:12:11 MST-07" |
|
%-%H:%M:%S%F ! |
Default time_duration format for output. Sign will only be displayed for negative durations. |
"-13:14:15.003400" |
|
%H:%M:%S%F |
Default time_duration format for input. |
"13:14:15.003400" |