时间和日历以及时间的格式化处理在软件的设计中起着非常重要的作用,但是目前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() 

 

编译程序将输入如下结果

 

时间和日历类的设计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 

 

Time类获取当前精确到微秒的实现如下(Win32提供的API只能精确到豪秒)

 

时间和日历类的设计Time
时间和日历类的设计Time::gettimeofday()

 

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

 

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2022-12-23
  • 2021-06-25
  • 2021-04-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案