zoukankan      html  css  js  c++  java
  • Linux时间编程

    格林尼治标准时间
    格林尼治标准时间(Greenwich Mean Time, GMT)即世界时(Universal Time, UT),是以地球自转为基础的时间计量系统,由于地球每天的自转不规则,而且正在减速,因此标准时间由原子钟报时的协调世界时(Coordinated Universal Time, UTC)取代。在不需要精确到秒的情况下,通常也将GMT和UTC视作等同。

    日历时间
    日历时间(Calendar Time)是用“一个标准时间点(如1970年1月1日0点0分0秒)到此时经过的秒数”来表示的时间。

    1.Shell命令

    time 常用于测量一个命令的执行时间。
    date 显示或设定系统时间和日期。

    2.系统调用

    time_t time(time_t *t);
    

    返回从 1970-01-01 00:00:00 UTC 到现在所经历的秒数。
    t 不为NULL,返回值也保存到 t 所指的内存中。

    struct timeval {
    	time_t      tv_sec;     /* seconds */
    	suseconds_t tv_usec;    /* microseconds */
    };
    struct timezone {
    	int tz_minuteswest;     /* minutes west of Greenwich 格林尼治时间往西相差的分钟数,往东为负值 */
    	int tz_dsttime;         /* type of DST correction  DST时间的修正方式 */
    };
    int gettimeofday(struct timeval *tv, struct timezone *tz);
    int settimeofday(const struct timeval *tv, const struct timezone *tz);
    

    获取/设置时间。
    tv为时间,精确到微秒;struct timezone结构已经废弃,tz一般为NULL。

    3.C库调用

    struct tm {
    	int tm_sec;        /* The seconds, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds. */
    	int tm_min;        /* The minutes, in the range 0 to 59. */
    	int tm_hour;       /* The hours, in the range 0 to 23. */
    	int tm_mday;       /* The day of the month, in the range 1 to 31. */
    	int tm_mon;        /* The number of months since January, in the range 0 to 11. */
    	int tm_year;       /* The number of years since 1900. */
    	int tm_wday;       /* The number of days since Sunday, in the range 0 to 6. */
    	int tm_yday;       /* The number of days since January 1, in the range 0 to 365. */
    	int tm_isdst;      /* daylight saving time */
    };
    

    tm_isdst
    A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available.

    char *asctime(const struct tm *tm);
    char *asctime_r(const struct tm *tm, char *buf);
    
    char *ctime(const time_t *timep);
    char *ctime_r(const time_t *timep, char *buf);
    

    上述两组函数都是把时间转换为字符串格式,如"Wed Jun 30 21:49:08 1993 ",只不过两组函数所需要的参数类型不同。函数的返回值保存在一个静态分配的字符串中,其值可能被随后的任何日期和时间函数调用覆盖。
    asctime_r和ctime_r多了一个参数buf,用于保存转换后的字符串。而asctime和ctime函数的返回值应该立即处理,否则可能被覆盖。

    struct tm *gmtime(const time_t *timep);
    struct tm *gmtime_r(const time_t *timep, struct tm *result);
    
    struct tm *localtime(const time_t *timep);
    struct tm *localtime_r(const time_t *timep, struct tm *result);
    

    上述两组函数都是将日历时间转换为比较直观的struct tm结构,所不同的是,gmtime采用0时区进行转换,localtime采用当前时区进行转换,所以这两个函数在非0时区返回的结构中tm_hour的值是不同的。函数的返回值保存在一个静态分配的结构体中,其值可能被随后的任何日期和时间函数调用覆盖。关于这两组函数的详细分析见http://blog.csdn.net/csuwzc/article/details/6912104
    gmtime_r和localtime_r的参数result用于保存转换后的结构体,函数处理的结果可以稍后处理而不会被覆盖。

    time_t mktime(struct tm *tm);
    

    将 struct tm结构的时间转换为日历时间。

    clock_t clock(void);
    

    返回从程序启动到该函数被调用所占用CPU的时间,返回值为CPU时钟计时单元(clock tick)数,换算成秒需要除以CLOCKS_PER_SEC或CLK_TCK。32位系统上CLOCK_PER_SEC等于1000000,大约每隔72分钟该函数就会返回相同的值。

    struct timeb {
        time_t         time;        /* Epoch至今的秒数 */
        unsigned short millitm;     /* 微秒数 */
        short          timezone;    /* 时区,用格林尼治时间往西相差的分钟数表示,东时区为负值 */
        short          dstflag;     /* 阳光节约时 */
    };
    int ftime(struct timeb *tp);
    

    将当前时间存储到 tp 所指的结构中。

    size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
    

    格式化日期和时间。将 tm 所指的结构化的时间转化为format字符串指定的格式,转换后的字符串保存在 s 所指的内存中,字符串的最大长度由max控制。

    char *strptime(const char *s, const char *format, struct tm *tm);
    

    将format指定格式的时间字符串 s 转换为struct tm结构。

    void tzset (void);
    extern char *tzname[2];
    

    设置时区。将环境变量 TZ 赋给全局变量tzname,即从环境变量取得本地时区。该函数被其他依赖时区的时间转换函数自动调用。若 TZ 未设置,全局变量tzname会依据/etc/localtime找出最接近当地的时区,如 TZ 的值为空或无法识别,则使用UTC时区。

  • 相关阅读:
    LeetCode-Pascal's Triangle
    macOS Sierra 10.12版本 显示隐藏文件
    Android主页导航:fragment+viewpager
    (转)Android工程出现 java.lang.NoClassDefFoundError错误解决方法
    Android Studio安装以及Fetching android sdk component information超时的解决方案
    Android项目使用Ant多渠道打包(最新sdk)
    eclipse或Myeclipse中web项目没有run on server时怎么办?
    mysq查询语句包含中文以及中文乱码,字符集 GBK、GB2312、UTF8的区别
    论HTML的div、span和label的区别
    斐波那契数列
  • 原文地址:https://www.cnblogs.com/chenbeibei/p/5008537.html
Copyright © 2011-2022 走看看