求助求助!定义DateTime和c timespan 格式化类来给定任意日期,计算该天是周几!非常感谢!

TimeSpan 结构& 表示一个时间间隔。
命名空间:System 程序集:mscorlib(在 mscorlib.dll 中)
说明: 1.DateTime值类型代表了一个从公元日0点0分0秒到公元日23点59分59秒之间的具体日期时刻。因此,你可以用DateTime值类型来描述任何在想象范围之内的时间。TimeSpan值包含了许多属性与方法,用于访问或处理一个TimeSpan值,
其中的五个重载方法之一的结构 TimeSpan( int days, int hours, int minutes, int seconds )
下面的列表涵盖了其中的一部分方法及属性解释
Add:与另一个TimeSpan值相加。
Days:返回用天数计算的TimeSpan值。
Duration:获取TimeSpan的绝对值。
Hours:返回用小时计算的TimeSpan值
Milliseconds:返回用毫秒计算的TimeSpan值。
Minutes:返回用分钟计算的TimeSpan值。
Negate:返回当前实例的相反数。
Seconds:返回用秒计算的TimeSpan值。
Subtract:从中减去另一个TimeSpan值。
Ticks:返回TimeSpan值的tick数。
TotalDays:返回TimeSpan值表示的天数。
TotalHours:返回TimeSpan值表示的小时数。
TotalMilliseconds:返回TimeSpan值表示的毫秒数。
TotalMinutes:返回TimeSpan值表示的分钟数。
TotalSeconds:返回TimeSpan值表示的秒数。
TimeSpan ts = new TimeSpan(12, 12, 11, 12, 21);
int days = new TimeSpan(12, 12, 11, 12, 21).D
TimeSpan duration = new TimeSpan(-12, 12, 11, 12, 21).Duration();//24小时的反值
double totalDays = new TimeSpan(12, 12, 11, 12, 21).TotalD
double totalHours = new TimeSpan(2, 12, 11, 12, 21).TotalH
int hours = new TimeSpan(12, 12, 11, 12, 21).H
int minutes = new TimeSpan(12, 12, 11, 12, 21).M
TimeSpan ts2 = new TimeSpan(1, 3, 01, 12, 21);
// TimeSpan ts2 = new TimeSpan(5, 3, 1, 12, 21);//this is also right
TimeSpan ts3 = new TimeSpan(5, 3, 01, 12, 21).Add(ts);
TimeSpan ts4 = new TimeSpan(4, 3, 01, 12, 21).Subtract(ts);
TimeSpan ts5 = new TimeSpan(4, 3, 01, 12, 21).Subtract(ts).Duration();
TimeSpan Negate = new TimeSpan(5, 3, 01, 12, 21).Negate();
DateTime dtnow = DateTime.N
Console.WriteLine(ts);
Console.WriteLine(days);
Console.WriteLine(duration);
Console.WriteLine(totalDays);
Console.WriteLine(totalHours);
Console.WriteLine(hours);
Console.WriteLine(minutes);
Console.WriteLine(ts2);
Console.WriteLine(ts+ts2);
Console.WriteLine(ts3);
Console.WriteLine("Subtract:"+ts4);
Console.WriteLine("Duration:" + ts5);
Console.WriteLine(ts - ts2);
Console.WriteLine(Negate);
DateTime span = DateTime.Now.Add(ts2);
TimeSpan spdate = span - DateTime.N
long dateTicks = DateTime.Now.Add(ts2).T
Console.WriteLine(span);
Console.WriteLine(spdate);
Console.WriteLine(dateTicks);
上面是较晚的日期减较早的日期,所以各属性值为正数,如果是较早的日期减较晚的日期,则属性值为负数。
ASP.NET 中,两个时间相减,得到一个 TimeSpan 实例,TimeSpan 有一些属性:Days、TotalDays、Hours、TotalHours、Minutes、TotalMinutes、Seconds、TotalSeconds、Ticks,注意没有 TotalTicks。
&时间 1 是
用时间 2 减时间 1,得到一个 TimeSpan 实例。
那么时间 2 比时间 1 多 9 天 23 小时 59 分 59 秒。
那么,Days 就是 9,Hours 就是 23,Minutes 就是 59,Seconds 就是 59。
再来看 Ticks,Tick 是一个计时周期,表示一百纳秒,即一千万分之一秒,那么 Ticks 在这里表示总共相差多少个时间周期,即:9 * 24 * 3600 *
+ 23 * 3600 *
+59 * 60 *
= 0。3600 是一小时的秒数。
TotalDays 就是把 Ticks 换算成日数,即:0 / ( * 24 * 3600) = 9.93。
TotalHours 就是把 Ticks 换算成小时数,即:0 / ( * 3600) = 239.。
TotalMinutes 就是把 Ticks 换算成分钟数,即:0 / ( * 60) = 3333。
TotalSeconds 就是把 Ticks 换算成秒数,即:0 / () = 863999。
以下提供一些常用的方法
求离最近发表时间的函数public string DateStringFromNow(DateTime dt) { TimeSpan span = DateTime.Now - if (span.TotalDays & 60) { return dt.ToShortDateString(); } else if ( span.TotalDays & 30 ) { return "1个月前"; } else if (span.TotalDays & 14) { return "2周前"; } else if (span.TotalDays & 7) { return "1周前"; } else if (span.TotalDays & 1) { return string.Format("{0}天前", (int)Math.Floor(span.TotalDays)); } else if (span.TotalHours & 1) { return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours)); } else if (span.TotalMinutes & 1) { return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes)); } else if (span.TotalSeconds &= 1) { return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds)); }
else { return "1秒前"; } }C#中使用TimeSpan计算两个时间的差值可以反加两个日期之间任何一个时间单位。private string DateDiff(DateTime DateTime1, DateTime DateTime2){string dateDiff =TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);TimeSpan ts = ts1.Subtract(ts2).Duration();dateDiff = ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小时"+ ts.Minutes.ToString()+"分钟"+ ts.Seconds.ToString()+"秒";return dateD}
TimeSpan ts = Date1 - Date2;
double dDays = ts.TotalD//带小数的天数,比如1天12小时结果就是1.5
比如1天12小时结果就是1.5
int nDays = ts.D//整数天数,1天12小时或者1天20小时结果都是1
/// &summary&/// 日期比较/// &/summary&/// &param name="today"&当前日期&/param&/// &param name="writeDate"&输入日期&/param&/// &param name="n"&比较天数&/param&&returns&大于天数返回true,小于返回false&/returns&private bool CompareDate(string today, string writeDate, int n){DateTime Today = Convert.ToDateTime(today);DateTime WriteDate = Convert.ToDateTime(writeDate);WriteDate = WriteDate.AddDays(n);if (Today&= WriteDate)else}
在定义Date数据类型时候,必须注意下面三点:
1. Date数值必须以数字符号"#"括起来。
2. Date数值中的日期数据可有可无,如果有必须符合格式"m/d/yyyy"。
3. Date数值中的时间数据可有可无,如果有必须和日期数据通过空格分开,并且时分秒之间以":"分开。
一.DateTime和TimeSpan的关系和区别:
DateTime和TimeSpan是Visual Basic .Net中用以处理时间日期类型数据的二个主要的结构,这二者的区别在于,DatTime表示一个固定的时间,而TimeSpan表示的是一个时间间隔, 即一段时间。在下面介绍的程序示例中,TimeSpan就用以当前时间和给定时间之差。
二.DateTime和TimeSpan中的常用成员及其说明:
DateTime结构和TimeSpan结构提供了丰富的方法和属性,
属性 说明Date 获取此实例的日期部分。Day 获取此实例所表示的日期为该月中的第几天。DayOfWeek 获取此实例所表示的日期是星期几。DayOfYear 获取此实例所表示的日期是该年中的第几天。Hour 获取此实例所表示日期的小时部分。Millisecond 获取此实例所表示日期的毫秒部分。Minute 获取此实例所表示日期的分钟部分。Month 获取此实例所表示日期的月份部分。Now 创建一个DateTime实例,它是此计算机上的当前本地日期和时间。Second 获取此实例所表示日期的秒部分。TimeOfDay 获取此实例的当天的时间。Today 获取当前日期。Year 获取此实例所表示日期的年份部分。
Add 将指定的TimeSpan的值加到此实例的值上。AddDays 将指定的天数加到此实例的值上。AddHours 将指定的小时数加到此实例的值上。AddMilliseconds 将指定的毫秒数加到此实例的值上。AddMinutes 将指定的分钟数加到此实例的值上。AddMonths 将指定的月份数加到此实例的值上。AddSeconds 将指定的秒数加到此实例的值上。AddYears 将指定的年份数加到此实例的值上。DaysInMonth 返回指定年份中指定月份的天数。IsLeapYear 返回指定的年份是否为闰年的指示。Parse
将日期和时间的指定字符串表示转换成其等效的DateTime实例。Subtract 从此实例中减去指定的时间或持续时间。ToLongDateString 将此实例的值转换为其等效的长日期字符串表示形式。ToLongTimeString 将此实例的值转换为其等效的长时间字符串表示形式。ToShortTimeString
将此实例的值转换为其等效的短时间字符串表示形式。ToShortDateString 将此实例的值转换为其等效的短日期字符串表示形式。
阅读(...) 评论()6972人阅读
设计模式 and .net家族(c#、vb.net)(17)
& & 我们在编程中,通常都需要处理时间日期数据,这也是一种比较难于处理的数据类型,因为时间日期类型的结构相当复杂,并且还有一定的规则,如果在定义的时候不符合规则,程序在编译的时候就会出错。在Visual Basic .Net用以处理时间日期类型的结构(Structure)主要是2个,分别为DateTime和TimeSpan,这二个结构都位于命名空间System,并且在Visual
Basic .Net中自己也定义了一种数据类型Date,这种数据类型相当于上面提及的DateTime类。这时候可能很多朋友会问了,什么叫结构,它有什么作用?其实结构和类在作用和功能上是非常类似的,它也有构造函数,二者的主要区别如下:
  1. 结构不能包含显式的无参数构造函数。结构成员将自动初始化为它们的默认值。
  2. 结构不能有以下形式的初始值设定项:base。
  其实对于大多数朋友,你具体使用结构和类的时候,完全可以不需要考虑到二者区别,因为二者在使用上几乎相同。
  虽然Visual Basic .Net中提供了Date数据类型来处理时间日期类型,但在实际中往往并不使用它,因为它的定义非常麻烦,加上时间日期类型结构相当复杂,对于很多初学者往往会出现错误。下面就是在Visual Basic .Net中通过Date数据类型定义一个时间日期类型数据变量的语句,就可见其烦杂了:
Dim s As Date = #12/2/:00 PM#
  在定义Date数据类型时候,必须注意下面三点:
  1. Date数值必须以数字符号&#&括起来。
  2. Date数值中的日期数据可有可无,如果有必须符合格式&m/d/yyyy&。
  3. Date数值中的时间数据可有可无,如果有必须和日期数据通过空格分开,并且时分秒之间以&:&分开。
  一.DateTime和TimeSpan的关系和区别:
  DateTime和TimeSpan是Visual Basic .Net中用以处理时间日期类型数据的二个主要的结构,这二者的区别在于,DatTime表示一个固定的时间,而TimeSpan表示的是一个时间间隔,即一段时间。在下面介绍的程序示例中,TimeSpan就用以当前时间和给定时间之差。
  二.DateTime和TimeSpan中的常用成员及其说明:
  DateTime结构和TimeSpan结构提供了丰富的方法和属性,通过这些方法和属性,几乎可以直接处理任何时间日期类型数据。
表01和表02分别是DateTime结构的常用属性和常用方法及其说明:
获取此实例的日期部分。
获取此实例所表示的日期为该月中的第几天。
获取此实例所表示的日期是星期几。
获取此实例所表示的日期是该年中的第几天。
获取此实例所表示日期的小时部分。
millisecond
获取此实例所表示日期的毫秒部分。
获取此实例所表示日期的分钟部分。
获取此实例所表示日期的月份部分。
创建一个datetime实例,它是此计算机上的当前本地日期和时间。
获取此实例所表示日期的秒部分。
获取此实例的当天的时间。
获取当前日期。
获取此实例所表示日期的年份部分。
表01:datetime类的常用属性及其说明
将指定的timespan的值加到此实例的值上。
将指定的天数加到此实例的值上。
将指定的小时数加到此实例的值上。
addmilliseconds
将指定的毫秒数加到此实例的值上。
addminutes
将指定的分钟数加到此实例的值上。
将指定的月份数加到此实例的值上。
addseconds
将指定的秒数加到此实例的值上。
将指定的年份数加到此实例的值上。
daysinmonth
返回指定年份中指定月份的天数。
isleapyear
返回指定的年份是否为闰年的指示。
将日期和时间的指定字符串表示转换成其等效的datetime实例。
从此实例中减去指定的时间或持续时间。
tolongdatestring
将此实例的值转换为其等效的长日期字符串表示形式。
tolongtimestring
将此实例的值转换为其等效的长时间字符串表示形式。
toshorttimestring
将此实例的值转换为其等效的短时间字符串表示形式。
toshortdatestring
将此实例的值转换为其等效的短日期字符串表示形式。
表02:datetime结构的常用方法及其说明
表03和表04分别是timespan结构的常用属性和常用方法及其说明:
获取由此实例表示的整天数。
获取由此实例表示的整小时数。
milliseconds
获取由此实例表示的整毫秒数。
获取由此实例表示的整分钟数。
获取由此实例表示的整秒数。
获取用刻度表示的此实例的值。
获取以整天数和天的小数部分表示的此实例的值。
totalhours
获取以整小时数和小时的小数部分表示的此实例的值。
totalmilliseconds
获取以整毫秒数和毫秒的小数部分表示的此实例的值。
totalminutes
获取以整分钟数和分钟的小数部分表示的此实例的值。
totalseconds
获取以整秒数和秒的小数部分表示的此实例的值。
表03:timespan结构的常用属性及其说明
将指定的timespan添加到此实例中。
返回其值为此实例的绝对值的timespan。
返回表示指定天数的timespan,其中对天数的指定精确到最接近的毫秒。
返回表示指定小时数的timespan,其中对小时数的指定精确到最接近的毫秒。
frommilliseconds
返回表示指定毫秒数的timespan。
fromminutes
返回表示指定分钟数的timespan,其中对分钟数的指定精确到最接近的毫秒。
fromseconds
返回表示指定秒数的timespan,其中对秒数的指定精确到最接近的毫秒。
从此实例中减去指定的timespan。
表04:timespan结构的常用方法及其说明
=======================下面为实例========================
三.DateTime和TimeSpan中常用成员的使用方法及其使用技巧:
  在了解了DateTime和TimeSpan的常用方法和常用属性后,下面将通过一个示例来掌握上述方法和属性的用法。在下面的示例中将着重介绍下列问题的处理方法:
  1. 判断输入的日期时间字符串的合法性。
  2. DateTime实例之间的运算。
  3. 日期时间数据的获取方法。
  下面是用Visual Basic .Net实现上述功能的示例的主要步骤:
& & 1. 启动 Visual Studio .Net。
  2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。
  &3. 将【项目类型】设置为【Visual Basic项目】。
  4. 将【模板】设置为【Windows窗体应用程序】。
  5. 在【名称】文本框中输入【处理日期时间数据】。
  6. 在【位置】的文本框中输入【E:\VS.NET项目】,然后单击【确定】按钮,这样在&E:\VS.NET项目&目录中就产生了名称为&处理日期时间数据&的文件夹,并在里面创建了名称为【处理日期时间数据】的项目文件。
  7. 把Visual Studio .Net的当前窗口切换到【Form1.vb(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往Form1窗体中拖入下列组件,并执行相应操作:
  一个TabControl组件(包含三个TabPage组件.)
  十九个Label组件。
  十九个TextBox组件,用以显示时间日期数值。
  三个Button组件,并在这三个Button组件拖入Form1的设计窗体后,分别双击这三个组件,这样系统会在Form1.vb文件分别产生这三个组件的Click时间对应的处理代码。
  8. 按照图01、图02、图03设定设定组件的主要属性
& 图01:【处理日期时间数据】设计界面之一
图02:【处理日期时间数据】设计界面之二
图03:【处理日期时间数据】设计界面之三
在完成上面的工作以后,下面就进入程序的功能实现阶段。
& & 9. 判断输入的日期时间字符串的合法性。为了实现这个功能,首先要掌握把输入的日期时间字符串转换成可供Visual Basic .Net使用的日期时间类型的数据的方法。这个方法就是使用DateTime中的Parse方法,Parse方法能够把一个符合日期时间的字符串转换成一个DateTime实例。下面是一个具体的转换代码:
Dim dtTemp as System.DateTime = System.DateTime.Parse ( &12/2/:25&)
但如果给定要转换的字符串不合法,程序在执行的时候就会出现异常。程序通过对异常的捕获,来判断给定要转换的字符串合法性。在Visual Basic .Net中捕获异常一般使用的是Try …Catch ….End Try语句。这里要注意的是Try …Catch ….End Try语句是一个非常重要的语句,在后面章节中的很多关键代码部分都会经常的使用到它,通过它的确能够解决很多麻烦的问题。
下面是在本程序中实现这个功能的具体操作步骤:
  首先把Visual Studio .Net的当前窗口切换到【Form1.vb】,进入Form1.vb文件的编辑界面。然后用下列的代码替换Form1.vb中btnJudge组件的Click事件对应的处理代码。
'判断输入日期时间字符串合法性
Private Sub btnJudge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJudge.Click
Dim dtMyDate As System.DateTime
dtMyDate = DateTime.Parse(txtDateTime.Text) '转换给定的日期时间字符串
MessageBox.Show(&你输入的时间日期字符串不合法!&, &错误!&)
txtDateTime.Text = &&
获取计算机日期时间数据。程序要实现这个功能非常简单,只需要掌握表01和表02中列出的DateTime常用属性、方法的使用方法就能够方便完成了。具体到本程序具体的操作是用下列代码替换Form1.vb中btnDateTimeNow的Click事件对应的处理代码:
'获得计算机日期、时间
Private Sub btnDateTimeNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDateTimeNow.Click
'创建实例,此实例存放当前日期和时间
Dim dMyDate As DateTime = DateTime.Now
'显示当前日期和时间
txtDateTimeNow.Text = dMyDate.ToString()
'显示当前日期
txtDateNow.Text = dMyDate.Date
'显示当前年度
txtYear.Text = dMyDate.Year
'显示当前月份
txtMonth.Text = dMyDate.Month
txtDay.Text = dMyDate.Day
'显示当前天是一年中的第多少天
txtDayofyear.Text = dMyDate.DayOfYear
'显示当前天是本星期中的第多少天
txtWeek.Text = dMyDate.DayOfWeek
'以长日期形式来显示日期
txtDateLong.Text = dMyDate.ToLongDateString
'以短日期形式来显示日期
txtDateShort.Text = dMyDate.ToShortDateString
'显示当前时间
txtTimeNow.Text = dMyDate.TimeOfDay.ToString()
'显示当前时间的小时
txtHour.Text = dMyDate.Hour
'显示当前时间的分钟
txtMinute.Text = dMyDate.Minute
'显示当前时间的秒
txtSecond.Text = dMyDate.Second
'显示当前时间的毫秒
txtMillisecond.Text = dMyDate.Millisecond
'以长时间形式来显示当前时间
txtTimeLong.Text = dMyDate.ToLongTimeString
'以短时间形式来显示当前时间
txtTimeShort.Text = dMyDate.ToShortTimeString
& & 11. 日期时间数据的运算。在下面介绍的代码是实现二个DateTime实例之差,即当前的时间和给定的时间之差。解决的步骤是首先判断给定的日期时间字符串的合法性,如果不合法,则返回。如果合法,则以此来创建DateTime实例dtMyDate。然后dtMyDate调用其Subtract方法减去当前的时间,并存放到TimeSpan实例tsTemp。tsTemp调用其Duration方法把二者之差的绝对值显示出来。具体到程序中的实现方法是用下列代码替换Form1.vb中的btnOK的Click事件的处理代码:
'输入时间与系统时间之差
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim tsTemp As TimeSpan
Dim dtMydate As DateTime
'判断输入的时间日期字符串的合法性
dtMydate = DateTime.Parse(txtDateTimeEnter.Text)
MessageBox.Show(&输入的时间日期字符串不合法&, &错误!&)
'两个DateTime实例相减
tsTemp = dtMydate.Subtract(DateTime.Now)
'取两个各DateTime之差绝对值,并显示出来
txtTimeDifference.Text = tsTemp.Duration.ToString()
& & 至此在上述步骤都正确执行,并成功保存后,【处理日期时间数据】项目的全部工作就完成了。图04、图05和图06分别是【处理日期时间数据】项目编译后的运行界面:
图04:【处理日期时间数据】运行界面之一
图05:【处理日期时间数据】运行界面之二
图06:【处理日期时间数据】运行界面之三
四.小结:
  本文介绍在Visual Basic .Net中日期时间类型数据的处理方法及在处理这些数据时的一些注意事项和技巧。以下就是本文中的知识点:
  1. 结构和类的区别。
  2. 用Date变量定义日期时间,及其注意事项。
  3. DateTime和TimeSpan的主要区别,和二者主要的成员及其简单说明。
  4. 判断给定时间日期的合法性。
  5. 日期时间类型数据(DateTime实例)的运算。
  6. 日期时间及其相关数值的获取。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1184120次
积分:14547
积分:14547
排名:第569名
原创:167篇
评论:2912条
(4)(4)(4)(4)(4)(4)(4)(4)(4)(4)(4)(4)(4)(6)(4)(4)(4)(4)(4)(4)(4)(5)(8)(7)(5)(3)(9)(4)(5)(4)(7)(4)(3)(3)(6)(3)(5)(3)(1)(1)POCO C++库学习和分析 -- 日期与时间
POCO C++库学习和分析 -- 日期与时间
&&&&&&& 在Poco库中,与时间和日期相关的一些类,其内部实现是非常简单的。看相关文档时,比较有意思的倒是历史上的不同时间表示法。
1. 系统时间函数
&&&&&&& 在编程时,时间函数不可避免的会被使用。linux系统下相关时间的数据结构有time_t,timeval,timespec,tm,clock_t; windows下time_t,tm,SYSTEMTIME,clock_t。其中clock_t、timeval、timespec用于表示时间跨度,time_t、tm、SYSTEMTIME用于表示绝对时间。不同的数据结构之间,多少也有些差异。
&&&&&& 首先 这些时间结构体的精度不同,Second(time_t/tm), microsecond(timeval/SYSTEMTIME),& nanoSeconds(timespec)。
&&&&&&& 起始时间不同,time_t起始于日0时0分0秒,tm表示起始于1900年,SYSTEMTIME起始于1601年,clock起始于机器开机。
&&&&&&& 同这些数据结构相关联,C语言为tm,time_t提供了一组函数用于时间运算和数据结构转换:
// 日历时间(一个用time_t表示的整数)&&
// 比较日历时间&&
double difftime(time_t time1, time_t time0);&
// 获取日历时间&&
time_t time(time_t * timer);&
// 转换日历时间为字符串&&
char * ctime(const time_t *timer);&
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(GMT timezone)&&
struct tm * gmtime(const time_t *timer);&&&&&&&
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(本地 timezone)&&
struct tm * localtime(const time_t * timer);&
// 关于本地时间的计算公式:&&
localtime = utctime[Gmt time] + utcOffset()[时区偏移] + dst()[夏令时偏移]&
// 把tm转换为字符串&&
char * asctime(const struct tm * timeptr);&
// 把tm转换为日历时间&&
time_t mktime(struct tm * timeptr);&
// 获取开机以来的微秒数&&
clock_t clock (void);&
// 日历时间(一个用time_t表示的整数)
// 比较日历时间
double difftime(time_t time1, time_t time0);
// 获取日历时间
time_t time(time_t * timer);
// 转换日历时间为字符串
char * ctime(const time_t *timer);
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(GMT timezone)
struct tm * gmtime(const time_t *timer);&&&&&
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(本地 timezone)
struct tm * localtime(const time_t * timer);
// 关于本地时间的计算公式:
localtime = utctime[Gmt time] + utcOffset()[时区偏移] + dst()[夏令时偏移]
// 把tm转换为字符串
char * asctime(const struct tm * timeptr);
// 把tm转换为日历时间
time_t mktime(struct tm * timeptr);
// 获取开机以来的微秒数
clock_t clock (void);
&&&&&&& 我们回想一下程序中的时间数据结构和函数的用法,可以发现主要是2个目的:
&&&&&&& 1. 获取绝对时间
&&&&&&& 2. 获取两个时间点的相对时间
2. Timestamp类
&&&&&&& 同C语言中函数类似,Poco中定义了自己的时间类。Timestamp类似于time_t,用于获取比较日历时间。Timestamp定义如下:
class Foundation_API Timestamp&
&&& typedef Int64 TimeV&&& /// monotonic UTC time value in microsecond resolution&&
&&& typedef Int64 UtcTimeV /// monotonic UTC time value in 100 nanosecond resolution&&
&&& typedef Int64 TimeD&& /// difference between two timestamps in microseconds&&
&&& Timestamp();&
&&&&&&& /// Creates a timestamp with the current time.&&
&&& Timestamp(TimeVal tv);&
&&&&&&& /// Creates a timestamp from the given time value.&&
&&& Timestamp(const Timestamp& other);&
&&&&&&& /// Copy constructor.&&
&&& ~Timestamp();&
&&&&&&& /// Destroys the timestamp&&
&&& Timestamp& operator = (const Timestamp& other);&
&&& Timestamp& operator = (TimeVal tv);&
&&& void swap(Timestamp& timestamp);&
&&&&&&& /// Swaps the Timestamp with another one.&&
&&& void update();&
&&&&&&& /// Updates the Timestamp with the current time.&&
&&& bool operator == (const Timestamp& ts)&
&&& bool operator != (const Timestamp& ts)&
&&& bool operator && (const Timestamp& ts)&
&&& bool operator &= (const Timestamp& ts)&
&&& bool operator && (const Timestamp& ts)&
&&& bool operator &= (const Timestamp& ts)&
&&& Timestamp& operator +& (TimeDiff d)&
&&& Timestamp& operator -& (TimeDiff d)&
&&& TimeDiff&& operator -& (const Timestamp& ts)&
&&& Timestamp& operator += (TimeDiff d);&
&&& Timestamp& operator -= (TimeDiff d);&
&&& std::time_t epochTime()&
&&&&&&& /// Returns the timestamp expressed in time_t.&&
&&&&&&& /// time_t base time is midnight, January 1, 1970.&&
&&&&&&& /// Resolution is one second.&&
&&& UtcTimeVal utcTime()&
&&&&&&& /// Returns the timestamp expressed in UTC-based&&
&&&&&&& /// time. UTC base time is midnight, October 15, 1582.&&
&&&&&&& /// Resolution is 100 nanoseconds.&&
&&& TimeVal epochMicroseconds()&
&&&&&&& /// Returns the timestamp expressed in microseconds&&
&&&&&&& /// since the Unix epoch, midnight, January 1, 1970.&&
&&& TimeDiff elapsed()&
&&&&&&& /// Returns the time elapsed since the time denoted by&&
&&&&&&& /// the timestamp. Equivalent to Timestamp() - *this.&&
&&& bool isElapsed(TimeDiff interval)&
&&&&&&& /// Returns true iff the given interval has passed&&
&&&&&&& /// since the time denoted by the timestamp.&&
&&& static Timestamp fromEpochTime(std::time_t t);&
&&&&&&& /// Creates a timestamp from a std::time_t.&&
&&& static Timestamp fromUtcTime(UtcTimeVal val);&
&&&&&&& /// Creates a timestamp from a UTC time value.&&
&&& static TimeVal resolution();&
&&&&&&& /// Returns the resolution in units per second.&&
&&&&&&& /// Since the timestamp has microsecond resolution,&&
&&&&&&& /// the returned value is always 1000000.&&
&&& TimeVal _&
class Foundation_API Timestamp
&typedef Int64 TimeV&&& /// monotonic UTC time value in microsecond resolution
&typedef Int64 UtcTimeV /// monotonic UTC time value in 100 nanosecond resolution
&typedef Int64 TimeD&& /// difference between two timestamps in microseconds
&Timestamp();
&&/// Creates a timestamp with the current time.
&Timestamp(TimeVal tv);
&&/// Creates a timestamp from the given time value.
&Timestamp(const Timestamp& other);
&&/// Copy constructor.
&~Timestamp();
&&/// Destroys the timestamp
&Timestamp& operator = (const Timestamp& other);
&Timestamp& operator = (TimeVal tv);
&void swap(Timestamp& timestamp);
&&/// Swaps the Timestamp with another one.
&void update();
&&/// Updates the Timestamp with the current time.
&bool operator == (const Timestamp& ts)
&bool operator != (const Timestamp& ts)
&bool operator && (const Timestamp& ts)
&bool operator &= (const Timestamp& ts)
&bool operator && (const Timestamp& ts)
&bool operator &= (const Timestamp& ts)
&Timestamp& operator +& (TimeDiff d)
&Timestamp& operator -& (TimeDiff d)
&TimeDiff&& operator -& (const Timestamp& ts)
&Timestamp& operator += (TimeDiff d);
&Timestamp& operator -= (TimeDiff d);
&std::time_t epochTime()
&&/// Returns the timestamp expressed in time_t.
&&/// time_t base time is midnight, January 1, 1970.
&&/// Resolution is one second.
&UtcTimeVal utcTime()
&&/// Returns the timestamp expressed in UTC-based
&&/// time. UTC base time is midnight, October 15, 1582.
&&/// Resolution is 100 nanoseconds.
&TimeVal epochMicroseconds()
&&/// Returns the timestamp expressed in microseconds
&&/// since the Unix epoch, midnight, January 1, 1970.
&TimeDiff elapsed()
&&/// Returns the time elapsed since the time denoted by
&&/// the timestamp. Equivalent to Timestamp() - *this.
&bool isElapsed(TimeDiff interval)
&&/// Returns true iff the given interval has passed
&&/// since the time denoted by the timestamp.
&static Timestamp fromEpochTime(std::time_t t);
&&/// Creates a timestamp from a std::time_t.
&static Timestamp fromUtcTime(UtcTimeVal val);
&&/// Creates a timestamp from a UTC time value.
&static TimeVal resolution();
&&/// Returns the resolution in units per second.
&&/// Since the timestamp has microsecond resolution,
&&/// the returned value is always 1000000.
&TimeVal _
&&&&&&& Timestamp内部定义了一个Int64的变量_ts。存储了一个基于utc时间的64位int值,理论上可以提供微秒级的精度(实际精度依赖于操作系统)。由于Poco::Timestamp是基于UTC(世界标准时间或世界協調時間)的,所以它是独立于时区设置的。Poco::Timestamp实现了值语义,比较和简单的算术操作。
&&&&&&& 1. UTC (Coordinated Universal Time)是从日深夜开始计时的. Poco库中精度为100纳秒。
&&&&&&& 2. epoch time指是从日深夜开始计时的(指unix诞生元年)。Poco库中精度为1秒。
&&&&&&& 数据类型:
&&&&&&& Poco::Timestamp内部定义了下列数据类型:
&&&&&&& 1. TimeVal
&&&&&&&&& 一个64位的int整数值,保存utc时间,精度微秒
&&&&&&& 2. UtcTimeVal
&&&&&&&& 一个64位的int整数值,保存utc时间,精度100纳秒(真实精度仍然是微秒)
&&&&&&& 3. TimeDiff
&&&&&&& 一个64位的int整数值,保存两个Timestamp的差值,精度微秒
&&&&&&& 构造函数:
&&&&&&& 1. 默认构造函数会以当前时间初始化一个Timestamp值,基于UTC时间(从日开始计时,精度为100纳秒)
&&&&&&& 2. 提供了两个静态函数用于创建Timestamp对象,
&&&&&&&&&&&&&&&&&& a) Timestamp fromEpochTime(time_t time)。这个函数从time_t构建,内部会把EpochTime(从日深夜开始计时的,精度为1秒)的时间转换成为UTC时间。
&&&&&&&&&&&&&&&&&& b) Timestamp fromUtcTime(UtcTimeVal val)。这个函数从一个UtcTimeVal构建。
&&&&&&& Timestamp的成员函数:
&&&&&&& 1. time_t epochTime() const
&&&&&&& 返回一个以epoch time计算的日历时间(精度秒)。(函数内部会把基于UTC时间的值转为基于epoch time的值)
&&&&&&& 2. UtcTimeVal utcTime() const
&&&&&&& 返回一个以UTC时间计算的日历时间(精度100纳秒)。
&&&&&&& 3. TimeVal epochMicroseconds() const
&&&&&&& 返回一个以epoch time计算的日历时间(精度微秒)
&&&&&&& 4. void update()
&&&&&&& 取当前的时间更新
&&&&&&& 5. TimeDiff elapsed() const
&&&&&&& 返回当前时间与Timestamp内部时间_ts的一个时间差值(精度微秒)
&&&&&&& 6. bool isElapsed(TimeDiff interval) const
&&&&&&& 如果当前时间与Timestamp内部时间_ts的一个时间差值大于interval时间,返回true。(精度微秒)
&&&&&&& Timestamp算术计算:
&&&&&&& 1. Timestamp operator + (TimeDiff diff) const
&&&&&&& 增加一个时间偏移,并返回值。(精度微秒)
&&&&&&& 2. Timestamp operator - (TimeDiff diff) const
&&&&&&& 减掉一个时间偏移,并返回值。(精度微秒)
&&&&&&& 3. TimeDiff operator - (const Timestamp& ts) const
&&&&&&& 返回两个Timestamp对象的时间偏移。(精度微秒)
&&&&&&& 4. Timestamp& operator += (TimeDiff d)
&&&&&&&&&&& Timestamp& operator -= (TimeDiff d)
&&&&&&& 增加或减小一个时间偏移值
&&&&&&& 下面来看一个例子:
#include &Poco/Timestamp.h&&&
#include &ctime&&&
using Poco::T&
int main(int argc, char** argv)&
&&& T // the current date and time&&
&&& std::time_t t1 = now.epochTime(); // convert to time_t ...&&
&&& Timestamp ts1(Timestamp::fromEpochTime(t1)); // ... and back again&&
&&& for (int i = 0; i & 100000; ++i) ; // wait a bit&&
&&& Timestamp::TimeDiff diff = now.elapsed(); // how long did it take?&&
&&& Timestamp start(now); // save start time&&
&&& now.update(); // update with current&&
&&& time diff = now - // again, how long?&&
&&& return 0;&
#include &Poco/Timestamp.h&
#include &ctime&
using Poco::T
int main(int argc, char** argv)
&T // the current date and time
&std::time_t t1 = now.epochTime(); // convert to time_t ...
&Timestamp ts1(Timestamp::fromEpochTime(t1)); // ... and back again
&for (int i = 0; i & 100000; ++i) ; // wait a bit
&Timestamp::TimeDiff diff = now.elapsed(); // how long did it take?
&Timestamp start(now); // save start time
&now.update(); // update with current
&time diff = now - // again, how long?
&return 0;
3. DateTime类
&&&&&&& Poco中提供了DateTime类,作用和tm类似。下面是它的定义:
class Foundation_API DateTime&
&&& enum Months&
&&&&&&& /// Symbolic names for month numbers (1 to 12).&&
&&&&&&& JANUARY = 1,&
&&&&&&& FEBRUARY,&
&&&&&&& MARCH,&
&&&&&&& APRIL,&
&&&&&&& MAY,&
&&&&&&& JUNE,&
&&&&&&& JULY,&
&&&&&&& AUGUST,&
&&&&&&& SEPTEMBER,&
&&&&&&& OCTOBER,&
&&&&&&& NOVEMBER,&
&&&&&&& DECEMBER&
&&& enum DaysOfWeek&
&&&&&&& /// Symbolic names for week day numbers (0 to 6).&&
&&&&&&& SUNDAY = 0,&
&&&&&&& MONDAY,&
&&&&&&& TUESDAY,&
&&&&&&& WEDNESDAY,&
&&&&&&& THURSDAY,&
&&&&&&& FRIDAY,&
&&&&&&& SATURDAY&
&&& DateTime();&
&&&&&&& /// Creates a DateTime for the current date and time.&&
&&& DateTime(const Timestamp& timestamp);&
&&&&&&& /// Creates a DateTime for the date and time given in&&
&&&&&&& /// a Timestamp.&&
&&& DateTime(int year, int month, int day, int hour = 0, int minute = 0, int&&
second = 0, int millisecond = 0, int microsecond = 0);&
&&&&&&& /// Creates a DateTime for the given Gregorian date and time.&&
&&&&&&& ///&& * year is from 0 to 9999.&&
&&&&&&& ///&& * month is from 1 to 12.&&
&&&&&&& ///&& * day is from 1 to 31.&&
&&&&&&& ///&& * hour is from 0 to 23.&&
&&&&&&& ///&& * minute is from 0 to 59.&&
&&&&&&& ///&& * second is from 0 to 59.&&
&&&&&&& ///&& * millisecond is from 0 to 999.&&
&&&&&&& ///&& * microsecond is from 0 to 999.&&
&&& DateTime(double julianDay);&
&&&&&&& /// Creates a DateTime for the given Julian day.&&
&&& DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);&
&&&&&&& /// Creates a DateTime from an UtcTimeVal and a TimeDiff.&&
&&&&&&& ///&&
&&&&&&& /// Mainly used internally by DateTime and friends.&&
&&& DateTime(const DateTime& dateTime);&
&&&&&&& /// Copy constructor. Creates the DateTime from another one.&&
&&& ~DateTime();&
&&&&&&& /// Destroys the DateTime.&&
&&& DateTime& operator = (const DateTime& dateTime);&
&&&&&&& /// Assigns another DateTime.&&
&&& DateTime& operator = (const Timestamp& timestamp);&
&&&&&&& /// Assigns a Timestamp.&&
&&& DateTime& operator = (double julianDay);&
&&&&&&& /// Assigns a Julian day.&&
&&& DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0,&&
int second = 0, int millisecond = 0, int microseconds = 0);&
&&&&&&& /// Assigns a Gregorian date and time.&&
&&&&&&& ///&& * year is from 0 to 9999.&&
&&&&&&& ///&& * month is from 1 to 12.&&
&&&&&&& ///&& * day is from 1 to 31.&&
&&&&&&& ///&& * hour is from 0 to 23.&&
&&&&&&& ///&& * minute is from 0 to 59.&&
&&&&&&& ///&& * second is from 0 to 59.&&
&&&&&&& ///&& * millisecond is from 0 to 999.&&
&&&&&&& ///&& * microsecond is from 0 to 999.&&
&&& void swap(DateTime& dateTime);&
&&&&&&& /// Swaps the DateTime with another one.&&
&&& int year()&
&&&&&&& /// Returns the year.&&
&&& int month()&
&&&&&&& /// Returns the month (1 to 12).&&
&&& int week(int firstDayOfWeek = MONDAY)&
&&&&&&& /// Returns the week number within the year.&&
&&&&&&& /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).&&
&&&&&&& /// The returned week number will be from 0 to 53. Week number 1 is&&&
the week&&
&&&&&&& /// containing January 4. This is in accordance to ISO 8601.&&
&&&&&&& ///&&&
&&&&&&& /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started&&
&&&&&&& /// on a Saturday, week 1 will be the week starting on Monday, January 3.&&
&&&&&&& /// January 1 and 2 will fall within week 0 (or the last week of the previous year).&&
&&&&&&& ///&&
&&&&&&& /// For 2007, which starts on a Monday, week 1 will be the week&&&
startung on Monday, January 1.&
&&&&&&& /// There will be no week 0 in 2007.&&
&&& int day()&
&&&&&&& /// Returns the day witin the month (1 to 31).&&
&&& int dayOfWeek()&
&&&&&&& /// Returns the weekday (0 to 6, where&&
&&&&&&& /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).&&
&&& int dayOfYear()&
&&&&&&& /// Returns the number of the day in the year.&&
&&&&&&& /// January 1 is 1, February 1 is 32, etc.&&
&&& int hour()&
&&&&&&& /// Returns the hour (0 to 23).&&
&&& int hourAMPM()&
&&&&&&& /// Returns the hour (0 to 12).&&
&&& bool isAM()&
&&&&&&& /// Returns true if hour & 12;&&
&&& bool isPM()&
&&&&&&& /// Returns true if hour &= 12.&&
&&& int minute()&
&&&&&&& /// Returns the minute (0 to 59).&&
&&& int second()&
&&&&&&& /// Returns the second (0 to 59).&&
&&& int millisecond()&
&&&&&&& /// Returns the millisecond (0 to 999)&&
&&& int microsecond()&
&&&&&&& /// Returns the microsecond (0 to 999)&&
&&& double julianDay()&
&&&&&&& /// Returns the julian day for the date and time.&&
&&& Timestamp timestamp()&
&&&&&&& /// Returns the date and time expressed as a Timestamp.&&
&&& Timestamp::UtcTimeVal utcTime()&
&&&&&&& /// Returns the date and time expressed in UTC-based&&
&&&&&&& /// time. UTC base time is midnight, October 15, 1582.&&
&&&&&&& /// Resolution is 100 nanoseconds.&&
&&& bool operator == (const DateTime& dateTime)&&&
&&& bool operator != (const DateTime& dateTime)&&&
&&& bool operator && (const DateTime& dateTime)&&&&
&&& bool operator &= (const DateTime& dateTime)&&&&
&&& bool operator && (const DateTime& dateTime)&&&&
&&& bool operator &= (const DateTime& dateTime)&&&&
&&& DateTime& operator +& (const Timespan& span)&
&&& DateTime& operator -& (const Timespan& span)&
&&& Timespan& operator -& (const DateTime& dateTime)&
&&& DateTime& operator += (const Timespan& span);&
&&& DateTime& operator -= (const Timespan& span);&
&&& void makeUTC(int tzd);&
&&&&&&& /// Converts a local time into UTC, by applying the given time zone&&&
differential.&
&&& void makeLocal(int tzd);&
&&&&&&& /// Converts a UTC time into a local time, by applying the given time&&&
zone differential.&
&&& static bool isLeapYear(int year);&
&&&&&&& /// Returns true if the given&&
&&&&&&& /// false otherwise.&&
&&& static int daysOfMonth(int year, int month);&
&&&&&&& /// Returns the number of days in the given month&&
&&&&&&& /// and year. Month is from 1 to 12.&&
&&& static bool isValid(int year, int month, int day, int hour = 0, int minute =&&
0, int second = 0, int millisecond = 0, int microsecond = 0);&
&&&&&&& /// Checks if the given date and time is valid&&
&&&&&&& /// (all arguments are within a proper range).&&
&&&&&&& ///&&
&&&&&&& /// Returns true if all arguments are valid, false otherwise.&&
protected:&&&
&&& // ...&&
&&& // ...&&
&&& Timestamp::UtcTimeVal _utcT&
&&& short& _&
&&& short& _&
&&& short& _&
&&& short& _&
&&& short& _&
&&& short& _&
&&& short& _&
&&& short& _&
class Foundation_API DateTime
&enum Months
&&/// Symbolic names for month numbers (1 to 12).
&&JANUARY = 1,
&&FEBRUARY,
&&SEPTEMBER,
&&OCTOBER,
&&NOVEMBER,
&&DECEMBER
&enum DaysOfWeek
&&/// Symbolic names for week day numbers (0 to 6).
&&SUNDAY = 0,
&&TUESDAY,
&&WEDNESDAY,
&&THURSDAY,
&&SATURDAY
&DateTime();
&&/// Creates a DateTime for the current date and time.
&DateTime(const Timestamp& timestamp);
&&/// Creates a DateTime for the date and time given in
&&/// a Timestamp.
&DateTime(int year, int month, int day, int hour = 0, int minute = 0, int
second = 0, int millisecond = 0, int microsecond = 0);
&&/// Creates a DateTime for the given Gregorian date and time.
&&///&& * year is from 0 to 9999.
&&///&& * month is from 1 to 12.
&&///&& * day is from 1 to 31.
&&///&& * hour is from 0 to 23.
&&///&& * minute is from 0 to 59.
&&///&& * second is from 0 to 59.
&&///&& * millisecond is from 0 to 999.
&&///&& * microsecond is from 0 to 999.
&DateTime(double julianDay);
&&/// Creates a DateTime for the given Julian day.
&DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);
&&/// Creates a DateTime from an UtcTimeVal and a TimeDiff.
&&/// Mainly used internally by DateTime and friends.
&DateTime(const DateTime& dateTime);
&&/// Copy constructor. Creates the DateTime from another one.
&~DateTime();
&&/// Destroys the DateTime.
&DateTime& operator = (const DateTime& dateTime);
&&/// Assigns another DateTime.
&DateTime& operator = (const Timestamp& timestamp);
&&/// Assigns a Timestamp.
&DateTime& operator = (double julianDay);
&&/// Assigns a Julian day.
&DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0,
int second = 0, int millisecond = 0, int microseconds = 0);
&&/// Assigns a Gregorian date and time.
&&///&& * year is from 0 to 9999.
&&///&& * month is from 1 to 12.
&&///&& * day is from 1 to 31.
&&///&& * hour is from 0 to 23.
&&///&& * minute is from 0 to 59.
&&///&& * second is from 0 to 59.
&&///&& * millisecond is from 0 to 999.
&&///&& * microsecond is from 0 to 999.
&void swap(DateTime& dateTime);
&&/// Swaps the DateTime with another one.
&int year()
&&/// Returns the year.
&int month()
&&/// Returns the month (1 to 12).
&int week(int firstDayOfWeek = MONDAY)
&&/// Returns the week number within the year.
&&/// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
&&/// The returned week number will be from 0 to 53. Week number 1 is
&&/// containing January 4. This is in accordance to ISO 8601.
&&/// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
&&/// on a Saturday, week 1 will be the week starting on Monday, January 3.
&&/// January 1 and 2 will fall within week 0 (or the last week of the previous year).
&&/// For 2007, which starts on a Monday, week 1 will be the week
startung on Monday, January 1.
&&/// There will be no week 0 in 2007.
&int day()
&&/// Returns the day witin the month (1 to 31).
&int dayOfWeek()
&&/// Returns the weekday (0 to 6, where
&&/// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
&int dayOfYear()
&&/// Returns the number of the day in the year.
&&/// January 1 is 1, February 1 is 32, etc.
&int hour()
&&/// Returns the hour (0 to 23).
&int hourAMPM()
&&/// Returns the hour (0 to 12).
&bool isAM()
&&/// Returns true if hour & 12;
&bool isPM()
&&/// Returns true if hour &= 12.
&int minute()
&&/// Returns the minute (0 to 59).
&int second()
&&/// Returns the second (0 to 59).
&int millisecond()
&&/// Returns the millisecond (0 to 999)
&int microsecond()
&&/// Returns the microsecond (0 to 999)
&double julianDay()
&&/// Returns the julian day for the date and time.
&Timestamp timestamp()
&&/// Returns the date and time expressed as a Timestamp.
&Timestamp::UtcTimeVal utcTime()
&&/// Returns the date and time expressed in UTC-based
&&/// time. UTC base time is midnight, October 15, 1582.
&&/// Resolution is 100 nanoseconds.
&bool operator == (const DateTime& dateTime)&
&bool operator != (const DateTime& dateTime)&
&bool operator && (const DateTime& dateTime)&
&bool operator &= (const DateTime& dateTime)&
&bool operator && (const DateTime& dateTime)&
&bool operator &= (const DateTime& dateTime)&
&DateTime& operator +& (const Timespan& span)
&DateTime& operator -& (const Timespan& span)
&Timespan& operator -& (const DateTime& dateTime)
&DateTime& operator += (const Timespan& span);
&DateTime& operator -= (const Timespan& span);
&void makeUTC(int tzd);
&&/// Converts a local time into UTC, by applying the given time zone
differential.
&void makeLocal(int tzd);
&&/// Converts a UTC time into a local time, by applying the given time
zone differential.
&static bool isLeapYear(int year);
&&/// Returns true if the given
&&/// false otherwise.
&static int daysOfMonth(int year, int month);
&&/// Returns the number of days in the given month
&&/// and year. Month is from 1 to 12.
&static bool isValid(int year, int month, int day, int hour = 0, int minute =
0, int second = 0, int millisecond = 0, int microsecond = 0);
&&/// Checks if the given date and time is valid
&&/// (all arguments are within a proper range).
&&/// Returns true if all arguments are valid, false otherwise.
protected:&
&Timestamp::UtcTimeVal _utcT
&&&&&&& Poco::DateTime是基于格里高利历(Gregorian calendar)(就是公历啦)设计的。它除了可以用来保存日历时间外,还可以被用于日期计算。如果只是为了日历时间的存储,Timestamp类更加适合。在DateTime的内部,DateTime类用两种格式维护了日期和时间。第一种是UTC日历时间。第二种是用年、月、日、时、分、秒、微秒、毫秒。为了进行内部时间日期之间的换算,DateTime使用了儒略日(Julian day)历法。
&&&&&&& 格里高利历(Gregorian calendar)
&&&&&&& 格里高利历就是我们通常讲的公历。它以耶稣的诞生为初始年份,也就是公元0001年。格里高利历以日为基本单位,1年有365或366天,分成12个月,每个月时长不等。由于不同国家采用格里高利历时间不同(德国1582, 英国1752),所以格里高利历日期和旧式的日期有差别,即使是用来表示历史上相同的一件事。一个耶稣像,^_^。
&&&&& _&&&&& xxxx&&&&& _
&&&& /_;-.__ / _\& _.-;_\
&&&&&&& `-._`'`_/'`.-'
&&&&&&&&&&& `\&& /`
&&&&&&&&&&&& |& /
&&&&&&&&&&& /-.(
&&&&&&&&&&& \_._\
&&&&&&&&&&&& \ \`;
&&&&&&&&&&&&& & |/
&&&&&&&&&&&& / //
&&&&&&&&&&&& |//
&&&&&&&&&&&& \(\
&&&&&&& 儒略日和儒略日日期
&&&&&&& 儒略日的起点订在公元前4713年(天文学上记为 -4712年)1月1日格林威治时间平午(世界时12:00),即JD 0指定为UT时间B.C.日12:00到UC时间B.C.日12:00的24小时。注意这一天是礼拜一。每一天赋予了一个唯一的数字,顺数而下,如:日12:00:00的儒略日是2450084。这个日期是考虑了太阳、月亮的轨道运行周期,以及当时收税的间隔而订出来的。Joseph Scliger定义儒略周期为7980年,是因28、19、15的最小公倍数为28&19&15=7980。
&&&&&&& 日期的注意事项:
&&&&&&& 1. 0是一个合法数字(根据ISO 8691和天文年编号)
&&&&&&& 2. 0年是一个闰年。
&&&&&&& 3. 负数是不支持的。比如说公元前1年。
&&&&&&& 4. 格里高利历同历史上的日期可能不同,由于它们采用的日历方式不同。
&&&&&&& 5. 最好只是用DateTime用来计算当前的时间。对于历史上的或者天文日历的时间计算,还是使用其他的特定软件。
&&&&&&& 构造DateTime:
&&&&&&& 1. 可以从一个已有的DateTime构造
&&&&&&& 2. 当前的时间和日期
&&&&&&& 3. 一个Timestamp对象
&&&&&&& 4. 用年、月、日、时、分、秒、微秒、毫秒构造
&&&&&&& 5. 使用一个儒略日日期构造(用double形式保存)
&&&&&&& 成员函数:
&&&&&&& 1. int year() const
&&&&&&&&&& 返回年
&&&&&&& 2. int month() const
&&&&&&&&&& 返回月(1-12)
&&&&&&& 3. int week(int firstDayOfWeek = DateTime::MONDAY) const
&&&&&&&&&& 返回所在的周,根据ISO 8601标准(第一周是1月4日所在的周),一周的第一天是礼拜一或者礼拜天。
&&&&&&& 4. int day() const
&&&&&&&&&& 返回月中的所在天(1 - 31)
&&&&&&& 5. int dayOfWeek() const
&&&&&&&&&& 返回周中的所在天 0为周日,1为周一,.....
&&&&&&& 6. int dayOfYear() const
&&&&&&&&&& 返回年中的所在天(1 - 366)
&&&&&&& 7. int hour() const
&&&&&&&&&& 返回天中所在小时(0 - 23)
&&&&&&& 8. int hourAMPM() const
&&&&&&&&&& 返回上下午所在小时(0 - 12)
&&&&&&& 9. bool isAM() const
&&&&&&&&&& 如果上午返回真
&&&&&&& 10. bool isPM() const
&&&&&&&&&& 如果下午返回真
&&&&&&& 11. int minute() const
&&&&&&&&&& 返回分钟数(0 - 59)
&&&&&&& 12. int second() const
&&&&&&&&&& 返回秒数(0 - 59)
&&&&&&& 13. int millisecond() const
&&&&&&&&&& 返回毫秒数(0 - 999)
&&&&&&& 14. int microsecond() const
&&&&&&&&&& 返回微秒数(0 - 999)
&&&&&&& 15. Timestamp timestamp() const
&&&&&&&&&& 返回用Timestamp保存的日历时间(精度微秒)
&&&&&&& 16. Timestamp::UtcTimeVal utcTime() const
&&&&&&&&&& 返回用Timestamp保存的日历时间(精度100纳秒)
&&&&&&& 17. DateTime支持关系运算符(==, !=, &, &=, &, &=).
&&&&&&& 18. DateTime支持算术操作(+, -, +=, -=)
&&&&&&& 静态函数:
&&&&&&& 1. bool isLeapYear(int year)
&&&&&&&&&& 所给年是否闰年
&&&&&&& 2. int daysOfMonth(int year, int month)
&&&&&&&&&& 所给年和月的天数
&&&&&&& 3. bool isValid(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond)
&&&&&&&&&& 判断所给年月日是否合法
&&&&&&& 下面是DateTime的一个例子:
#include &Poco/DateTime.h&&&
using Poco::DateT&
int main(int argc, char** argv)&
&&& DateT // the current date and time in UTC&&
&&& int year = now.year();&
&&& int month = now.month();&
&&& int day = now.day();&
&&& int dow = now.dayOfWeek();&
&&& int doy = now.dayOfYear();&
&&& int hour = now.hour();&
&&& int hour12 = now.hourAMPM();&
&&& int min = now.minute();&
&&& int sec = now.second();&
&&& int ms = now.millisecond();&
&&& int us = now.microsecond();&
&&& double jd = now.julianDay();&
&&& Poco::Timestamp ts = now.timestamp();&
&&& DateTime xmas(); //
00:00:00&&
&&& Poco::Timespan timeToXmas = xmas -&
&&& DateTime dt(, 2, 30, 45); //
02:30:45&&
&&& dt.assign(, 13, 45, 12, 345); //
12:45:12.345&&
&&& bool isAM = dt.isAM(); // false&&
&&& bool isPM = dt.isPM(); // true&&
&&& bool isLeap = DateTime::isLeapYear(2006); // false&&
&&& int days = DateTime::daysOfMonth(2006, 2); // 28&&
&&& bool isValid = DateTime::isValid(); // false&&
&&& dt.assign(2006, DateTime::OCTOBER, 22); //
00:00:00&&
&&& if (dt.dayOfWeek() == DateTime::SUNDAY)&
&&&&&&& // ...&&
&&& return 0;&
#include &Poco/DateTime.h&
using Poco::DateT
int main(int argc, char** argv)
&DateT // the current date and time in UTC
&int year = now.year();
&int month = now.month();
&int day = now.day();
&int dow = now.dayOfWeek();
&int doy = now.dayOfYear();
&int hour = now.hour();
&int hour12 = now.hourAMPM();
&int min = now.minute();
&int sec = now.second();
&int ms = now.millisecond();
&int us = now.microsecond();
&double jd = now.julianDay();
&Poco::Timestamp ts = now.timestamp();
&DateTime xmas(); //
&Poco::Timespan timeToXmas = xmas -
&DateTime dt(, 2, 30, 45); //
&dt.assign(, 13, 45, 12, 345); //
12:45:12.345
&bool isAM = dt.isAM(); // false
&bool isPM = dt.isPM(); // true
&bool isLeap = DateTime::isLeapYear(2006); // false
&int days = DateTime::daysOfMonth(2006, 2); // 28
&bool isValid = DateTime::isValid(); // false
&dt.assign(2006, DateTime::OCTOBER, 22); //
&if (dt.dayOfWeek() == DateTime::SUNDAY)
&return 0;
4. LocalDateTime类
&&&&&&& Poco::LocalDateTime同Poco::DateTime类似,不同的是Poco::LocalDateTime存储一个本地时间。关于本地时间和UTC时间有如下计算公式:
&&&&&&&&&&&&&&& (UTC时间 = 本地时间 - 时区差).
&&&&&&& 构造函数:
&&&&&&& 1. 通过当前时间构造
&&&&&&& 2. 通过Timestamp对象
&&&&&&& 3. 通过年、月、日、时、分、秒、微秒、毫秒构造
&&&&&&& 4. 通过儒略日时间构造(儒略日时间用double存储)
&&&&&&& 5. 作为可选项。时区可作为构造时第一个参数被指定。(如果没有指定的话,会使用系统当前的时区)
&&&&&&& 成员函数:
&&&&&&& 1. LocalDateTime支持所有的DateTime的函数。
&&&&&&& 2. 在进行比较之前,所有的关系操作符函数会把时间都换算为UTC时间
&&&&&&& 3. int tzd() const
&&&&&&&&&& 返回时区差
&&&&&&& 4. DateTime utc() const
&&&&&&&&&& 转换本地时间到utc时间
&&&&&&& 下面是一个例子:
#include &Poco/LocalDateTime.h&&&
using Poco::LocalDateT&
int main(int argc, char** argv)&
&&& LocalDateT // the current date and local time&&
&&& int year = now.year();&
&&& int month = now.month();&
&&& int day = now.day();&
&&& int dow = now.dayOfWeek();&
&&& int doy = now.dayOfYear();&
&&& int hour = now.hour();&
&&& int hour12 = now.hourAMPM();&
&&& int min = now.minute();&
&&& int sec = now.second();&
&&& int ms = now.millisecond();&
&&& int us = now.microsecond();&
&&& int tzd = now.tzd();&
&&& double jd = now.julianDay();&
&&& Poco::Timestamp ts = now.timestamp();&
&&& LocalDateTime dt1(, 2, 30, 45); //
02:30:45&&
&&& dt1.assign(, 13, 45, 12, 345); //
12:45:12.345&&
&&& LocalDateTime dt2(, 9, 12, 2, 30, 45, 0, 0); // UTC +1 hour&&
&&& dt2.assign(, 10, 13, 13, 45, 12, 345, 0);&
&&& Poco::Timestamp nowTS;&
&&& LocalDateTime dt3(3600, nowTS); // construct from Timestamp&&
&&& return 0;&
#include &Poco/LocalDateTime.h&
using Poco::LocalDateT
int main(int argc, char** argv)
&LocalDateT // the current date and local time
&int year = now.year();
&int month = now.month();
&int day = now.day();
&int dow = now.dayOfWeek();
&int doy = now.dayOfYear();
&int hour = now.hour();
&int hour12 = now.hourAMPM();
&int min = now.minute();
&int sec = now.second();
&int ms = now.millisecond();
&int us = now.microsecond();
&int tzd = now.tzd();
&double jd = now.julianDay();
&Poco::Timestamp ts = now.timestamp();
&LocalDateTime dt1(, 2, 30, 45); //
&dt1.assign(, 13, 45, 12, 345); //
12:45:12.345
&LocalDateTime dt2(, 9, 12, 2, 30, 45, 0, 0); // UTC +1 hour
&dt2.assign(, 10, 13, 13, 45, 12, 345, 0);
&Poco::Timestamp nowTS;
&LocalDateTime dt3(3600, nowTS); // construct from Timestamp
&return 0;
5. Timespan类
&&&&&&& Poco::Timespan能够提供一个微秒精度的时间间隔,也可以用天、小时、分钟、秒、微秒、毫秒来表示。在其内部这个时间间隔用一个64-bit整形来表示。
&&&&&&& 构造函数:
&&&&&&& 1. 一个TimeStamp::TimeDiff对象(微秒精度)
&&&&&&& 2. 秒+微秒
&&&&&&&&&& 主要用于从timeval结构体构建
&&&&&&& 3. 通过日、时、分、秒、微秒构造
&&&&&&& 操作符:
&&&&&&& 1. Poco::Timespan支持所有的关系操作符
&&&&&&& (==, !=, &, &=, &, &=)
&&&&&&& 2. Poco::Timespan支持加法和减法操作
&&&&&&& (+, -, +=, -=)
&&&&&&& 成员函数:
&&&&&&& 1. int days() const
&&&&&&&&&& 返回时间跨度的天
&&&&&&& 2. int hours() const
&&&&&&&&&& 返回时间跨度的小时(0 - 23)
&&&&&&& 3. int totalHours() const
&&&&&&&&&& 返回时间跨度总的小时数
&&&&&&& 4. int minutes() const
&&&&&&&&&& 返回时间跨度的分钟(0 - 59)
&&&&&&& 5. int totalMinutes() const
&&&&&&&&&& 返回时间跨度总的分钟数
&&&&&&& 6. int seconds() const
&&&&&&&&&& 返回时间跨度的秒(0 - 60)
&&&&&&& 7. int totalSeconds() const
&&&&&&&&&& 返回时间跨度总的秒数
&&&&&&& 8. int milliseconds() const
&&&&&&&&&& 返回时间跨度的毫秒((0 - 999)
&&&&&&& 9. int totalMilliseconds() const
&&&&&&&&&& 返回时间跨度总的毫秒数
&&&&&&& 10. int microseconds() const
&&&&&&&&&& 返回时间跨度的微秒( (0 - 999)
&&&&&&& 11. int totalMicroseconds() const
&&&&&&&&&& 返回时间跨度总的微秒数
&&&&&&& 下面是一个例子:
#include &Poco/Timespan.h&&&
using Poco::T&
int main(int argc, char** argv)&
&&& Timespan ts1(1, 11, 45, 22, 123433); // 1d 11h 45m 22.123433s&&
&&& Timespan ts2(33*Timespan::SECONDS); // 33s&&
&&& Timespan ts3(2*Timespan::DAYS + 33*Timespan::HOURS); // 3d 33h&&
&&& int days = ts1.days(); // 1&&
&&& int hours = ts1.hours(); // 11&&
&&& int totalHours = ts1.totalHours(); // 35&&
&&& int minutes = ts1.minutes(); // 45&&
&&& int totalMins = ts1.totalMinutes(); // 2145&&
&&& int seconds = ts1.seconds(); // 22&&
&&& int totalSecs = ts1.totalSeconds(); // 128722&&
&&& return 0;&
#include &Poco/Timespan.h&
using Poco::T
int main(int argc, char** argv)
&Timespan ts1(1, 11, 45, 22, 123433); // 1d 11h 45m 22.123433s
&Timespan ts2(33*Timespan::SECONDS); // 33s
&Timespan ts3(2*Timespan::DAYS + 33*Timespan::HOURS); // 3d 33h
&int days = ts1.days(); // 1
&int hours = ts1.hours(); // 11
&int totalHours = ts1.totalHours(); // 35
&int minutes = ts1.minutes(); // 45
&int totalMins = ts1.totalMinutes(); // 2145
&int seconds = ts1.seconds(); // 22
&int totalSecs = ts1.totalSeconds(); // 128722
&return 0;
&&&&&&& 下面来看一个DateTime, LocalDateTime和Timespan的混合例子:
#include &Poco/DateTime.h&&&
#include &Poco/Timespan.h&&&
using Poco::DateT&
using Poco::T&
int main(int argc, char** argv)&
&&& // what is my age?&&
&&& DateTime birthdate(, 2, 30); //
02:30:00&&
&&& DateT&
&&& Timespan age = now -&
&&& int days = age.days(); // in days&&
&&& int hours = age.totalHours(); // in hours&&
&&& int secs = age.totalSeconds(); // in seconds&&
&&& // when was I 10000 days old?&&
&&& Timespan span(10000*Timespan::DAYS);&
&&& DateTime dt = birthdate +&
&&& return 0;&
#include &Poco/DateTime.h&
#include &Poco/Timespan.h&
using Poco::DateT
using Poco::T
int main(int argc, char** argv)
&// what is my age?
&DateTime birthdate(, 2, 30); //
&Timespan age = now -
&int days = age.days(); // in days
&int hours = age.totalHours(); // in hours
&int secs = age.totalSeconds(); // in seconds
&// when was I 10000 days old?
&Timespan span(10000*Timespan::DAYS);
&DateTime dt = birthdate +
&return 0;
6. Timezone类
&&&&&&& Poco::Timezone提供静态函数用于获取时区信息和夏令时信息。
&&&&&&& 1. 时区差
&&&&&&& 2. 是否采用夏时制(daylight saving time (DST))
&&&&&&& 3. 时区名
&&&&&&& 成员函数:
&&&&&&& 1. int utcOffset()
&&&&&&&&&& 返回本地时间相对于UTC时间的差值(精度秒)。不包括夏令时偏移:
&&&&&&&&&&&&&&&&&& (local time = UTC + utcOffset())
&&&&&&& 2. int dst()
&&&&&&&&&& 返回夏令时偏移。通常是固定值3600秒。0的话表示无夏令时。
&&&&&&& 3. bool isDst(const Timestamp& timestamp)
&&&&&&&&&& 对于给定的timestamp时间测试,是否使用夏令时
&&&&&&& 4. int tzd()
&&&&&&&&&& 返回本地时间相对于UTC时间的差值(精度秒)。包括夏令时偏移:
&&&&&&&&&& (tzd = utcOffset() + dst())
&&&&&&& 5. std::string name()
&&&&&&&&&& 返回当前的时区名
&&&&&&& 6. std::string standardName()
&&&&&&&&&& 返回当前的标准时区名(如果不采用夏令时)
&&&&&&& 7. std::string dstName()
&&&&&&&&&& 返回当前的时区名(如果采用夏令时)
&&&&&&& 8. 时区名的返回依赖于操作系统,并且名称不具有移植性,仅作显示用。
&&&&&&& 下面是一个使用的例子:
#include &Poco/Timezone.h&&&
#include &Poco/Timestamp.h&&&
using Poco::T&
using Poco::T&
int main(int argc, char** argv)&
&&& int utcOffset = Timezone::utcOffset();&
&&& int dst = Timezone::dst();&
&&& bool isDst = Timezone::isDst(Timestamp());&
&&& int tzd = Timezone::tzd();&
&&& std::string name = Timezone::name();&
&&& std::string stdName = Timezone::standardName();&
&&& std::string dstName = Timezone::dstName();&
&&& return 0;&
#include &Poco/Timezone.h&
#include &Poco/Timestamp.h&
using Poco::T
using Poco::T
int main(int argc, char** argv)
&int utcOffset = Timezone::utcOffset();
&int dst = Timezone::dst();
&bool isDst = Timezone::isDst(Timestamp());
&int tzd = Timezone::tzd();
&std::string name = Timezone::name();
&std::string stdName = Timezone::standardName();
&std::string dstName = Timezone::dstName();
&return 0;
6. Poco::DateTimeFormatter类
&&&&&&& Poco::DateTimeFormatter用来定义当Timestamp, DateTime, LocalDateTime and Timespan转换为字符串时所需的日期和事件格式。Poco::DateTimeFormatter的作用和strftime()是类似的。Poco::DateTimeFormat内部定义了一些约定的格式。
&&&&&&& 1. ISO8601_FORMAT (T12:00:00+01:00)
&&&&&&& 2. RFC1123_FORMAT (Sat, 1 Jan :00 +0100)
&&&&&&& 3. SORTABLE_FORMAT ( 12:00:00)
&&&&&&& 4. For more information, please see the reference documentation.
&&&&&&& 成员函数:
&&&&&&& 所有的DateTimeFormatter函数都是静态的。
&&&&&&& 下面是一个使用的例子:
#include &Poco/Timestamp.h&&&
#include &Poco/Timespan.h&&&
#include &Poco/DateTimeFormatter.h&&&
#include &Poco/DateTimeFormat.h&&&
using Poco::DateTimeF&
using Poco::DateTimeF&
int main(int argc, char** argv)&
&&& Poco::DateTime dt(, 15, 22, 34);&
&&& std::string s(DateTimeFormatter::format(dt, &%e %b %Y %H:%M&));&
&&& // &22 Oct &&&
&&& Poco::T&
&&& s = DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT);&
&&& // & 09:27:44&&&
&&& Poco::Timespan span(5, 11, 33, 0, 0);&
&&& s = DateTimeFormatter::format(span, &%d days, %H hours, %M minutes&);&
&&& // &5 days, 11 hours, 33 minutes&&&
&&& return 0;&
#include &Poco/Timestamp.h&
#include &Poco/Timespan.h&
#include &Poco/DateTimeFormatter.h&
#include &Poco/DateTimeFormat.h&
using Poco::DateTimeF
using Poco::DateTimeF
int main(int argc, char** argv)
&Poco::DateTime dt(, 15, 22, 34);
&std::string s(DateTimeFormatter::format(dt, &%e %b %Y %H:%M&));
&// &22 Oct &
&s = DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT);
&// & 09:27:44&
&Poco::Timespan span(5, 11, 33, 0, 0);
&s = DateTimeFormatter::format(span, &%d days, %H hours, %M minutes&);
&// &5 days, 11 hours, 33 minutes&
&return 0;
7. Poco::DateTimeParser类
&&&&&&& Poco::DateTimeParser用来从字符串中解析时间和日期。下面是其一个例子:
#include &Poco/DateTimeParser.h&&&
#include &Poco/DateTime.h&&&
#include &Poco/DateTimeFormat.h&&&
#include &Poco/LocalDateTime.h&&&
#include &Poco/Timestamp.h&&&
using Poco::DateTimeP&
using Poco::DateTimeF&
using Poco::DateT&
int main(int argc, char** argv)&
&&& std::string s(&Sat, 1 Jan :00 GMT&);&
&&& DateT&
&&& DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, s, dt, tzd);&
&&& Poco::Timestamp ts = dt.timestamp();&
&&& Poco::LocalDateTime ldt(tzd, dt);&
&&& bool ok = DateTimeParser::tryParse(&&, dt, tzd);&
&&& ok = DateTimeParser::tryParse(&%e.%n.%Y&, &22.10.2006&, dt, tzd);&
&&& return 0;&
#include &Poco/DateTimeParser.h&
#include &Poco/DateTime.h&
#include &Poco/DateTimeFormat.h&
#include &Poco/LocalDateTime.h&
#include &Poco/Timestamp.h&
using Poco::DateTimeP
using Poco::DateTimeF
using Poco::DateT
int main(int argc, char** argv)
&std::string s(&Sat, 1 Jan :00 GMT&);
&DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, s, dt, tzd);
&Poco::Timestamp ts = dt.timestamp();
&Poco::LocalDateTime ldt(tzd, dt);
&bool ok = DateTimeParser::tryParse(&&, dt, tzd);
&ok = DateTimeParser::tryParse(&%e.%n.%Y&, &22.10.2006&, dt, tzd);
&return 0;
8. Stopwatch类
&&&&&&& Stopwatch用来测量时间差值,精度为微秒.下面是其定义:
class Foundation_API Stopwatch&
&&& /// A simple facility to measure time intervals&&
&&& /// with microsecond resolution.&&
&&& /// Note that Stopwatch is based on the Timestamp&&
&&& /// class. Therefore, if during a Stopwatch run,&&
&&& /// the system time is changed, the measured time&&
&&& /// will not be correct.&&
&&& Stopwatch();&
&&& ~Stopwatch();&
&&& void start();&
&&&&&&& /// Starts (or restarts) the stopwatch.&&
&&& void stop();&
&&&&&&& /// Stops or pauses the stopwatch.&&
&&& void reset();&
&&&&&&& /// Resets the stopwatch.&&
&&& void restart();&
&&&&&&& /// Resets and starts the stopwatch.&&
&&& Timestamp::TimeDiff elapsed()&
&&&&&&& /// Returns the elapsed time in microseconds&&
&&&&&&& /// since the stopwatch started.&&
&&& int elapsedSeconds()&
&&&&&&& /// Returns the number of seconds elapsed&&
&&&&&&& /// since the stopwatch started.&&
&&& static Timestamp::TimeVal resolution();&
&&&&&&& /// Returns the resolution of the stopwatch.&&
&&& Stopwatch(const Stopwatch&);&
&&& Stopwatch& operator = (const Stopwatch&);&
&&& Timestamp&&&&&&&&&& _&
&&& Timestamp::TimeDiff _&
&&& bool&&&&&&&&&&&&&&& _&
class Foundation_API Stopwatch
&/// A simple facility to measure time intervals
&/// with microsecond resolution.
&/// Note that Stopwatch is based on the Timestamp
&/// class. Therefore, if during a Stopwatch run,
&/// the system time is changed, the measured time
&/// will not be correct.
&Stopwatch();
&~Stopwatch();
&void start();
&&/// Starts (or restarts) the stopwatch.
&void stop();
&&/// Stops or pauses the stopwatch.
&void reset();
&&/// Resets the stopwatch.
&void restart();
&&/// Resets and starts the stopwatch.
&Timestamp::TimeDiff elapsed()
&&/// Returns the elapsed time in microseconds
&&/// since the stopwatch started.
&int elapsedSeconds()
&&/// Returns the number of seconds elapsed
&&/// since the stopwatch started.
&static Timestamp::TimeVal resolution();
&&/// Returns the resolution of the stopwatch.
&Stopwatch(const Stopwatch&);
&Stopwatch& operator = (const Stopwatch&);
&Timestamp&&&&&&&&&& _
&Timestamp::TimeDiff _
&bool&&&&&&&&&&&&&&& _
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 timespan 的文章

更多推荐

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

点击添加站长微信