获取系统日期时间的简单方法
不用MFC类,直接使用C/C++ANSI标准库函数
1.time.h简介
日常编程中,常常需要获取系统的日期时间或对相关日期时间进行简单处理等。我常常使用MFC类库中的CMonthCalCtrl、 CDateTimeCtrl
CTime/CTimeSpan、COleDateTime/COleDateTimeSpan等类,它们提供了很强大的功能。但有时仅仅只需要获取当前的日期/时间,使用MFC类库感觉是大材小用啦(杀鸡焉用宰牛刀)。无意中发现,C/C++库或者系统API也为我们提供了相关的函数,简单搞定。能简单的就简单搞定,这也是一种境界哦(条条道路通罗马,只是看那条路好走/距离近)。查看MSDN,得到了关于日期时间的函数说明:
================================================================================================================================
Run-Time Library Reference |
Time Management |
Updated: November 2007
Use these functions to get the current time and convert, adjust, and storeit as necessary. The current time is the system time.
The _ftime and localtime routines use theTZenvironment variable. IfTZ is not set, the run-time library attempts touse the time-zone information specified by the operating system. If thisinformation is unavailable, these functions use the default value of PST8PDT.For more information onTZ, see_tzset; also see _daylight, timezone, and _tzname.
Time Routines
Function |
Use |
.NET Framework equivalent |
|
asctime, _wasctime, asctime_s, _wasctime_s |
Convert time from type struct tm to character string.The versions of these functions with the_s suffix are more secure. |
System::DateTime::ToLongDateString, System::DateTime::ToLongTimeString, System::DateTime::ToShortDateString, System::DateTime::ToShortTimeString, System::DateTime::ToString |
|
clock |
Return elapsed CPU time for process. |
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples. |
|
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64, _ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s |
Convert time from type time_t, __time32_t or __time64_t to character string. The versions of these functions with the _s suffix are more secure. |
System::DateTime::GetDateTimeFormats, System::DateTime::ToString, System::DateTime::ToLongTimeString, System::DateTime::ToShortTimeString |
|
difftime, _difftime32, _difftime64 |
Compute difference between two times. |
System::DateTime::Subtract |
|
_ftime, _ftime32, _ftime64, _ftime_s, _ftime32_s, _ftime64_s |
Store current system time in variable of type struct _timeb or type struct __timeb64 The versions of these functions with the _s suffix are more secure. |
System::DateTime::Now |
|
_futime, _futime32, _futime64 |
Set modification time on open file |
System::IO::File::SetLastAccessTime, System::IO::File::SetLastWriteTime, System::IO::File::SetCreationTime |
|
gmtime, _gmtime32, _gmtime64, _gmtime_s, _gmtime32_s, _gmtime64_s |
Convert time from type time_t to struct tm or from type __time64_t to struct tm. The versions of these functions with the _s suffix are more secure. |
System::DateTime::UtcNow, System::DateTime::ToUniversalTime |
|
localtime, _localtime32, _localtime64, localtime_s, _localtime32_s, |