boost日期格式互转和time_t互转 - 小众知识

boost日期格式互转和time_t互转

2021-08-18 02:16:07 苏内容
  标签: boost/日期
阅读:3608

boost日期库虽然强大,使用起来不太方便,在此记录下日期转换代码。

boost日期格式转换代码如下:

  1. bool FromString(boost::posix_time::ptime& pt, std::string datetime, std::string format)
  2. {
  3. std::stringstream ss(datetime);
  4. //std::locale responsible for releasing memory.
  5. ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_input_facet(format)));//out
  6. if ( ss >> pt )
  7. {
  8. return true;
  9. }
  10. return false;
  11. }
  12. bool ToString(const boost::posix_time::ptime& pt, std::string& datetime, std::string format)
  13. {
  14. std::stringstream ss;
  15. //std::locale responsible for releasing memory.
  16. ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet(format)));//out
  17. if ( ss << pt )
  18. {
  19. datetime = ss.str();
  20. return true;
  21. }
  22. return false;
  23. }
  24. std::time_t ToUtcTime(const boost::posix_time::ptime& pt)
  25. {
  26. boost::posix_time::time_duration offset(
  27.        boost::posix_time::second_clock::local_time() - boost::posix_time::second_clock::universal_time());
  28. boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
  29. boost::posix_time::ptime putc = pt - offset;
  30. boost::posix_time::time_duration diff(putc - epoch);
  31. return diff.total_seconds();
  32. }
  33. //FromUtcTime => boost::posix_time::from_time_t(std::time_t time);
  34. //FromTM => ptime ptime_from_tm(tm timetm)
  35. //ToTM => tm to_tm(time_duration)

日期格式文档如下:

Date Facet Format Flags

Format SpecifierDescription
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).
  1. date d(2005, Jan, 1); // Saturday
  2. // with format %U
  3. ss << d; // "00"
  4. d += day(1); // Sunday
  5. 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
  1. date d(2005, Jan, 2); // Sunday
  2. // with format %W
  3. ss << d; // "00"
  4. d += day(1); // Monday
  5. ss << d; // "01" beginning of week 1
%x
Implementation defined date format from the locale.
  1. date d(2005,Oct,31);
  2. date_facet* f = new date_facet("%x");
  3. locale loc = locale(locale("en_US"), f);
  4. cout.imbue(loc);
  5. cout << d; // "10/31/2005"
  6. loc = locale(locale("de_DE"), f);
  7. cout.imbue(loc);
  8. 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"

Time Facet Format Flags

Format SpecifierDescription
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.
  1. "13:15:16"
  2. "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.
  1. date d(2005,Oct,31);
  2. ptime pt(d, hours(20));
  3. time_facet* f = new time_facet("%x %X");
  4. locale loc = locale(locale("en_US"), f);
  5. cout.imbue(loc);
  6. cout << pt; // "10/31/2005 08:00:00 PM"
  7. loc = locale(locale("de_DE"), f);
  8. cout.imbue(loc);
  9. 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"


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1