德语缩写中月份的表达能用缩写形式吗? 如12月可以直接写成Dez.吗?

字符串表示的日期
多個字符串的分析和輸出。
/* 以下例子示範了 date 和 std::string 之間的轉換。
* 正常輸出:
* 2001-Oct-09
* Tuesday October 9, 2001
* 有一個異常:
* Exception: Month number is out of range 1..12
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
#include &string&
using namespace boost::
// 以下日期以 ISO 8601 擴展格式表示 (CCYY-MM-DD)
std::string s(""); //2001-October-09
date d(from_simple_string(s));
std::cout && to_simple_string(d) && std::
// 讀入 ISO 標準格式(CCYYMMDD) 並輸出 ISO 擴展格式
std::string ud(""); //2001-Oct-09
date d1(from_undelimited_string(ud));
std::cout && to_iso_extended_string(d1) && std::
// 輸出 date 的各個部分 - Tuesday October 9, 2001
date::ymd_type ymd = d1.year_month_day();
greg_weekday wd = d1.day_of_week();
std::cout && wd.as_long_string() && " "
&& ymd.month.as_long_string() && " "
&& ymd.day && ", " && ymd.year
// 我們傳入月份 25 以創造一個異常
std::string bad_date(""); //2001-??-09
std::cout && "An expected exception is next: " && std::
date wont_construct(from_undelimited_string(bad_date));
//use wont_construct so compiler doesn't complain, but you wont get here!
std::cout && "oh oh, you shouldn't reach this line: "
&& to_iso_string(wont_construct) && std::
catch(std::exception& e) {
std::cout && "
Exception: " &&
e.what() && std::
已生活的日子
使用時間長度和日期來計算你已經活過的天數。
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
using namespace boost::gregorian;
std::string s;
std::cout && "Enter birth day YYYY-MM-DD (eg: ): ";
std::cin && s;
date birthday(from_simple_string(s));
date today = day_clock::local_day();
days days_alive = today - birthday;
days one_day(1);
if (days_alive == one_day) {
std::cout && "Born yesterday, very funny" && std::endl;
else if (days_alive & days(0)) {
std::cout && "Not born yet, hmm: " && days_alive.days()
&& " days" &&std::endl;
std::cout && "Days alive: " && days_alive.days() && std::endl;
catch(...) {
std::cout && "Bad date entered: " && s && std::endl;
距離新年的日子
計算到新年的天數。
/* 以下例子使用一個 date_generator, 以及簡單的數學操作,
* 來計算距離今年新年的天數,以及到下一個新年的天數。
* 正常結果:
* 兩個天數相加為 365 (閏年則為 366)。
#include &iostream&
#include "boost/date_time/gregorian/gregorian.hpp"
using namespace boost::
date today = day_clock::local_day();
partial_date new_years_day(1,Jan);
//Subtract two dates to get a duration
days days_since_year_start = today - new_years_day.get_date(today.year());
std::cout && "Days since Jan 1: " && days_since_year_start.days()
days days_until_year_start = new_years_day.get_date(today.year()+1) -
std::cout && "Days until next Jan 1: " && days_until_year_start.days()
每個月的最後一天
以下例子讓用戶輸入一個月份和年份,找出該年所剩每個月的最後一天。
/* 該例找出給定月份的最後一天,然後顯示給定年份所剩各月的最後一天。
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
using namespace boost::
greg_year year(1400);
greg_month month(1);
// 用戶給定一個月份和年份
std::cout && "
Enter Year(ex: 2002): ";
std::cin &&
year = greg_year(y);
std::cout && "
Enter Month(1..12): ";
std::cin &&
month = greg_month(m);
catch(bad_year by) {
std::cout && "Invalid Year Entered: " && by.what() && '\n'
&& "Using minimum values for month and year." && std::
catch(bad_month bm) {
std::cout && "Invalid Month Entered" && bm.what() && '\n'
&& "Using minimum value for month. " && std::
date start_of_next_year(year+1, Jan, 1);
date d(year, month, 1);
// 將一個月增加到 d 直到下一年。
while (d & start_of_next_year){
std::cout && to_simple_string(d.end_of_month()) && std::
d += months(1);
本地化示例
boost::date_time 庫提供了創建定制 locale facets 的能力。日期的順序、語言、分隔符和縮寫均可定制。
/* 下面示範了創建一個以德語輸出日期的 facet (請原諒我的德語中的錯誤 --
* 我的母語不是德語)。
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
#include &algorithm&
/* 定義一系列字符數據,分別為與德語日期輸出相關的縮寫和全名字符串
* (從該 locale 中取出的將是 US 名)。 */
const char* const de_short_month_names[] =
"Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
"Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM"
const char* const de_long_month_names[] =
"Januar", "Februar", "Marz", "April", "Mai",
"Juni", "Juli", "August", "September", "Oktober",
"November", "Dezember", "NichtDerMonat"
const char* const de_long_weekday_names[] =
"Sonntag", "Montag", "Dienstag", "Mittwoch",
"Donnerstag", "Freitag", "Samstag"
const char* const de_short_weekday_names[] =
"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"
int main()
using namespace boost::
// 創建一些輸出用的格里曆對像
date d1(2002, Oct, 1);
greg_month m = d1.month();
greg_weekday wd = d1.day_of_week();
// 為德語日期創建一個 facet 和一個 locale
date_facet* german_facet = new date_facet();
std::cout.imbue(std::locale(std::locale::classic(), german_facet));
// 創建德語名字集合
date_facet::input_collection_type short_months, long_months,
short_weekdays, long_
std::copy(&de_short_month_names[0], &de_short_month_names[11],
std::back_inserter(short_months));
std::copy(&de_long_month_names[0], &de_long_month_names[11],
std::back_inserter(long_months));
std::copy(&de_short_weekday_names[0], &de_short_weekday_names[6],
std::back_inserter(short_weekdays));
std::copy(&de_long_weekday_names[0], &de_long_weekday_names[6],
std::back_inserter(long_weekdays));
// 替換我們的缺省名字
// 註:date_generators 和 special_values 未被替換,因為本例中未使用
german_facet-&short_month_names(short_months);
german_facet-&long_month_names(long_months);
german_facet-&short_weekday_names(short_weekdays);
german_facet-&long_weekday_names(long_weekdays);
// 以德語的月份縮寫輸出日期
german_facet-&format("%d.%m.%Y");
std::cout && d1 && std:: //01.10.2002
german_facet-&month_format("%B");
std::cout && m && std:: //Oktober
german_facet-&weekday_format("%A");
std::cout && wd && std:: //Dienstag
// 以 US 名輸出相同的格里曆對像
date_facet* us_facet = new date_facet();
std::cout.imbue(std::locale(std::locale::classic(), us_facet));
us_facet-&format("%m/%d/%Y");
std::cout && d1 && std:: //
10/01/2002
// 英文名,iso 順序(年-月-日),'-' 分隔符
us_facet-&format("%Y-%b-%d");
std::cout && d1 && std:: //
2002-Oct-01
日期段計算
用時間段計算函數計算某個日期是否位於一個&無規則&的時間段集合中。
本例示範了使用時間段來計算日期信息。
本例計算一個給定的日期是否在一個給定的週末或假日集合中。即,每個週末或假日
作為一個時間間隔放入一個集合中。如果給定的日期被某一個間隔所包含則認為它是
在這個排除集合之中,是一個休息日。
Number Excluded Periods: 5
In Exclusion Period:
#include "boost/date_time/gregorian/gregorian.hpp"
#include &set&
#include &algorithm&
#include &iostream&
typedef std::set&boost::gregorian::date_period& date_period_
//生成排除集合
date_period_set
generateExclusion()
using namespace boost::
date_period periods_array[] =
{ date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd
date_period(date(2002,Feb,9), date(2002,Feb,11)),
date_period(date(2002,Feb,16), date(2002,Feb,18)),
date_period(date(2002,Feb,23), date(2002,Feb,25)),
date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12
const int num_periods = sizeof(periods_array)/sizeof(date_period);
date_period_
//將時間段插入到集合中
std::insert_iterator&date_period_set& itr(ps, ps.begin());
std::copy(periods_array, periods_array+num_periods, itr );
int main()
using namespace boost::
date_period_set ps = generateExclusion();
std::cout && "Number Excluded Periods: "
&& ps.size() && std::
date d(2002,Feb,16);
date_period_set::const_iterator i = ps.begin();
//打印時間段,並檢查包含性
for (;i != ps.end(); i++) {
std::cout && to_iso_string(*i) && std::
//如果 date 位于于排除時間段中則打印它
if (i-&contains(d)) {
std::cout && "In Exclusion Period: "
&& to_iso_string(d) && " --& " && to_iso_string(*i)
這是一個用函數對像來定義假日時間表的例子。
/* 使用一組日期生成器來生成一組日期
* 輸出如下:
* Enter Year: 2002
* 2002-Jan-01 [Tue]
* 2002-Jan-21 [Mon]
* 2002-Feb-12 [Tue]
* 2002-Jul-04 [Thu]
* 2002-Sep-02 [Mon]
* 2002-Nov-28 [Thu]
* 2002-Dec-25 [Wed]
* Number Holidays: 7
#include "boost/date_time/gregorian/gregorian.hpp"
#include &algorithm&
#include &functional&
#include &vector&
#include &iostream&
#include &set&
print_date(boost::gregorian::date d)
using namespace boost::
#if defined(BOOST_DATE_TIME_NO_LOCALE)
std::cout && to_simple_string(d) && " [" && d.day_of_week() && "]\n";
std::cout && d && " [" && d.day_of_week() && "]\n";
std::cout && "Enter Year: ";
std::cin &&
using namespace boost::
//定義一組固定月份和日期的假日
std::vector&year_based_generator*&
holidays.push_back(new partial_date(1,Jan)); //新年
holidays.push_back(new partial_date(4,Jul)); //美國獨立日
holidays.push_back(new partial_date(25, Dec));//聖誕節
//為 nth_day_of_the_week_in_month 函數對像定義一個縮寫
typedef nth_day_of_the_week_in_month nth_
//美國勞動節
holidays.push_back(new nth_dow(nth_dow::first,
holidays.push_back(new nth_dow(nth_dow::third,
//Pres day
holidays.push_back(new nth_dow(nth_dow::second, Tuesday,
holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov));
typedef std::set&date& date_
date_set all_
for(std::vector&year_based_generator*&::iterator it = holidays.begin();
it != holidays.end(); ++it)
all_holidays.insert((*it)-&get_date(year));
//將假日打印到屏幕
std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
std::cout && "Number Holidays: " && all_holidays.size() && std::
一個簡單的工具,打印一個月的所有日期。示範了日期迭代器(date_time::date_itr).
/* 本例打印一個月的所有日期。它示範了迭代器和 gregorian_calendar 函數的使用
* Enter Year: 2002
* Enter Month(1..12): 2
* 2002-Feb-01 [Fri]
* 2002-Feb-02 [Sat]
* 2002-Feb-03 [Sun]
* 2002-Feb-04 [Mon]
* 2002-Feb-05 [Tue]
* 2002-Feb-06 [Wed]
* 2002-Feb-07 [Thu]
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
std::cout && "Enter Year: ";
std::cin &&
std::cout && "Enter Month(1..12): ";
std::cin &&
using namespace boost::
//使用 calendar 取得該月的最後一天
int eom_day = gregorian_calendar::end_of_month_day(year,month);
date endOfMonth(year,month,eom_day);
//構造一個迭代器,從該月的第一天開始
day_iterator ditr(date(year,month,1));
//loop thru the days and print each one
for (; ditr &= endOfM ++ditr) {
#if defined(BOOST_DATE_TIME_NO_LOCALE)
std::cout && to_simple_string(*ditr) && " ["
std::cout && *ditr && " ["
&& ditr-&day_of_week() && "]"
catch(std::exception& e) {
std::cout && "Error bad date, check your entry: \n"
Details: " && e.what() && std::
不使用迭代器,將一個月加到某天上。
/* 本程序使用格里曆來步進剛好一個月,無論這個月有多少天。
* 本方法可作為迭代器之外的另一個選擇
#include "boost/date_time/gregorian/gregorian.hpp"
#include &iostream&
using namespace boost::
date d = day_clock::local_day();
add_month mf(1);
date d2 = d + mf.get_offset(d);
std::cout && "Today is: " && to_simple_string(d) && ".\n"
&& "One month from today will be: " && to_simple_string(d2)
時間的算術
時間和時長的多種運算。
/* 有關時間的構造和運算的一些簡單例子
* 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02. = -5:04:02.
#include "boost/date_time/posix_time/posix_time.hpp"
#include &iostream&
using namespace boost::posix_
using namespace boost::
date d(2002,Feb,1); //任意一個日期
//通過加上一個時長來構造一個時間
ptime t1(d, hours(5)+minutes(4)+seconds(2)+millisec(1));
//通過減去一個時間來構造新的時間
ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1);
//構造一個表示兩個時間的差距的時長
time_duration td = t2 - t1;
std::cout && to_simple_string(t2) && " - "
&& to_simple_string(t1) && " = "
&& to_simple_string(td) && std::
示範時間迭代器,時鐘取回,以及簡單的運算。
/* 打印一天中剩餘的小時
* 使用時鐘取出本地時間
* 使用一個迭代器來遍歷剩餘的小時
* 從時間中取出日期部分
* 期望的輸出如下:
* 2002-Mar-08 16:30:59
* 2002-Mar-08 17:30:59
* 2002-Mar-08 18:30:59
* 2002-Mar-08 19:30:59
* 2002-Mar-08 20:30:59
* 2002-Mar-08 21:30:59
* 2002-Mar-08 22:30:59
* 2002-Mar-08 23:30:59
* Time left till midnight: 07:29:01
#include "boost/date_time/posix_time/posix_time.hpp"
#include &iostream&
using namespace boost::posix_
using namespace boost::
//從時鐘取出當前時間 -- 秒解析度
ptime now = second_clock::local_time();
//取出時間的日期部分
date today = now.date();
date tommorrow = today + days(1);
ptime tommorrow_start(tommorrow); //午夜
//迭代器,每次加一個小時
time_iterator titr(now,hours(1));
for (; titr & tommorrow_ ++titr) {
std::cout && to_simple_string(*titr) && std::
time_duration remaining = tommorrow_start -
std::cout && "Time left till midnight: "
&& to_simple_string(remaining) && std::
本地時間到 UTC 的轉換
示範 utc 到本地時間以及本地時間到 utc 的轉換,包括 dst.
/* 示範在本地時間和 utc 之間的轉換
* UTC &--& New York while DST is NOT active (5 hours)
* 2001-Dec-31 19:00:00 in New York is 2002-Jan-01 00:00:00 UTC time
* 2002-Jan-01 00:00:00 UTC is 2001-Dec-31 19:00:00 New York time
* UTC &--& New York while DST is active (4 hours)
* 2002-May-31 20:00:00 in New York is 2002-Jun-01 00:00:00 UTC time
* 2002-Jun-01 00:00:00 UTC is 2002-May-31 20:00:00 New York time
* UTC &--& Arizona (7 hours)
* 2002-May-31 17:00:00 in Arizona is 2002-Jun-01 00:00:00 UTC time
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time_adjustor.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
#include &iostream&
using namespace boost::posix_
using namespace boost::
//本地時間調整器取決於機器的 TZ 設置 -- 高度危險!
typedef boost::date_time::c_local_adjustor&ptime& local_
ptime t10(date(2002,Jan,1), hours(7));
ptime t11 = local_adj::utc_to_local(t10);
std::cout && "UTC &--& Zone base on TZ setting" && std::
std::cout && to_simple_string(t11) && " in your TZ is "
&& to_simple_string(t10) && " UTC time "
time_duration td = t11 - t10;
std::cout && "A difference of: "
&& to_simple_string(td) && std::
//東部時區為 utc-5
typedef boost::date_time::local_adjustor&ptime, -5, us_dst& us_
ptime t1(date(2001,Dec,31), hours(19)); //紐約時間的午夜前5個小時
std::cout && "\nUTC &--& New York while DST is NOT active (5 hours)"
ptime t2 =
us_eastern::local_to_utc(t1);
std::cout && to_simple_string(t1) && " in New York is "
&& to_simple_string(t2) && " UTC time "
ptime t3 = us_eastern::utc_to_local(t2);//轉換回去,應該相同
std::cout && to_simple_string(t2) && " UTC is "
&& to_simple_string(t3) && " New York time "
&& "\n\n";
ptime t4(date(2002,May,31), hours(20)); //紐約時間的午夜前4個小時
std::cout && "UTC &--& New York while DST is active (4 hours)" && std::
ptime t5 = us_eastern::local_to_utc(t4);
std::cout && to_simple_string(t4) && " in New York is "
&& to_simple_string(t5) && " UTC time "
ptime t6 = us_eastern::utc_to_local(t5);//轉換回去,應該相同
std::cout && to_simple_string(t5) && " UTC is "
&& to_simple_string(t6) && " New York time "
&& "\n" && std::
//亞利桑那時區為 utc-7 無 dst
typedef boost::date_time::local_adjustor&ptime, -7, no_dst& us_
ptime t7(date(2002,May,31), hours(17));
std::cout && "UTC &--& Arizona (7 hours)" && std::
ptime t8 = us_arizona::local_to_utc(t7);
std::cout && to_simple_string(t7) && " in Arizona is "
&& to_simple_string(t8) && " UTC time "
示範有關時間段的一些簡單使用。
/* 有關構造和計算時間的一些簡單例子
* [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.]
* contains 2002-Feb-01 03:00:05
* [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.]
* intersected with
* [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.]
* [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.]
#include "boost/date_time/posix_time/posix_time.hpp"
#include &iostream&
using namespace boost::posix_
using namespace boost::
//創建一個簡單的 period 類,包括一天中的所有時間
class day_period : public time_period
day_period(date d) : time_period(ptime(d),//午夜
ptime(d,hours(24)))
date d(2002,Feb,1); //任意一天
//表示一天的時間段
day_period dp(d);
ptime t(d, hours(3)+seconds(5)); //那天的任意一個時間
if (dp.contains(t)) {
std::cout && to_simple_string(dp) && " contains "
&& to_simple_string(t)
//表示一天的一部分的時間段
time_period part_of_day(ptime(d, hours(0)), t);
//計算兩個時間的段的交集並打印結果
if (part_of_day.intersects(dp)) {
time_period result = part_of_day.intersection(dp);
std::cout && to_simple_string(dp) && " intersected with\n"
&& to_simple_string(part_of_day) && " is \n"
&& to_simple_string(result) && std::
簡單的時區
使用 custom_time_zone 和 posix_time_zone 的例子。
/* 使用 custom_time_zone 和 posix_time_zone 的簡單例子。
#include "boost/date_time/local_time/local_time.hpp"
#include &iostream&
using namespace local_
using posix_time::time_
/***** custom_time_zone *****/
// 為 custom_time_zone 創建相關的對象
time_zone_names tzn("Eastern Standard Time", "EST",
"Eastern Daylight Time", "EDT");
time_duration utc_offset(-5,0,0);
dst_adjustment_offsets adj_offsets(time_duration(1,0,0),
time_duration(2,0,0),
time_duration(2,0,0));
// 本時區的夏時制規則是:
// 從四月第一個星期天的 2 am 開始
// 到十月最後一個星期天的 2 am 結束
// 所以我們使用 first_last_dst_rule
first_day_of_the_week_in_month start_rule(Sunday, Apr);
last_day_of_the_week_in_month
end_rule(Sunday, Oct);
shared_ptr&dst_calc_rule& nyc_rules(new first_last_dst_rule(start_rule,
end_rule));
// 為無夏時制的 custom_time_zone 創建更多的相關對像
time_zone_names tzn2("Mountain Standard Time", "MST",
"", ""); // 無 dst 使用空的 dst 字符串
time_duration utc_offset2(-7,0,0);
dst_adjustment_offsets adj_offsets2(time_duration(0,0,0),
time_duration(0,0,0),
time_duration(0,0,0));
// 無 dst 意味著我們需要空指針來表示規則
shared_ptr&dst_calc_rule& phx_
// 創建 custom_time_zones
time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset,
adj_offsets, nyc_rules));
time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2,
adj_offsets2, phx_rules));
/***** posix_time_zone *****/
// 創建以上 custom_time_zones 的複製品 posix_time_zones.
// 有關時區名的更多細節請見 posix_time_zone 文檔。
std::string nyc_string, phx_
nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00";
// nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // 使用缺省值時更短
phx_string = "MST-07"; // 無 dst
time_zone_ptr nyc_2(new posix_time_zone(nyc_string));
time_zone_ptr phx_2(new posix_time_zone(phx_string));
/***** 顯示兩個設置相等 *****/
std::cout && "The first zone is in daylight savings from:\n "
&& nyc_1-&dst_local_start_time(2004) && " through "
&& nyc_1-&dst_local_end_time(2004) && std::
std::cout && "The second zone is in daylight savings from:\n "
&& nyc_2-&dst_local_start_time(2004) && " through "
&& nyc_2-&dst_local_end_time(2004) && std::
std::cout && "The third zone (no daylight savings):\n "
&& phx_1-&std_zone_abbrev() && " and "
&& phx_1-&base_utc_offset() && std::
std::cout && "The fourth zone (no daylight savings):\n "
&& phx_2-&std_zone_abbrev() && " and "
&& phx_2-&base_utc_offset() && std::
夏時制計算規則
創建多個夏時制計算規則對象的例子。
/* 一個簡單例子,創建多個 dst_calc_rule 實例
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/local_time/local_time.hpp"
#include &iostream&
using namespace local_
/***** 創建所需的 date_generator 對像 *****/
// 開始日期生成器
first_day_of_the_week_in_month fd_start(Sunday, May);
last_day_of_the_week_in_month ld_start(Sunday, May);
nth_day_of_the_week_in_month nkd_start(nth_day_of_the_week_in_month::third,
Sunday, May);
partial_date pd_start(1, May);
// 結束日期生成器
first_day_of_the_week_in_month fd_end(Sunday, Oct);
last_day_of_the_week_in_month ld_end(Sunday, Oct);
nth_day_of_the_week_in_month nkd_end(nth_day_of_the_week_in_month::third,
Sunday, Oct);
partial_date pd_end(31, Oct);
/***** 創建多個 dst_calc_rule 對像 *****/
dst_calc_rule_ptr pdr(new partial_date_dst_rule(pd_start, pd_end));
dst_calc_rule_ptr flr(new first_last_dst_rule(fd_start, ld_end));
dst_calc_rule_ptr llr(new last_last_dst_rule(ld_start, ld_end));
dst_calc_rule_ptr nlr(new nth_last_dst_rule(nkd_start, ld_end));
dst_calc_rule_ptr ndr(new nth_day_of_the_week_in_month_dst_rule(nkd_start,
nkd_end));
飛行時間例子
本例示範了如何計算飛機從鳳凰城飛到紐約的到達時間。在飛行中,紐約恰好轉入了夏時制(因為亞利桑那州不使用夏時制,所以鳳凰城也不使用)。
#include "boost/date_time/local_time/local_time.hpp"#include &iostream&int main(){
using namespace boost::gregorian;
using namespace boost::local_time;
using namespace boost::posix_time;
tz_database tz_db;
tz_db.load_from_file("date_time_zonespec.csv");
time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
time_zone_ptr phx_tz(new posix_time_zone("MST-07:00:00"));
local_date_time phx_departure(date(2005, Apr, 2), hours(23),
local_date_time::NOT_DATE_TIME_ON_ERROR);
time_duration flight_length = hours(4) + minutes(30);
local_date_time phx_arrival = phx_departure + flight_length;
local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz);
std::cout && "departure phx time: " && phx_departure && std::endl;
std::cout && "arrival phx time:
" && phx_arrival && std::endl;
std::cout && "arrival nyc time:
" && nyc_arrival && std::endl;
從紀元起計的秒數
這個例子使用 local_date_time 計算從紀元(1970-Jan-1)以來的秒數。
/* 本例示範了使用時區數據庫和本地時間來計算從 UTC
* time_t 紀元
00:00:00 起計的秒數。注意選用的時區
* 可以是時區數據庫文件中支持的任意時區,該數據庫文件可由用戶修改和更新。
* 為解決這個問題,需要以下步驟:
* 1) 從 tz 數據庫中取出本地時間的時區
* 2) 使用該時區構造一個本地時間
* 3) 為 time_t 紀元時間構造一個 posix_time::ptime
* 4) 將 local_time 轉換為 utc 並減去紀元時間
#include "boost/date_time/local_time/local_time.hpp"
#include &iostream&
int main()
using namespace boost::
using namespace boost::local_
using namespace boost::posix_
tz_database tz_
tz_db.load_from_file("../data/date_time_zonespec.csv");
}catch(data_not_accessible dna) {
std::cerr && "Error with time zone data file: " && dna.what() && std::
exit(EXIT_FAILURE);
}catch(bad_field_count bfc) {
std::cerr && "Error with time zone data file: " && bfc.what() && std::
exit(EXIT_FAILURE);
time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
date in_date();
time_duration td(12,14,32);
// 構造本地時間值
// 如果無效則創建 not-a-date-time (如:在夏時制轉換中)
local_date_time nyc_time(in_date,
local_date_time::NOT_DATE_TIME_ON_ERROR);
std::cout && nyc_time && std::
ptime time_t_epoch(date());
std::cout && time_t_epoch && std::
// 通過 utc_time() 將 nyc_time 轉換為 utc 並減去 ptime.
time_duration diff = nyc_time.utc_time() - time_t_
//正確結果
std::cout && "Seconds diff: " && diff.total_seconds() && std::
Copyright &
CrystalClear Software, Inc}

我要回帖

更多关于 德语缩写 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信