获取系统日期时间的简单方法

本文介绍了如何在C/C++中使用标准库函数获取和处理系统日期时间,包括time.h的基本概念、获取日历时间、日期时间格式转换、时间计时等功能,并给出了实例演示和代码示例。
摘要由CSDN通过智能技术生成

获取系统日期时间的简单方法

不用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, 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值