青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Beginning to 編程

VC++ 方面編程文章

 

時(shí)間和日歷類的設(shè)計(jì)

 

C++通用框架的設(shè)計(jì) 作者:naven

1           介紹

時(shí)間和日歷以及時(shí)間的格式化處理在軟件的設(shè)計(jì)中起著非常重要的作用,但是目前C++的庫卻未有一個(gè)簡單易用的時(shí)間類,大部分都需要開發(fā)者直接調(diào)用操作系統(tǒng)的API來完成,而且很多API都不是線程安全的。某些大型的C++框架雖然提供一些時(shí)間類,但是卻不通用,也很難直接拿出來使用。下面介紹一下參考Java Framework中的時(shí)間相關(guān)的類來設(shè)計(jì)并實(shí)現(xiàn)C++版本的時(shí)間和日歷類。

 

主要有如下一些類

 

Time類,對應(yīng)于Javajava.util.Date類,表示特定的瞬間,精確到毫秒(Linux可精確到微秒,Solaris可精確到十億分之一秒)。Time只表示某時(shí)某地的瞬間,從197011 00:00:00 GMT以來的微秒數(shù),無時(shí)區(qū)。

 

Calendar 類,對應(yīng)于Javajava.util.Calendar類,它既表示了Time的精確瞬間,還代表了此時(shí)的年、月、日、時(shí)區(qū)等。它為特定瞬間與一組諸如 YEARMONTHDAY_OF_MONTHHOUR 日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用微秒值來表示,它是距歷元(即格林威治標(biāo)準(zhǔn)時(shí)間 1970 1 1 日的 00:00:00.000GMT)的偏移量。

 

TimeFormat類,類似Javajava.text.SimpleDateFormat類,它用來將時(shí)間和日歷格式化成一個(gè)本地時(shí)間格式的文本形式,或者指定格式的文本形式。如果格式化時(shí)間,將缺省使用操作系統(tǒng)設(shè)定的本地(locale)時(shí)間格式處理。

 

TimeParser類,類似Javajava.text.SimpleDateFormat類,與TimeFormat相反,它用來將一個(gè)時(shí)間的文本轉(zhuǎn)化成一個(gè)本地時(shí)間Time,缺省使用操作系統(tǒng)設(shè)定的本地時(shí)間格式處理。

 

2           Hello World!

下面介紹一下它們的使用

 

void main() 
{
    
// 獲取當(dāng)前時(shí)間,精確到微秒
    Time t = Time::getCurrentTime(); 
    
    
// 使用CRT的API格式化時(shí)間,只能精確到秒
    time_t tt = t.sec(); 
    printf(
"\n%s\n", ctime(&tt)); 
    
    
// 使用系統(tǒng)缺省的locale輸出格式化時(shí)間文本
    TimeFormat tf("%c"); 
    printf(
"\n%s\n", tf.format(t).c_str()); 
    
    
// 使用系統(tǒng)缺省的locale輸出格式化時(shí)間文本
    TimeFormat tf2("%#c"); 
    printf(
"\n%s\n", tf2.format(t).c_str()); 
    
    
// 自定義輸出格式化時(shí)間文本,精確到豪秒
    TimeFormat tff("%Y-%m-%d %H:%M:%S.%q"); 
    printf(
"\n%s\n", tf2.format(t).c_str()); 
    
    
// 自定義輸出格式化時(shí)間文本,精確到豪秒,輸出所有時(shí)間信息,包括時(shí)區(qū)和年代等
    TimeFormat tfCN("%G %Y年%B%d日 %A %H時(shí)%M分%S秒%q豪秒 時(shí)區(qū)%z""zh_CN.gb2312"); 
    printf(
"\n%s\n", tfCN.format(t).c_str()); 
    
    
// 使用自定義 zh_CN.gb2312 的locale和字符集輸出格式化時(shí)間文本,不受系統(tǒng)locale影響
    TimeFormat tfCN2("%G %Y %b %d %a %H:%M:%S.%q %z""zh_CN.gb2312"); 
    printf(
"\n%s\n", tfCN2.format(t).c_str()); 
    
    
// 使用自定義 en_US.iso8859-1 的locale和字符集輸出格式化時(shí)間文本,不受系統(tǒng)locale影響
    TimeFormat tfUS("%G %d %B %Y %A %H:%M:%S.%q TZ:%z""en_US.iso8859-1"); 
    printf(
"\n%s\n", tfUS.format(t).c_str()); 
    
    TimeFormat tfUS2(
"%G %Y %b %d %a %H:%M:%S.%q %z""en_US.iso8859-1"); 
    printf(
"\n%s\n", tfUS2.format(t).c_str()); 
    
    
// 使用 MIME 標(biāo)準(zhǔn)格式輸出格式化時(shí)間文本
    TimeFormat tfMIME("%a, %d %b %Y %H:%M:%S %z"); 
    printf(
"\n%s\n", tfMIME.format(t).c_str()); 

    
// 使用 MimeParse 方法轉(zhuǎn)換時(shí)間
    Time t2 = tf.mimeParse(tf.mimeFormat(t)); 
    printf(
"\n%s\n", tfCN.format(t2).c_str()); 
    
}

 

編譯程序?qū)⑤斎肴缦陆Y(jié)果

 

Wed Nov  9 16:09:40 2005


11/09/05 16:09:40

Wednesday, November 
092005 16:09:40

Wednesday, November 
092005 16:09:40

公元 2005年十一月09日 星期三 16時(shí)09分40秒078豪秒 時(shí)區(qū)
+0800

公元 
2005 11月 09 周三 16:09:40.078 +0800

AD 
09 November 2005 Wednesday 16:09:40.078 TZ:+0800

AD 
2005 Nov 09 Wed 16:09:40.078 +0800

Wed, 
09 Nov 2005 16:09:40 +0800

公元 2005年十一月09日 星期三 16時(shí)09分40秒000豪秒 時(shí)區(qū)
+0800

 

3           Time

由于Time要精確到微秒,所以Time類使用timeval結(jié)構(gòu)存儲(chǔ)時(shí)間,該結(jié)構(gòu)包含兩個(gè)long整形,一個(gè)表示秒數(shù)(從197011 00:00:00 GMT以來的偏移量),一個(gè)表示微秒數(shù)。

 

Time類的定義如下

 

class Time 

private:
    
/**
     * Store the values as a timeval which fields as.
     * struct timeval {
     *      long tv_sec;   // seconds
     *      long tv_usec;  // microseconds 
     
*/

    
struct timeval _tv;
}

 

Time類獲取當(dāng)前精確到微秒的實(shí)現(xiàn)如下(Win32提供的API只能精確到豪秒)

 

Time
Time::gettimeofday()
{
#if defined(HAVE_GETTIMEOFDAY)
    
struct timeval tp;
    tp.tv_sec 
= 0
    tp.tv_usec 
= 0
#ifdef IS_SOLARIS_OS
    ::gettimeofday(
&tp, 0); // microseconds = 1/1,000,000 sec
    Time nowtime(tp); 
#else
    
struct timezone tz; 
    tz.tz_dsttime 
= 0
    tz.tz_minuteswest 
= 0
    ::gettimeofday(
&tp, &tz); // microseconds = 1/1,000,000 sec
    Time nowtime(tp); 
#endif
    
return nowtime; 

#elif defined(HAVE_FTIME)
    
struct timeb tb;
    tb.dstflag 
= 0
    tb.millitm 
= 0
    tb.time 
= 0
    tb.timezone 
= 0
    ftime(
&tb);  // milliseconds = 1/1,000 sec
    Time nowtime(tb); 
    
return nowtime; 

#elif defined(HAVE_CLOCK_GETTIME)
    
struct timespec ts; 
    ts.tv_sec 
= 0
    ts.tv_nsec 
= 0
    ::clock_gettime(CLOCK_REALTIME, 
&ts); // nanoseconds = 1/1,000,000,000 sec
    Time nowtime(ts); 
    
return nowtime; 

#else
//#warning "Time::gettimeofday()- low resolution timer: gettimeofday and ftime unavailable"
    Time nowtime(::time(0), 0); // seconds
    return nowtime; 

#endif
}

 

Time類提供很多方法如果構(gòu)造方法和操作符等,可以在time_t和其他時(shí)間類型之間轉(zhuǎn)換,也可以單獨(dú)獲取和設(shè)置時(shí)間的秒和微秒。

 

4           Calendar

Calendar類是一個(gè)表示日歷的類,它可以實(shí)現(xiàn)日歷的向前向后前進(jìn)等所有功能。例如你可以設(shè)置,獲取,和操縱一個(gè)日期對象的各個(gè)部分,比方一個(gè)月的一天或者是一個(gè)星期的一天。舉例如下:

 

    // 定義日歷對象并設(shè)置為當(dāng)前時(shí)間
    Time nowtm = Time::getCurrentTime(); 
    TimeFormat ntf(
"%Y-%m-%d %H:%M:%S.%q"); 
    printf(
"\n%s\n", ntf.format(nowtm).c_str()); 
    
    Calendar cal; 
    cal.setTime(nowtm); 
    
    
// 年月日都向前移動(dòng)一單位
    cal.rollUpYear(); 
    cal.rollUpMonth(); 
    cal.rollUpDayOfMonth(); 
    
    String scal; 
    ntf.format(cal, scal); 
    printf(
"\n%s\n", scal.c_str());

 

程序輸出結(jié)果

 

2005-11-09 16:45:54.589

2006-12-10 16:45:54.589

 

Calendar類是建立在Time類的基礎(chǔ)上的,并加入了時(shí)區(qū)等信息,它的定義看起來如下所示:

 

class Calendar
{
protected
    
/**
     * Value of the <code>ERA</code> field indicating
     * the period before the common era (before Christ), also known as BCE.
     * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
     * , 2 BC, 1 BC, 1 AD, 2 AD,
     * @see Calendar#ERA
     
*/

    
int _era; 

    
/**
     * The currently set time for this calendar, expressed in milliseconds after
     * January 1, 1970, 0:00:00 GMT.
     * @see #isTimeSet
     * @serial
     
*/

    Time _time;

    
/**
     * The <code>TimeZone</code> used by this calendar. </code>Calendar</code>
     * uses the time zone data to translate between locale and GMT time.
     * @serial
     
*/

    
struct timezone _zone;

    
/**
     *  The asctime() and mktime() functions both take an argument
     *  representing  broken-down time which is a binary represen-
     *  tation separated into year, month, day, etc.   Broken-down
     *  time  is  stored  in  the structure tm which is defined in
     *  <time.h> as follows:<P>
     *  <code>
     *         struct tm
     *         {
     *                 int     tm_sec;         // seconds 
     *                 int     tm_min;         // minutes 
     *                 int     tm_hour;        // hours 
     *                 int     tm_mday;        // day of the month 
     *                 int     tm_mon;         // month 
     *                 int     tm_year;        // year 
     *                 int     tm_wday;        // day of the week 
     *                 int     tm_yday;        // day in the year 
     *                 int     tm_isdst;       // daylight saving time 
     *         };
     *  </code>
     
*/

    
struct tm _tm; 
    
}

 

Calendar類定義了很多方法和操作符用來在Time類和時(shí)區(qū)、年月日等之間轉(zhuǎn)換和設(shè)置、讀取等。Calendar類實(shí)現(xiàn)時(shí)間的生成、轉(zhuǎn)換等都是自己實(shí)現(xiàn)的,并不調(diào)用操作系統(tǒng)的APImktime()等,并不使用CRTC運(yùn)行時(shí))的全局變量如timezone等,所以它是線程安全的,每一個(gè)Calendar對象都是互相獨(dú)立的,擁有自己的時(shí)區(qū)等信息。

 

5           TimeFormat

TimeFormat類主要實(shí)現(xiàn)了將時(shí)間格式化成一個(gè)時(shí)間文本,可以使用系統(tǒng)缺省的本地格式,也可以指定格式轉(zhuǎn)換。轉(zhuǎn)換的用法如下:

 

TimeFormat tf("%Y-%m-%d %H:%M:%S.%q"); //定義一個(gè)格式
Time t = Time::getCurrentTime(); 
String s 
= tf.format(t); // 將時(shí)間對象格式化成文本字符串

 

時(shí)間格式化pattern有如下幾種

 

    %a     The abbreviated weekday name according to the current locale.

    %A     The full weekday name according to the current locale.

    %b     The abbreviated month name according to the current locale.

    %B     The full month name according to the current locale.

    %c     The preferred date and time representation for the current locale.

    %C     The century number (year/100) as a 2-digit integer. (SU)

    %d     The day of the month as a decimal number (range 01 to 31).

    %D     Equivalent  to  %m/%d/%y. (Yecch - for Americans only. 

           Americans should note that in other countries

           %d/%m/%y is rather common. This means that in international context 

           this  format  is  ambiguous  and should not be used.) (SU)

    %e     Like %d, the day of the month as a decimal number, but a leading

           zero is replaced by a space. (SU)

    %E     Modifier: use alternative format, see below. (SU)

    %G     The  ISO  8601 year with century as a decimal number.  The 4-digit

           year corresponding to the ISO week number (see %V).  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. (TZ)

    %g     Like %G, but without century, i.e., with a 2-digit year (00-99). (TZ)

    %h     Equivalent to %b. (SU)

    %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).

    %j     The day of the year as a decimal number (range 001 to 366).

    %k     The  hour (24-hour clock) as a decimal number (range 0 to 23); single

           digits are preceded by a blank. (See also %H.) (TZ)

    %l     The hour (12-hour clock) as a decimal number (range 1 to 12); single

           digits are preceded by a  blank. (See also %I.) (TZ)

    %m     The month as a decimal number (range 01 to 12).

    %M     The minute as a decimal number (range 00 to 59).

    %n     A newline character. (SU)

    %O     Modifier: use alternative format, see below. (SU)

    %p     Either  `AM'  or `PM' according to the given time value, or the

           corresponding strings for the current locale.  Noon is treated as `pm'

           and midnight as `am'.

    %P     Like %p but in lowercase: `am' or `pm' or a corresponding string for

           the current locale. (GNU)

    %r     The time in a.m. or p.m. notation.  In the POSIX locale this is

           equivalent to `%I:%M:%S %p'. (SU)

    %R     The time in 24-hour notation (%H:%M). (SU) For a version including the

           seconds, see %T below.

    %s     The number of seconds since the Epoch, i.e., since 1970-01-01 00:00:00 UTC.

           (TZ)

    %S     The second as a decimal number (range 00 to 61).

    %t     A tab character. (SU)

    %T     The time in 24-hour notation (%H:%M:%S). (SU)

    %u     The day of the week as a decimal, range 1 to 7, Monday being 1. 

           See also %w. (SU)

    %U     The week number of the current year as a decimal number, range 00 to 53,

           starting with the first Sun? day as the first day of week 01.

           See also %V and %W.

    %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.

           See also %U and %W. (SU)

    %w     The day of the week as a decimal, range 0 to 6, Sunday being 0.  See also %u.

    %W     The week number of the current year as a decimal number, range 00 to 53,

           starting with the first Mon? day as the first day of week 01.

    %x     The preferred date representation for the current locale without the time.

    %X     The preferred time representation for the current locale without the date.

    %y     The year as a decimal number without a century (range 00 to 99).

    %Y     The year as a decimal number including the century.

    %z     The time-zone as hour offset from GMT.  Required to emit RFC822-conformant

           dates (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)

    %Z     The time zone or name or abbreviation.

    %+     The date and time in date(1) format. (TZ)

    %%     A literal `%' character.

 

6           TimeParser

TimeParser類實(shí)現(xiàn)與TimeFormat相反的功能,是將一個(gè)指定格式的時(shí)間文本轉(zhuǎn)換成時(shí)間Time對象或Calendar對象,它的設(shè)計(jì)與TimeFormat類似,目前還未全部完成,只實(shí)現(xiàn)了轉(zhuǎn)換Mime時(shí)間格式的文本的功能。

 

示例見上面的 Hello World 程序。

 

 

C++通用框架的設(shè)計(jì) 作者:naven 日期:2005-11-9

posted on 2006-03-10 10:56 Beginning to 編程 閱讀(650) 評論(0)  編輯 收藏 引用 所屬分類: 關(guān)鍵代碼

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

留言簿(4)

隨筆分類

隨筆檔案

文章檔案

相冊

BlogDev

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品久久波多野结衣| 欧美gay视频激情| 国产精品青草久久久久福利99| 亚洲精品欧美日韩| 亚洲电影欧美电影有声小说| 欧美一区在线直播| 好看的日韩av电影| 男人插女人欧美| 免费观看在线综合色| 亚洲黄色尤物视频| 99视频一区| 国产亚洲精品7777| 欧美大片91| 欧美日韩精品免费在线观看视频| 亚洲一区二区伦理| 午夜激情综合网| 黑人一区二区| 亚洲国产成人精品视频| 欧美人成网站| 久久成人人人人精品欧| 久久男女视频| 在线亚洲观看| 欧美在线日韩在线| 国产精品国产三级国产专区53| 欧美在线免费观看亚洲| 久久最新视频| 亚洲自拍偷拍网址| 久久婷婷av| 99视频一区| 亚洲一区二区三区色| 影音先锋日韩有码| 一区二区三区欧美亚洲| 国产一区在线免费观看| 亚洲激情黄色| 一本不卡影院| 欧美三日本三级三级在线播放| 国产亚洲综合精品| 蜜臀av一级做a爰片久久| 免费观看一区| 国产精品美女久久久久av超清| 久久精品在线播放| 国产亚洲精品久久久久久| 亚洲日本欧美| 99这里有精品| 欧美午夜久久久| 99国产精品久久久| 99re热这里只有精品视频| 日韩网站在线| 一区二区三区视频在线观看| 欧美电影美腿模特1979在线看| 99国产精品久久久久老师| 久久综合网色—综合色88| 在线观看欧美精品| 亚洲三级影院| 亚洲欧美日韩国产一区二区三区| 一区二区三区在线高清| 男人的天堂亚洲| 99精品欧美一区| 欧美sm视频| 久久国产日韩| 欧美日韩亚洲另类| 免费日韩av| 国产乱人伦精品一区二区| 亚洲无线视频| 欧美成人精精品一区二区频| 美女主播视频一区| 亚洲欧美日韩综合aⅴ视频| 久久久综合网站| 樱桃成人精品视频在线播放| 欧美激情综合在线| 亚洲欧美综合另类中字| 欧美国产激情二区三区| 亚洲影院免费| 在线播放中文一区| 久久精品国产免费| 久久久久久一区二区| 一区二区日韩伦理片| 亚洲国产精品尤物yw在线观看| 国产视频亚洲精品| 欧美日韩在线大尺度| 欧美成黄导航| 欧美日韩精品在线播放| 欧美日本亚洲视频| 一二三区精品福利视频| 亚洲欧美国产视频| 亚洲国产成人av| 亚洲国产1区| 日韩视频中午一区| 中文精品一区二区三区| 欧美日韩精品一区二区在线播放 | 亚洲美女色禁图| 久久女同精品一区二区| 亚洲欧美中日韩| 久久精品女人的天堂av| 亚洲小说欧美另类社区| 亚洲欧美一区二区激情| 美女黄毛**国产精品啪啪| 美国成人毛片| 欧美日韩综合视频网址| 国产精品色婷婷| 在线高清一区| 亚洲视频欧洲视频| 欧美一级大片在线免费观看| 久久久精品2019中文字幕神马| 欧美电影免费观看高清| 野花国产精品入口| 久久久一本精品99久久精品66| 国产精品久久国产三级国电话系列| 夜夜夜久久久| 香港久久久电影| 老色鬼精品视频在线观看播放| 欧美精品久久久久久久免费观看| 国产日韩精品视频一区| 亚洲网站在线看| 国产日韩欧美高清| 国产精品美女久久| 国产精品视频内| 欧美在线观看一区| 欧美日韩一二三四五区| 亚洲一级电影| 99视频精品全部免费在线| 欧美三区在线视频| 久久久噜噜噜久久中文字免| 亚洲激情亚洲| 羞羞色国产精品| 亚洲黄色毛片| 国产女主播一区| 欧美精品一区二区精品网| 亚洲一区二区黄色| 欧美激情一区二区三区蜜桃视频 | 欧美在线视频全部完| 91久久久久久久久久久久久| 久久av资源网| 在线一区视频| 亚洲线精品一区二区三区八戒| 欧美国产一区视频在线观看| 一区二区亚洲精品| 亚洲精品久久久久久下一站 | 日韩视频二区| 久久久91精品| 亚洲电影免费观看高清完整版| 久久免费精品视频| 欧美有码在线视频| 在线观看视频日韩| 久久久久国产一区二区三区| 亚洲视频999| 国产日韩在线看片| 久久精品亚洲一区二区| 久久成人精品| 91久久国产综合久久蜜月精品| 久久久久久久精| 欧美体内she精视频| 亚洲在线网站| 久久性天堂网| 久久嫩草精品久久久精品一| 亚洲精品系列| 久久女同互慰一区二区三区| 国产精品久久777777毛茸茸| 久久午夜电影| 久久综合九色综合欧美就去吻 | 99视频超级精品| 在线播放日韩欧美| 欧美女同视频| 免费日韩一区二区| 久久午夜国产精品| 久久国产主播| 久久精品观看| 久久精品日韩一区二区三区| 亚洲欧美在线磁力| 亚洲一二三级电影| 亚洲一区二区三区乱码aⅴ| 亚洲免费观看高清完整版在线观看| 亚洲电影免费观看高清完整版| 农村妇女精品| 一本色道久久综合| 亚洲精品乱码久久久久久日本蜜臀 | 免费欧美电影| 欧美日韩网站| 麻豆成人91精品二区三区| 亚洲曰本av电影| 国产精品久久久一区麻豆最新章节| 欧美黄色免费网站| 欧美大片在线看免费观看| 欧美v日韩v国产v| 欧美国产日韩在线观看| 欧美成人一区二区| 欧美精品一区二区三区四区| 欧美日韩专区在线| 国产精品日日摸夜夜添夜夜av | 亚洲综合三区| 久久爱www| 欧美 日韩 国产精品免费观看| 欧美电影资源| 欧美日韩在线播放三区四区| 国产精品午夜视频| 国产一区二区三区四区三区四| 1024国产精品| 一区二区91| 久久国产视频网| 免费欧美在线|