05-Linux时间编程

一、时间有三种:

  1. 日历时间:
    • 日历时间(Calendar Time)是用”一个标准时间点(如1970年1月1日0点)到此时经过的秒数”来表示的时间。
  2. 格林威治时间:
    • Coordinated Universal Time(UTC)是世界标准时间,即常说的格林威治标准时间(Greenwich Mean Time,GMT)。
    • 【tip】:还记得本初子午线吗,没错就是这个时间。
      注:格林威治时间和本地时间不同
  3. 本地时间:
    • 根据时区的不同,在格林威治的时间上加减时区即可。例如:中国是东八区8点,格林威治的时间就是0点。

二、unix高级环境编程的函数


  • 1、time函数(日历时间):
    函数原型time_t time(time_t *t) ;
    功能time_t记录自1970年1月1日凌晨以来的秒数,在Linux/Unix上定义为long int类型,在32位系统上,time_t最多只能记录2,147,483,647秒,也就是说到了2038年将会产生溢出,但在64位系统上不会出现此问题。
    头文件#include 《time.h》
    参数t:如果不为空,保存返回值;如果为空,也可以。
    返回值成功:返回日历时间;失败:返回-1;
#include <time.h>
void main()
{
    int seconds=0;
    seconds = time((time_t *)NULL);
    printf("seconds=%d\n",seconds);
}

  • 2、gmtime函数
    函数原型struct tm *gmtime(const time_t *timep)
    功能将日历时间timep转化为格林威治标准时间,并保存在tm结构
    头文件#include 《time.h》
    参数timep:待转化的日历时间;
    返回值成功:返回时间标准时间,保存在tm结构中;

    失败:返回NULL;

struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };
#include <time.h>
#include <stdio.h>
int main(void)
{
   struct tm *local;
   time_t t;
   t = time(NULL); //获取日历时间
   local = localtime(&t); //将日历时间转化为本地时间,并保存在struct tm结构中
   printf("local hour is :%d\n",local->tm_hour);
   local = gmtime(&t); //将日历时间转化为格林威治时间,并保存在struct tm结构中
   printf("UTC hour is :%d\n",local->tm_hour);
   return 0;
}

  • 3、localtime函数
    函数原型struct tm* localtime(const time_t *timep)
    功能将日历时间转化为本地时间,并保存至tm结构
    头文件#include 《time.h》
    参数待转化的日历时间
    返回值成功:返回本地时间,存储在tm结构中;失败:返回NULL ;
struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };
#include 
#include 
int main(void)
{
   struct tm *local;
   time_t t;
   t = time(NULL); //获取日历时间
   local = localtime(&t); //将日历时间转化为本地时间,并保存在struct tm结构中
   printf("local hour is :%d\n",local->tm_hour);
   local = gmtime(&t); //将日历时间转化为格林威治时间,并保存在struct tm结构中
   printf("UTC hour is :%d\n",local->tm_hour);
   return 0;
}

  • 4、asctime函数( 字符串(格式化)时间 ):
    函数原型char *asctime(const struct tm *tm);
    功能asctime()将参数timeptr 所指的tm 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
    头文件#include 《time.h》
    参数待转化的日历时间;
    返回值成功:字符串显示的时间;失败:返回NULL;
#include <time.h>
main(){
    time_t timep;
    time (&timep);
    printf("%s", asctime(gmtime(&timep)));
}
  • 5、gettimeofday函数(高精度时间-微秒):
    函数原型int gettimeofday(struct timeval*tv,struct timezone *tz ) ;
    功能gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中。
    头文件#include 《sys\time.h》
    参数1、记录了1970年1月1日凌晨以来的秒数和微秒数,放在结构体timeval中: struct timeval{ long tv_sec;/*秒*/ long tv_usec;/*微妙*/ };

    2、timezone 结构定义为: struct timezone{ int tz_minuteswest;/*和greenwich 时间差了多少分钟*/ int tz_dsttime;/*type of DST correction*/ };

    3、在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。

    返回值成功:返回0;失败:返回-1;错误代码存于errno中。
    说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值。
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>

int main()
{
        struct  timeval    tv;
        struct  timezone   tz;
        gettimeofday(&tv,&tz);

        printf(“tv_sec:%d\n”,tv.tv_sec);
        printf(“tv_usec:%d\n”,tv.tv_usec);
        printf(“tz_minuteswest:%d\n”,tz.tz_minuteswest);
        printf(“tz_dsttime:%d\n”,tz.tz_dsttime);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值