zoukankan      html  css  js  c++  java
  • Linux之时间相关操作20170607

    一、Linux常用时间相关函数

             -asctime,ctime,getttimeofday,gmtime,localtime,mktime,settimeofday,time

    asctime             :将日期转换成字符串

    ctime                 :将秒数转换成字符串

    gettimeofday   :根据给定的与1970年1月1日相减得秒数,得到时间和日期

    gmtime             :根据给定的与1970年1月1日相减得秒数,取得时间和日期

    localtime           :根据给定的与1970年1月1日相减得秒数,取得当地的时区的时间和日期

    mktime              :将时间结构数据转换成经过的秒数

    settimeofday   :设置系统时间,只有root用户能运行

    time                   :此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现                                  在所经过的秒数

    1.asctime(将时间和日期以字符串格式表示)

    相关函数

        time,ctime,gmtime,localtime

    表头文件

        #include<time.h>

    定义函数

        char * asctime(const struct tm * timeptr);

    函数说明

        asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993/n”

    返回值

        若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与 ctime不同处在于传入的参数是不同的结构。

    附加说明

        返回一字符串表示目前当地的时间日期。

    范例

        #include <time.h>

    main()

    {

    time_t timep;

    time (&timep);

    printf(“%s”,asctime(gmtime(&timep)));

    }

    执行

        Sat Oct 28 02:10:06 2000

     

    2.ctime(将时间和日期以字符串格式表示)

    相关函数

        time,asctime,gmtime,localtime

    表头文件

        #include<time.h>

    定义函数

        char *ctime(const time_t *timep);

    函数说明

        ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 1993/n”。若再调用相关的时间日期函数,此字符串可能会被破坏。

    返回值

        返回一字符串表示目前当地的时间日期。

    范例

        #include<time.h>

    main()

    {

    time_t timep;

    time (&timep);

    printf(“%s”,ctime(&timep));

    }

    执行

        Sat Oct 28 10 : 12 : 05 2000

     

    3.gettimeofday(取得目前的时间)

    相关函数

        time,ctime,ftime,settimeofday

    表头文件

        #include <sys/time.h>

    #include <unistd.h>

    定义函数

        int gettimeofday ( struct timeval * tv , struct timezone * tz )

    函数说明

        gettimeofday() 会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

    timeval结构定义为:

    struct timeval{

    long tv_sec; /*秒*/

    long tv_usec; /*微秒*/

    };

    timezone 结构定义为:

    struct timezone{

    int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/

    int tz_dsttime; /*日光节约时间的状态*/

    };

    上述两个结构都定义在/usr /include/sys/time.h。tz_dsttime 所代表的状态如下

    DST_NONE /*不使用*/

    DST_USA /*美国*/

    DST_AUST /*澳洲*/

    DST_WET /*西欧*/

    DST_MET /*中欧*/

    DST_EET /*东欧*/

    DST_CAN /*加拿大*/

    DST_GB /*大不列颠*/

    DST_RUM /*罗马尼亚*/

    DST_TUR /*土耳其*/

    DST_AUSTALT /*澳洲(1986年以后)*/

    返回值

        成功则返回0,失败返回-1,错误代码存于errno。附加说明 EFAULT指针tv和tz所指的内存空间超出存取权限。

    范例

        #include<sys/time.h>

    #include<unistd.h>

    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);

    }

    执行

        tv_sec: 974857339

    tv_usec:136996

    tz_minuteswest:-540

    tz_dsttime:0

     

    4.gmtime(取得目前时间和日期)

    相关函数

        time,asctime,ctime,localtime

    表头文件

        #include<time.h>

    定义函数

        struct tm*gmtime(const time_t*timep);

    函数说明

        gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

    结构tm的定义为

    struct tm

    {

    int tm_sec;

    int tm_min;

    int tm_hour;

    int tm_mday;

    int tm_mon;

    int tm_year;

    int tm_wday;

    int tm_yday;

    int tm_isdst;

    };

    int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒

    int tm_min 代表目前分数,范围0-59

    int tm_hour 从午夜算起的时数,范围为0-23

    int tm_mday 目前月份的日数,范围01-31

    int tm_mon 代表目前月份,从一月算起,范围从0-11

    int tm_year 从1900 年算起至今的年数

    int tm_wday 一星期的日数,从星期一算起,范围为0-6

    int tm_yday 从今年1月1日算起至今的天数,范围为0-365

    int tm_isdst 日光节约时间的旗标

    此函数返回的时间日期未经时区转换,而是UTC时间。

    返回值

        返回结构tm代表目前 UTC 时间

    范例

        #include <time.h>

    main(){

    char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

    time_t timep;

    struct tm *p;

    time(&timep);

    p=gmtime(&timep);

    printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);

    printf(“%s%d;%d;%d/n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);

    }

    执行

        2000/10/28 Sat 8:15:38

     

    5.localtime(取得当地目前时间和日期)

    相关函数

        time, asctime, ctime, gmtime

    表头文件

        #include<time.h>

    定义函数

        struct tm *localtime(const time_t * timep);

    函数说明

        localtime() 将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考 gmtime()。此函数返回的时间日期已经转换成当地时区。

    返回值

        返回结构tm代表目前的当地时间。

    范例

        #include<time.h>

    main(){

    char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};

    time_t timep;

    struct tm *p;

    time(&timep);

    p=localtime(&timep); /*取得当地时间*/

    printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);

    printf(“%s%d:%d:%d/n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);

    }

    执行

        2000/10/28 Sat 11:12:22

     

    6.mktime(将时间结构数据转换成经过的秒数)

    相关函数

        time,asctime,gmtime,localtime

    表头文件

        #include<time.h>

    定义函数

        time_t mktime(strcut tm * timeptr);

    函数说明

        mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。

    返回值

        返回经过的秒数。

    范例

        /* 用time()取得时间(秒数),利用localtime()

    转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/

    #include<time.h>

    main()

    {

    time_t timep;

    strcut tm *p;

    time(&timep);

    printf(“time() : %d /n”,timep);

    p=localtime(&timep);

    timep = mktime(p);

    printf(“time()->localtime()->mktime():%d/n”,timep);

    }

    执行

        time():974943297

    time()->localtime()->mktime():974943297

     

    7.settimeofday(设置目前时间)

    相关函数

        time,ctime,ftime,gettimeofday

    表头文件

        #include<sys/time.h>

    #include<unistd.h>

    定义函数

        int settimeofday ( const struct timeval *tv,const struct timezone *tz);

    函数说明

        settimeofday() 会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。

    返回值

        成功则返回0,失败返回-1,错误代码存于errno。

    错误代码

        EPERM 并非由root权限调用settimeofday(),权限不够。

             EINVAL 时区或某个数据是不正确的,无法正确设置时间。

     

    8.time(取得目前的时间)

    相关函数

        ctime,ftime,gettimeofday

    表头文件

        #include<time.h>

    定义函数

        time_t time(time_t *t);

    函数说明

        此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。

    返回值

        成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于 errno中。

    范例

        #include<time.h>

    main()

    {

    int seconds= time((time_t*)NULL);

    printf(“%d/n”,seconds);

    }

    执行

        9.73E+08

    9.strftime() 函数将时间格式化

    我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:

    size_t strftime(

         char *strDest,

         size_t maxsize,

         const char *format,

         const struct tm *timeptr

    );

    我们可以根据format指向字符串中格式命令把timeptr中保 存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符 数。函 数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串 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 每周的第几天,星期一为第一天 (值从0到6,星期一为0)

    %U 第年的第几周,把星期日做为第一天(值从0到53)

    %V 每年的第几周,使用基于周的年

    %w 十进制表示的星期几(值从0到6,星期天为0)

    %W 每年的第几周,把星期一做为第一天(值从0到53)

    %x 标准的日期串

    %X 标准的时间串

    %y 不带世纪的十进制年份(值从0到99)

    %Y 带世纪部分的十制年份

    %z,%Z 时区名称,如果不能得到时区名称则返回空字符。

    %% 百分号

    如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:

    #i nclude “time.h”

    #i nclude “stdio.h”

    int main(void)

    {

    struct tm *ptr;

    time_t lt;

    char str[80];

    lt=time(NULL);

    ptr=localtime(<);

    strftime(str,100,"It is now %I %p",ptr);

    printf(str);

    return 0;

    }

    其运行结果为:

    It is now 4PM

    而下面的程序则显示当前的完整日期:

    #include

    #nclude

    void main( void )

    {

              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);

    }

    实例:

    LD_S32 SetSysTime(LD_S32 sysSec)

    {

             struct timeval tv_set;

             tv_set.tv_sec  = sysSec;

             tv_set.tv_usec = 0;

             if (settimeofday(&tv_set,NULL)<0)

             {

                       DebugPrintf("Set time to %d failed! ", sysSec);

                       return LD_FAILURE;

             }

             else

             {

                       DebugPrintf("Set time to %d success! ", sysSec);

                       return LD_SUCCESS;

             }

    }

    二、使用ntpclient同步时间

    ntpclient

    选项:

    -c count count时间计数后停止(默认为0意味直到永远)

    -d      打印诊断(功能可以在编译时关闭)

    -g 选项会导致程序NtpClient后得到的结果更准确,而不仅仅是(微秒,默认为0意味直到永远停止)

    -h 主机名(ip地址)(强制)NTP服务器,对系统时间来衡量

    -i 间隔时间 每隔一定时间检查时间(默认值为600)

    -I 尝试服务器使用adjtimex锁定本地时钟(2)

    -p 端口名  锁定本地NTP客户端UDP端口(默认为0表示“任何可用的”)

    -q 最小延时分钟 最小数据包的延迟交易(默认800微秒)

    -r 在标准输入重播分析代码

    -s 简单的时钟设置(相当于-c 1)

    -t 信任网络和服务器,没有RFC-4330推荐使用的检查

    同步时间例子:

    $ ntpclient  -s -t -i 10 -h 202.120.2.101

    实例:

    sprintf(buf, "ntpclient -o -s -c %d -i 15 -h %s", count, pNtpSrv);

    if(RunSysCmd2(buf, buf, sizeof(buf)))

    {

             DebugPrintf("run ntpclient command failed! ");

             return LD_FAILURE;

    }

  • 相关阅读:
    UI-iOS开发中@property的属性weak nonatomic strong readonly等介绍
    UIView之userInteractionEnabled属性介绍
    UI-target...action设计模式,手势识别器.UIimageview
    UI-事件处理
    IOS开发—事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
    UI-事件,触摸与响应者链(一)
    第47月第11天 iOS 文件下载Download,支持断点续传、后台下载、设置下载并发数
    第47月第10天 telnet rpm包安装
    第47月第5天 openresty
    第47月第4天 arkit录制
  • 原文地址:https://www.cnblogs.com/yuweifeng/p/6958362.html
Copyright © 2011-2022 走看看