时间相关库<ctime>解析

原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5649487.html

一、定义的类型

1.clock_t:时钟类型

2.size_t:unsigned int

3.time_t:时间类型

4.struct tm:结构,成员如下:

Member Type Meaning Range
tm_sec int seconds after the minute 0-60*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900  
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag  

二、定义的宏

1.NULL:空指针

2.CLOCKS_PER_SEC:每秒的时钟周期

三、时间操作函数

1.clock:clock_t clock (void);

返回当前时钟周期,转换为s除以CLOCKS_PER_SEC

2.difftime:double difftime (time_t end, time_t beginning);

计算geginning和end之间的时间单位秒

3.mktime:time_t mktime (struct tm * timeptr);

返回timeptr指向的时间,timeptr也会更新其他的。

  time_t rawtime;
  struct tm * timeinfo;
  int year, month ,day;
  const char * weekday[] = { "Sunday", "Monday",
                             "Tuesday", "Wednesday",
                             "Thursday", "Friday", "Saturday"};
  printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);
  printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);
  printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;
  mktime ( timeinfo );
  printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);输出为:
Enter year: 2000
Enter month: 5
Enter day: 20
That day is a Saturday.

4.time:time_t time (time_t* timer);

得到当前的日历时间,存储在timer中,如果获取失败返回空指针,成功返回当前日历时间

四、时间转化相关函数

1.asctime:char* asctime (const struct tm * timeptr);

将tm类型的时间转化为字符串的形式,返回的字符串如Www Mmm dd hh:mm:ss yyyy

2.ctime:char* ctime (const time_t * timer);

将time_t类型的时间转化为字符串的形式,返回的字符串如:Www Mmm dd hh:mm:ss yyyy

3.gmtime:struct tm * gmtime (const time_t * timer);

将time_t的时间类型转化为UTC时间,相应的时间存储在结构tm中

4.localtime:struct tm * localtime (const time_t * timer);

使用timer中的时间填充localtime

5.strftime:size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );

将结构tm描述的时间按format格式拷贝到字符串ptr中,最大maxsize字符。formate格式如下:

  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];
  time (&rawtime);
  timeinfo = localtime (&rawtime);
  strftime (buffer,80,"Now it‘s %I:%M%p.",timeinfo);
  puts (buffer);

 输出为:

Now it‘s 03:21PM.
specifier Replaced by Example
%a Abbreviated weekday name * Thu
%A Full weekday name * Thursday
%b Abbreviated month name * Aug
%B Full month name * August
%c Date and time representation * Thu Aug 23 14:55:02 2001
%C Year divided by 100 and truncated to integer (00-99) 20
%d Day of the month, zero-padded (01-31) 23
%D Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
%e Day of the month, space-padded ( 1-31) 23
%F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23
%g Week-based year, last two digits (00-99) 01
%G Week-based year 2001
%h Abbreviated month name * (same as %b) Aug
%H Hour in 24h format (00-23) 14
%I Hour in 12h format (01-12) 02
%j Day of the year (001-366) 235
%m Month as a decimal number (01-12) 08
%M Minute (00-59) 55
%n New-line character (‘\n‘)  
%p AM or PM designation PM
%r 12-hour clock time * 02:55:02 pm
%R 24-hour HH:MM time, equivalent to %H:%M 14:55
%S Second (00-61) 02
%t Horizontal-tab character (‘\t‘)  
%T ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S 14:55:02
%u ISO 8601 weekday as number with Monday as 1 (1-7) 4
%U Week number with the first Sunday as the first day of week one (00-53) 33
%V ISO 8601 week number (00-53) 34
%w Weekday as a decimal number with Sunday as 0 (0-6) 4
%W Week number with the first Monday as the first day of week one (00-53) 34
%x Date representation * 08/23/01
%X Time representation * 14:55:02
%y Year, last two digits (00-99) 01
%Y Year 2001
%z ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
If timezone cannot be determined, no characters
+100
%Z Timezone name or abbreviation *
If timezone cannot be determined, no characters
CDT
%% % sign %

参考:http://www.cplusplus.com/reference/ctime/

时间: 2024-10-07 22:02:40

时间相关库<ctime>解析的相关文章

iOS开发-使用第三方库AFNetWorking解析JSON和XML数据

利用第三方库AFNetWorking解析网络请求的JSON和MXL数据具有很多方便的地方. 第三方库的下载地址:https://github.com/AFNetworking/AFNetworking 导入的包和宏定义 1 #import "ViewController.h" 2 #import "AFNetworking.h" 3 #import "GDataXMLNode.h" 4 5 //json地址 6 #define kJSONUrlS

boost之program_options库,解析命令行参数、读取配置文件

一.命令行解析 tprogram_options解析命令行参数示例代码: [cpp] view plaincopy #include <iostream> using namespace std; #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char*argv[]) { //int level; po::options_descripti

在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路**号院**社区&output=json&ak=您的AK密钥 返回结果实例: { "status":0, "result": { "location":{"lng":116.79, "lat":

VC++编译lua库 无法解析lua符号问题

VC编译lua库 出现错误: 错误 36 error LNK1120: 21 个无法解析的外部命令 C:\Users\luozhuang\Desktop\onscripter-20120416VC\Release\onscripter.exe onscripter 错误 19 error LNK2019: 无法解析的外部符号 "char const * __cdecl lua_tolstring(struct lua_State *,int,unsigned int *)" ([ema

Linux中用户和组中认证库和解析库的文件格式以及默认参数定义文件

解析库:/etc/passwd,/etc/group    组名和组的对应关系 认证库:/etc/shadow,/etc/gshadow  组密码的相关内容存储位置 与用户账户和组账户相关的文件有: /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/default/useradd /etc/login.defs(登录的默认属性) /etc/skel(家目录默认存储文件) /etc/passwd(用户的相关信息): 用户账户在/etc/pas

OCP 认证考试报名费技巧题库051052053解析合格线

本人于2017年4月22日通过参加OCP考试,第一次参加,一天之内考了三门,三门一次性通过,052 - 95% ,053 - 86% ,051 - 100% 一.关于考试考试报名费: 052:158$ 053:158$ 051:132$ 考试合格线: 052:66% 053: 66% 051: 60% 本人分数: 052:95% 053: 86% 051: 100% 考试题量: 052:70题 053: 78题 051: 64题 考试时间(计划): 052: 9:00-11:00(2小时) 05

python应用之爬虫实战2 请求库与解析库

知识内容: 1.requests库 2.selenium库 3.BeautifulSoup4库 一.requests库 1.安装及简单使用 (1)安装 1 pip3 install requests (2)简单使用 1 import requests 2 3 r = requests.get("http://www.baidu.com") # 发起get请求 4 print(r.status_code) # 打印状态码 5 r.encoding = "utf-8"

Android 百分比布局库(percent-support-lib) 解析与扩展

idxkhrsoyl.mag.mybaby.com.cn/wylcvzaxir.mag.mybaby.com.cn/vofjbsfzzq.mag.mybaby.com.cn/bpqccmjsjl.mag.mybaby.com.cn/fbiwzmrxky.mag.mybaby.com.cn/jgbhxguvpu.mag.mybaby.com.cn/hjwkryckpq.mag.mybaby.com.cn/ndprddekmr.mag.mybaby.com.cn/kcncwrjsqm.mag.myb

开源库 bouncyEditText解析

转载请注明出处:王亟亟的大牛之路 运行效果 如何使用? 在布局XML里引用即可,如下: <com.mingle.BouncyEditText android:layout_width="fill_parent" android:hint="账号" android:text="255" android:layout_height="wrap_content"> </com.mingle.BouncyEditTe