Linux(五)时间编程
1、日历时间和本地时间
1.1 日历时间
日历时间是指从某一时刻开始按秒(或毫秒)进行计时,以秒(或毫秒)为单位不间断的累加。在Ubuntu下是从1970年1月1日 00:00:00 开始按秒计时,其实就是系统程序里的一个int变量在不停的加加,它的单位是秒。
例如:在程序里想得到今年 是几几年(用year表示),假设计时器变量为t ,year = 1970 + t / ( 365*24*60*60 )
1.2 本地时间
本地时间就是我们平常在自己所在地区使用的时间,比如我我中国所使用的是北京时间
1.3 与时间有关的shell命令
date # 显示当前日期-- 中国北京时间
date –u # 显示当前日期时间 -- 世界标准时间 UTC 格林尼治时间和北京时间相差8个时区,加8个小时就是北京时间
date –R # 显示当前日期时间 – RFC格式
time # 显示程序运行的时间
cal # 显示日历 cal month year 显示指定年月的日历: cal 3 2005
2、在Linux(ubuntu)上获取日历时间、本地时间
2.1 获取日历时间函数 time
功能:返回日历时间
函数原型:time_t time(time_t *t);
所属头文件:#include<time.h>
参数:不为空的情况下保存日历时间值给传入的参数(注意如果参数是一个time_t类型指针必须分配内存空间!):1. 传入一个time_t 类型(其实就是long)的变量地址,函数会将日历时间赋给该变量(存放到该变量的地址空间里);2.传入一个time_t类型的指针,分配给该指针一个time_t大小的空间里面存有日历时间的值;3. 直接给NULL 。 建议使用1、3两种方式获取日历时间
返回值:成功返回日历时间,失败返回-1
示例代码:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main(void)
{
time_t t_para,t_retuVal;
time_t* t_point = (time_t*)malloc(sizeof(time_t)); //指针必须分配内存空间
/*方式一: 传入time_t类型的变量地址 */
t_retuVal = time(&t_para);
printf("方式一: t_para = %ld , t_retuVal = %ld\n",t_para,t_retuVal);
/*方式二: 传入time_t类型的指针*/
t_retuVal = time(t_point);
printf("方式二: *t_point = %ld , t_retuVal = %ld\n",*t_point,t_retuVal);
/*方式三: 直接给NULL */
t_retuVal = time(NULL);
printf("方式三: t_retuVal = %ld\n",t_retuVal);
return 0;
}
结果:
2.2 获取本地时间函数
2.2.1 localtime
功能:将timep指向的日历时间转换为本地时间
原型:struct tm *localtime(const time_t *timep);
所属头文件:#include<time.h>
参数:timep:指向待转化日历时间
返回值:返回以struct tm形式存储的本地时间,失败返回NULL
struct tm {
int tm_sec; /* seconds */ 0~59
int tm_min; /* minutes */ 0~59
int tm_hour; /* hours */ 0 ~ 23
int tm_mday; /* day of the month */ 1~31
int tm_mon; /* month */ 月份范围是0~11因此要加 1
int tm_year; /* year */ 要加上1900
int tm_wday; /* day of the week */ 一周当中的第几天
int tm_yday; /* day in the year */ 一年当中的第几天
int tm_isdst; /* daylight saving time */ 夏令时
};
2.2.1 ctime
功能:将日历时间转化为本地时间
原型:char *ctime(const time_t *timep);
所属头文件:#include<time.h>
参数:待转化的日历时间
返回值:返回一字符串表示目前当地的时间日期。
2.2.3 asctime
功能:将struct tm格式的时间转化为字符串
原型:char *asctime(const struct tm *tm);
所属头文件:#include<time.h>
参数:待转化的tm格式的时间
返回值:字符串显示的时间
2.2.4 自由格式strftime(不推荐使用,了解即可)
功能:字符串显示时间
原型:size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
所属头文件:#include <time.h>
参数:
s :存放format格式字符串的空间的地址(就是存放时间字符串的地址,格式是由参数format指定的)。
max: 这是给 str 要复制的字符的最大数目。
format : 常规字符和特殊格式说明符
我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
%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 每周的第几天,星期一为第一天 (值从1到7,星期一为1)
%U 第年的第几周,把星期日作为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
返回值:s指向缓冲区的字节长度,不包括‘\0’
2.2.1、2.2.2、2.2.3示例代码:
#include<stdio.h>
#include<time.h>
struct tm* locTime;
int main(void)
{
time_t t,reValue;
reValue = time(&t);
/*通过localtime 获取本地时间*/
locTime = localtime(&reValue);
printf("%d年%d月%d日 %d:%d:%d\n",locTime->tm_year+1900,locTime->tm_mon+1,locTime->tm_mday,locTime->tm_hour,locTime->tm_min,locTime->tm_sec);
/*日历时间转化为本地时间*/
char* time_p = NULL;
time_p = ctime(&reValue);
printf("%s\n",time_p);
/*
将struct tm类型日历时间转化为字符串
struct tm {
int tm_sec; // seconds 0~59
int tm_min; // minutes 0~59
int tm_hour; // hours 0 ~ 23
int tm_mday; // day of the month 1~31
int tm_mon; // month 要加 1
int tm_year; // year 要加上1900
int tm_wday; // day of the week
int tm_yday; // day in the year
int tm_isdst; // daylight saving time
};
*/
char* timStr = NULL;
timStr = asctime(locTime);
printf("%s\n",timStr);
//
char s[32];
strftime(s,32,"%c",locTime);
puts(s);
}
结果: