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

Javen-Studio 咖啡小屋

http://javenstudio.org - C++ Java 分布式 搜索引擎
Naven's Research Laboratory - Thinking of Life, Imagination of Future

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks

時間和日歷類的設計

C++通用框架的設計 作者:naven

1           介紹

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

 

主要有如下一些類

 

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

 

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

 

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

 

TimeParser類,類似Javajava.text.SimpleDateFormat類,與TimeFormat相反,它用來將一個時間的文本轉化成一個本地時間Time,缺省使用操作系統設定的本地時間格式處理。

 

2           Hello World!

下面介紹一下它們的使用

 

void main() 
{
    
// 獲取當前時間,精確到微秒
    Time t = Time::getCurrentTime(); 
    
    
// 使用CRT的API格式化時間,只能精確到秒
    time_t tt = t.sec(); 
    printf(
"\n%s\n", ctime(&tt)); 
    
    
// 使用系統缺省的locale輸出格式化時間文本
    TimeFormat tf("%c"); 
    printf(
"\n%s\n", tf.format(t).c_str()); 
    
    
// 使用系統缺省的locale輸出格式化時間文本
    TimeFormat tf2("%#c"); 
    printf(
"\n%s\n", tf2.format(t).c_str()); 
    
    
// 自定義輸出格式化時間文本,精確到豪秒
    TimeFormat tff("%Y-%m-%d %H:%M:%S.%q"); 
    printf(
"\n%s\n", tf2.format(t).c_str()); 
    
    
// 自定義輸出格式化時間文本,精確到豪秒,輸出所有時間信息,包括時區和年代等
    TimeFormat tfCN("%G %Y年%B%d日 %A %H時%M分%S秒%q豪秒 時區%z""zh_CN.gb2312"); 
    printf(
"\n%s\n", tfCN.format(t).c_str()); 
    
    
// 使用自定義 zh_CN.gb2312 的locale和字符集輸出格式化時間文本,不受系統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和字符集輸出格式化時間文本,不受系統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 標準格式輸出格式化時間文本
    TimeFormat tfMIME("%a, %d %b %Y %H:%M:%S %z"); 
    printf(
"\n%s\n", tfMIME.format(t).c_str()); 

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

 

編譯程序將輸入如下結果

 

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時09分40秒078豪秒 時區
+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時09分40秒000豪秒 時區
+0800

 

3           Time

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

 

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類獲取當前精確到微秒的實現如下(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類提供很多方法如果構造方法和操作符等,可以在time_t和其他時間類型之間轉換,也可以單獨獲取和設置時間的秒和微秒。

 

4           Calendar

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

 

    // 定義日歷對象并設置為當前時間
    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); 
    
    
// 年月日都向前移動一單位
    cal.rollUpYear(); 
    cal.rollUpMonth(); 
    cal.rollUpDayOfMonth(); 
    
    String scal; 
    ntf.format(cal, scal); 
    printf(
"\n%s\n", scal.c_str());

 

程序輸出結果

 

2005-11-09 16:45:54.589

2006-12-10 16:45:54.589

 

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

 

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類和時區、年月日等之間轉換和設置、讀取等。Calendar類實現時間的生成、轉換等都是自己實現的,并不調用操作系統的APImktime()等,并不使用CRTC運行時)的全局變量如timezone等,所以它是線程安全的,每一個Calendar對象都是互相獨立的,擁有自己的時區等信息。

 

5           TimeFormat

TimeFormat類主要實現了將時間格式化成一個時間文本,可以使用系統缺省的本地格式,也可以指定格式轉換。轉換的用法如下:

 

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

 

時間格式化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類實現與TimeFormat相反的功能,是將一個指定格式的時間文本轉換成時間Time對象或Calendar對象,它的設計與TimeFormat類似,目前還未全部完成,只實現了轉換Mime時間格式的文本的功能。

 

示例見上面的 Hello World 程序。

 

 

C++通用框架的設計 作者:naven 日期:2005-11-9

 

posted on 2005-11-09 17:28 Javen-Studio 閱讀(8385) 評論(5)  編輯 收藏 引用

評論

# 時間和日歷類的設計(Java的Date和Calendar的C 實現)[TrackBack] 2005-11-09 17:47 Naven
時間和日歷以及時間的格式化處理在軟件的設計中起著非常重要的作用,但是目前C 的庫卻未有一個簡單易用的時間類,大部分都需要開發者直接調用操作系統的API來完成,而且很多API都不是線程安全的。某些大型的C 框架雖然提供一些時間類,但是卻不通用,也很難直接拿出來使用。下面介紹一下參考Java Framework中的時間相關的類來設計并實現C 版本的時間和日歷類。

閱讀請點 http://m.shnenglu.com/javenstudio/articles/1018.html
  查看原文  回復  更多評論
  

# re: 時間和日歷類的設計(Java的Date和Calendar的C++實現) 2006-03-17 14:09 slummer
非常棒!!!
能開源碼?  回復  更多評論
  

# re: 時間和日歷類的設計(Java的Date和Calendar的C++實現) 2006-03-18 21:11 Javen-Studio
想打算以后開源,不過現在太忙了
所以我先介紹一下我的設計思想,過些時間有空再提供一個共享庫試用
謝謝  回復  更多評論
  

# re: 時間和日歷類的設計(Java的Date和Calendar的C++實現) 2006-09-26 23:08 yiwangt
不錯.不過好像沒有用JAVA實現日歷昨顯示功能.  回復  更多評論
  

# re: 時間和日歷類的設計(Java的Date和Calendar的C++實現) 2007-09-16 11:49 Javen-Studio
主要是根據自己應用的需要而開發的,所以很多都沒時間實現了
  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久久999精品| 久久午夜羞羞影院免费观看| 国产精品视频免费观看www| 欧美人在线观看| 欧美激情精品久久久久久蜜臀| 久久婷婷激情| 欧美国产欧美亚州国产日韩mv天天看完整| 模特精品在线| 欧美午夜不卡| 国内精品一区二区| 亚洲人成在线影院| 亚洲欧美电影在线观看| 久久久亚洲一区| 91久久精品国产91久久性色tv| 欧美激情一区在线| 亚洲桃花岛网站| 久久午夜国产精品| 国产精品白丝jk黑袜喷水| 国内精品久久久久影院优 | 欧美二区在线播放| 国产精品xvideos88| 在线观看成人一级片| 一本色道久久综合亚洲二区三区| 午夜精品国产| 亚洲国产日韩欧美在线图片| 亚洲女女女同性video| 欧美成在线视频| 国产香蕉97碰碰久久人人| 亚洲日本激情| 久久这里只精品最新地址| 一区二区三区欧美日韩| 美日韩精品免费观看视频| 国产伦一区二区三区色一情| 日韩亚洲在线| 欧美激情片在线观看| 亚洲综合社区| 欧美日韩另类视频| 亚洲三级观看| 欧美成人高清视频| 久久精品国产久精国产爱 | 日韩写真在线| 免费不卡欧美自拍视频| 亚洲欧美成人一区二区三区| 欧美二区视频| 亚洲男人av电影| aa成人免费视频| 亚洲永久免费精品| 欧美黄色aaaa| 久久久久亚洲综合| 国产又爽又黄的激情精品视频| 亚洲一区制服诱惑| 亚洲人成网站777色婷婷| 久久成人精品电影| 国产视频一区在线观看| 香蕉亚洲视频| 亚洲综合不卡| 国产精品亚洲网站| 亚洲欧美日本视频在线观看| 99re热精品| 欧美午夜片欧美片在线观看| 一区二区三区黄色| 夜夜嗨网站十八久久| 欧美日韩综合精品| 亚洲一区亚洲| 午夜欧美大片免费观看| 国产欧美日本一区视频| 小处雏高清一区二区三区| 亚洲视频1区| 国产日产欧美a一级在线| 久久久国产精品一区二区中文| 午夜精品成人在线| 国内成+人亚洲+欧美+综合在线| 久久久久久精| 久久人体大胆视频| 日韩视频在线免费| 中文在线一区| 国产日韩欧美亚洲一区| 久久久亚洲一区| 裸体一区二区三区| 中文在线一区| 欧美资源在线观看| 亚洲三级免费观看| 亚洲一区二区三区777| 黑人操亚洲美女惩罚| 亚洲国产精品va在线观看黑人| 欧美日本亚洲| 欧美一级视频精品观看| 久久精品一区四区| 9色精品在线| 久久av一区二区| 一区二区三区欧美视频| 久久er精品视频| 99pao成人国产永久免费视频| 夜夜爽99久久国产综合精品女不卡| 国产精品毛片在线看| 欧美丰满少妇xxxbbb| 国产精品日韩欧美大师| 欧美1级日本1级| 国产精品国产福利国产秒拍| 麻豆精品精华液| 国产精品久久77777| 欧美成人一品| 国产日韩精品视频一区| 亚洲国产精品成人综合色在线婷婷| 亚洲精选一区| 激情综合网激情| 亚洲视频一区二区在线观看| 精品999网站| 国产精品99久久久久久有的能看| 伊人狠狠色j香婷婷综合| 夜夜嗨av一区二区三区免费区| 曰本成人黄色| 欧美一级网站| 午夜影院日韩| 欧美午夜不卡视频| 亚洲国产精品毛片| 亚洲春色另类小说| 欧美一区二区日韩一区二区| 亚洲主播在线观看| 欧美激情综合网| 欧美成人dvd在线视频| 国产亚洲亚洲| 小嫩嫩精品导航| 先锋影音国产精品| 国产精品久久久久高潮| 日韩视频中文字幕| 99国产精品久久久久久久久久 | 在线日本成人| 欧美一区午夜精品| 午夜久久99| 国产日韩一区欧美| 性做久久久久久免费观看欧美| 亚洲综合欧美日韩| 国产精品白丝av嫩草影院| 一二三区精品福利视频| 中日韩高清电影网| 国产精品久久久久久久久| 99ri日韩精品视频| 亚洲影院免费观看| 国产伦精品一区二区三区视频孕妇 | 亚洲砖区区免费| 欧美日韩亚洲91| 在线一区免费观看| 欧美在线播放高清精品| 国产三区二区一区久久| 欧美在线www| 女同性一区二区三区人了人一| 亚洲成在人线av| 欧美精品亚洲一区二区在线播放| 亚洲日本欧美在线| 亚洲免费视频观看| 国产亚洲激情视频在线| 性欧美超级视频| 欧美国产大片| 亚洲免费中文| 激情丁香综合| 欧美日韩国产美| 先锋影院在线亚洲| 欧美风情在线观看| 亚洲欧洲av一区二区| 一区三区视频| 欧美日韩一区二| 久久国产精品免费一区| 最新日韩av| 久久精品视频99| 99热精品在线观看| 国产偷国产偷亚洲高清97cao| 免费看av成人| 亚洲少妇诱惑| 好吊日精品视频| 欧美日韩精品免费看| 欧美一区免费视频| 亚洲欧洲精品一区二区精品久久久| 一区二区三区国产在线| 国产亚洲人成网站在线观看| 欧美插天视频在线播放| 亚洲午夜羞羞片| 亚洲国产mv| 久久婷婷国产综合尤物精品| 亚洲最新视频在线| 激情欧美丁香| 国产精品国产三级国产专播品爱网 | 国产精品www.| 久久美女性网| 亚洲在线不卡| 亚洲精品国精品久久99热一| 久久国产欧美精品| 亚洲中字在线| 99视频精品在线| 亚洲高清不卡| 狠狠爱综合网| 国产美女精品在线| 国产精品a久久久久| 欧美国产欧美综合| 久久―日本道色综合久久| 午夜精品久久久久久| 99这里只有久久精品视频| 欧美aⅴ99久久黑人专区| 久久精品国产免费看久久精品| 亚洲视频精品|