C语言 时间类型

time.h

定义的类型:

time_t|表示距离 UTC 时间 1970-01-01 00:00:00 的秒数,类型是 long
clock_t|占用cpu的时间
struct tm|存储年月日的结构体(time_t不易人来读)

struct tm
{
    int	tm_sec;	/* Seconds: 0-59 (K&R says 0-61?) */
    int	tm_min;	/* Minutes: 0-59 */
    int	tm_hour;	/* Hours since midnight: 0-23 */
    int	tm_mday;	/* Day of the month: 1-31 */
    int	tm_mon;	/* Months *since* january: 0-11 */
    int	tm_year;	/* Years since 1900 */
    int	tm_wday;	/* Days since Sunday (0-6) */
    int	tm_yday;	/* Days since Jan. 1: 0-365 */
    int	tm_isdst;	/* +1 Daylight Savings Time, 0 No DST,* -1 don't know */
};
# 函数描述
time获取当前系统时间(UTC时间)的time_t值
ctime返回刻度格式的日期和时间信息

time_t time(time_t *timer)
获得时间戳

#include <time.h>
#include <stdio.h>

int main()
{
	time_t t;
	t = time(NULL);
	printf("1970-01-01距现在: %ld秒\n", t);
	system("pause");
}

char *ctime(const time_t *timer) 等同于 astime( localtime(tp) )
获得字符串类型的可读时间

#pragma warning(disable:4996)
#include <stdio.h>
#include <time.h>

int main()
{
	time_t curtime;
	time(&curtime);
	printf("当前时间 = %s", ctime(&curtime));
	system("pause");
}

struct tm *gmtime(time_t *time)
struct tm *localtime(const time_t *timer)
返回一个以tm结构表达的机器时间信息

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	time_t t;
	struct tm *gmt;
	t = time(NULL);
	gmt = gmtime(&t);
	printf("距1900年:%d年\n", gmt->tm_year);
	printf("现在是:%d+1月\n", gmt->tm_mon);
	system("pause");
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
	time_t timer;
	struct tm *gmt;
	timer = time(NULL); //不传指针类型的参数,就可以使用变量接收
	gmt = localtime(&timer);
	printf("距1900年:%d年\n", gmt->tm_year);
	printf("现在是:%d+1月\n", gmt->tm_mon);
	system("pause");
}

char* asctime(struct tm * ptr)
得到机器时间(日期时间转换为ASCII码)

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
	struct tm t; //通过自己为每个tm成员赋值,没有什么实际意思,大多数情况下都是通过系统函数计算time_t来得到tm中的各个数值
	char str[80];
	t.tm_sec = 1;
	t.tm_min = 1;
	t.tm_hour = 1;
	t.tm_mday = 1;
	t.tm_mon = 1;
	t.tm_year = 119;
	t.tm_wday = 0;
	t.tm_yday = 0;
	t.tm_isdst = 0;
	strcpy(str,asctime(&t)); //返回指针指向数组
	printf("%s",str);
	system("pause");
}

size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向 strDest中存放maxsize个字符。


/*
   类似于sprintf(),识别以百分号(%)开始 的格式命令集合,格式化输出结果放在一个字符串中。格式命令列在下面,它们是区分大小写的。
	%a 星期几的简写
	%A 星期几的全称
	%b 月分的简写
	%B 月份的全称
	%c 标准的日期的时间串
	%C 年份的后两位数字
	%d 十进制表示的每月的第几天
	%D 月/天/年
	%e 在两字符域中,十进制表示的每月的第几天
	%F 年-月-日
	%g 年份的后两位数字,使用基于周的年
	%G 年分,使用基于周的年
	%h 简写的月份名
	%H 24小时制的小时
	%I 12小时制的小时
	%j 十进制表示的每年的第几天
	%m 十进制表示的月份
	%M 十时制表示的分钟数
	%n 新行符
	%p 本地的AM或PM的等价显示
	%r 12小时的时间
	%R 显示小时和分钟:hh:mm
	%S 十进制的秒数
	%t 水平制表符
	%T 显示时分秒:hh:mm:ss
	%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
	%U 第年的第几周,把星期日做为第一天(值从0到53)
	%V 每年的第几周,使用基于周的年
	%w 十进制表示的星期几(值从0到6,星期天为0)
	%W 每年的第几周,把星期一做为第一天(值从0到53)
	%x 标准的日期串
	%X 标准的时间串
	%y 不带世纪的十进制年份(值从0到99)
	%Y 带世纪部分的十进制年份
	%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
	%% 百分号
*/
 
#include <stdio.h>
#include <time.h>
void main()
{
       struct tm *newtime;
       char tmpbuf[128];
       time_t lt1;
       time( <1 );
       newtime=localtime(<1);
       strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
       printf(tmpbuf);
       
       return 0;
}
 
 
# 其他
1. Coordinated Universal Time(UTC),也称格林威治标准时间(Greenwich Mean Time,GMT),世界标准时间,中国内地的时间与UTC得时差为+8
2. Calendar Time,日历时间.定时任务几月几号几点执行
3. epoch:时间点
4. clock tick: cpu时间





参考:
https://blog.csdn.net/chenyiming_1990/article/details/8682552
https://www.runoob.com/cprogramming/c-standard-library-time-h.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值