日期/时间可以采用两种格式:
>日历时间(a.k.a.simpletime) – 时间作为绝对值,通常是一些基准时间,通常称为协调世界时
>当地时间(a.k.a.细分时间) – 由年,月,日等组成的日历时间,其考虑当地时区,包括夏令时(如果适用).
数据类型
时间可以存储为整数或结构的实例:
>作为使用time_t算术类型的数字 – 将日历时间存储为自UNIX纪元1970年1月1日00:00:00以来经过的秒数
>使用结构timeval – 将日历时间存储为自UNIX纪元1970年1月1日00:00:00以来经过的秒数和纳秒数
>使用结构tm存储localtime,它包含如下属性:
tm_hour tm_min tm_isdst
上面的tm_isdst属性用于指示夏令时(DST).如果值为正,则为DST,如果值为0,则不是DST.
程序打印当前的协调世界时
#include <stdio.h> #include <time.h> int main ( int argc,char *argv[] ) { time_t Now; Now = time ( NULL ); printf ( "It’s %ld seconds since January 1,1970 00:00:00",(long) Now ); return 0; }
在上面的程序中,函数time读取UNIX系统时间,从1970年1月1日00:00:00(UNIX纪元)减去该值,并以秒为单位返回其结果.
程序打印当前本地时间
#include <stdio.h> #include <time.h> int main ( int argc,char *argv[] ) { time_t Now; struct tm *lcltime; Now = time ( NULL ); lcltime = localtime ( &Now ); printf ( "The time is %d:%d\n",lcltime->tm_hour,lcltime->tm_min ); return 0; }
在上面的程序中,函数localtime将从UNIX纪元开始的经过时间(以秒为单位)转换为细分时间. localtime读取UNIX环境TZ(通过调用tzset函数)返回相对于时区的时间并设置tm_isdst属性.
UNIX中TZ变量的典型设置(使用bash)如下:
export TZ=GMT
要么
export TZ=US/Eastern
程序打印当前格式化的格林威治标准时间
#include <stdio.h> #include <time.h> int main ( int argc,char *argv[] ) { time_t Now; struct tm *gmt; char formatted_gmt [50]; Now = time ( NULL ); gmt = gmtime ( &Now ); strftime ( formatted_gmt,sizeof(formatted_gmt),"%I:%M %p",gmt ); printf ( "The time is %s\n",formatted_gmt ); return 0; }
其他需要考虑的问题
原文地址:https://www.jb51.cc/bash/387036.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。