系统时间与日期函数的使用

本文介绍了在Unix系统中处理时间的结构体,包括`struct tm`、`struct timeval`和`struct timezone`,以及相关类型如`time_t`和`clock_t`。`struct tm`包含了秒、分、小时等详细时间信息,而`timeval`则包含秒和微秒。`timezone`结构体用于记录与格林威治时间的分钟差和日光节约时间状态。`time_t`和`clock_t`是表示时间的不同方式,前者表示从程序开始到现在的秒数,后者表示时钟节拍计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<time.h>

系统时间与日期函数如下:
函数名
功能
asctime将时间和日期以字符串格式表示
ctime将时间和日期以字符串格式表示
gettimeofday取得目前的时间
localtime取得当地目前时间和日期,并转换成真实世界的时间日期表示方法
mktime将时间结构数据转换成经过的秒数
settimeofday设置目前时间
time取得目前时间
gmtime取得目前时间和日期

以后再贴实现代码。
下面是相关的数据结构:
  • struct tm
  1. struct tm
  2. {
  3. int tm_sec;
  4. int tm_min;
  5. int tm_hour;
  6. int tm_mday;
  7. int tm_mon;
  8. int tm_year;
  9. int tm_wday;
  10. int tm_yday;
  11. int tm_isdst;
  12. };
每个成员的定义如下:
MemberMeaningRange
tm_secseconds after the minute0-61*
tm_minminutes after the hour0-59
tm_hourhours since midnight0-23
tm_mdayday of the month1-31
tm_monmonths since January0-11
tm_yearyears since 1900
tm_wdaydays since Sunday0-6
tm_ydaydays since January 10-365
tm_isdstDaylight Saving Time flag

  • struct timeval
  1. struct timeval
  2. {
  3. long tv_sec; //second
  4. long tv_usec; //microsecond
  5. };

  • struct timezone
  1. struct timezone
  2. {
  3. int tz_minuteswest;//和Greenwich 时间差了多少分钟
  4. int tz_dsttime;//日光节约时间的状态
  5. };

  • time_t,clock_t
time_t is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. This is due to historical reasons, since it corresponds to a unix timestamp, but is widely implemented in C libraries across all platforms.

翻译:

time_t通常被认为是一个整数值,表示从1970年1月1日 00:00起(用UTC表示)到现在的时间间隔。这是历史原因造成的,因为它对应UNIX时间戳,但是却广泛地被跨平台实现C库。


clock_t is a type that capable of representing clock tick counts and support arithmetical operations.This type is returned by the clock function of the <ctime> header to represent the number of click ticks since the beginning of the program execution.


翻译:

clock_t 类型表示时钟节拍,支持算术运算。定义在ctime.h头文件的clock函数的返回值就是这种类型的,它表示自程序开始运行以来所经过的节拍数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值