标签:apue unix环境高级编程 time_t strftime localtime
typedef long time_t; /* 时间值time_t 为长整型的别名*/
#include <time.h>time_t time(time_t *calptr);Returns: value of time if OK,?1 on error
#include <time.h>int clock_getres(clockid_t clk_id, struct timespec *res);int clock_gettime(clockid_t clk_id, struct timespec *tp);int clock_settime(clockid_t clk_id, const struct timespec *tp);Link with -lrt.编译时使用链接库lrt
CLOCK_REALTIME:系统实时时间,使用此选项时功能相当于time(),如果系统时间被用户修改过,则对应的时间相应改变CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响CLOCK_PROCESS_CPUTIME_ID:本进程运行的CPU时间CLOCK_THREAD_CPUTIME_ID:本线程运行的CPU时间
struct timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */};
#include <sys/time.h>int gettimeofday(struct timeval *tv, struct timezone *tz);int settimeofday(const struct timeval *tv, const struct timezone *tz);

struct tm { /* a broken-down time */int tm_sec; // 秒 【0~60】 60存在的原因是闰秒的存在int tm_min; //分int tm_hour; //时int tm_mday; //日int tm_mon; //月int tm_year; //年 结构中记录的是从1900至今的年数int tm_wday; //星期几 【0~6】int tm_yday; // 一年中第几天int tm_isdst; // 夏时制标志};
#include <time.h>struct tm *gmtime(const time_t *calptr);struct tm *localtime(const time_t *calptr);Both return: pointer to broken-down time,NULLon error
#include <time.h>time_t mktime(struct tm *tmptr);Returns: calendar time if OK,?1 on error
#include<stdio.h>#include <time.h>#include<sys/time.h>int main(void){time_t calptr;if (time(&calptr) == -1){printf("Error: time() failed!\n");exit(0);}printf("Time(time_t) now is: %d\n",(long) calptr);/** 转换成可读到时间*/struct tm *gmptr; //国际标准时间struct tm *localptr; //本地时间/* 转换成国际标准时间 */printf("\n\n ----------gm time--------------\n");if (NULL == (gmptr = gmtime(&calptr))){printf("Error: can‘t convert time_t to struct tm by gmtime()!\n");exit(0);}printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", gmptr->tm_year, gmptr->tm_mon, gmptr->tm_mday, gmptr->tm_wday);printf("Hour:%d,\tMin:%d,\t,sec:%d\n", gmptr->tm_hour, gmptr->tm_min, gmptr->tm_sec);/* 转换成本地标准时间 */printf("\n\n ----------local time--------------\n");if (NULL == (localptr = localtime(&calptr))){printf("Error: can‘t convert time_t to struct tm by localtime()!\n");exit(0);}printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", localptr->tm_year, localptr->tm_mon, localptr->tm_mday, localptr->tm_wday);printf("Hour:%d,\tMin:%d,\t,sec:%d\n", localptr->tm_hour, localptr->tm_min, localptr->tm_sec);return 0;}
windeal@ubuntu:~/Windeal/apue$ ./exeTime(time_t) now is: 1409207607----------gm time--------------year:114, Month:7, Day:28, Week:4Hour:6, Min:33, ,sec:27----------local time--------------year:114, Month:7, Day:28, Week:4Hour:14, Min:33, ,sec:27
#include <time.h>size_t strftime(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr);size_t strftime_l(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr,locale_t locale);——Both return: number of characters stored in array if room, 0 otherwise
#include <stdio.h>#include <stdlib.h>#include <time.h>intmain(void){time_t t;struct tm *tmp;char buf1[16];char buf2[64];time(&t);tmp = localtime(&t);if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0)printf("buffer length 16 is too small\n");elseprintf("%s\n", buf1);if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0)printf("buffer length 64 is too small\n");elseprintf("%s\n", buf2);exit(0);}~
windeal@ubuntu:~/Windeal/apue$ ./exebuffer length 16 is too smalltime and date: 02:43:55 PM, Thu Aug 28, 2014
#include <time.h>char *strptime(const char *restrictbuf,const char *restrictformat,struct tm *restricttmptr);Returns: pointer to one character past last character parsed,NULLotherwise

typedef long time_t; /* 时间值time_t 为长整型的别名*/
#include <time.h>time_t time(time_t *calptr);Returns: value of time if OK,?1 on error
#include <time.h>int clock_getres(clockid_t clk_id, struct timespec *res);int clock_gettime(clockid_t clk_id, struct timespec *tp);int clock_settime(clockid_t clk_id, const struct timespec *tp);Link with -lrt.编译时使用链接库lrt
CLOCK_REALTIME:系统实时时间,使用此选项时功能相当于time(),如果系统时间被用户修改过,则对应的时间相应改变CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响CLOCK_PROCESS_CPUTIME_ID:本进程运行的CPU时间CLOCK_THREAD_CPUTIME_ID:本线程运行的CPU时间
struct timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */};
#include <sys/time.h>int gettimeofday(struct timeval *tv, struct timezone *tz);int settimeofday(const struct timeval *tv, const struct timezone *tz);

struct tm { /* a broken-down time */int tm_sec; // 秒 【0~60】 60存在的原因是闰秒的存在int tm_min; //分int tm_hour; //时int tm_mday; //日int tm_mon; //月int tm_year; //年 结构中记录的是从1900至今的年数int tm_wday; //星期几 【0~6】int tm_yday; // 一年中第几天int tm_isdst; // 夏时制标志};
#include <time.h>struct tm *gmtime(const time_t *calptr);struct tm *localtime(const time_t *calptr);Both return: pointer to broken-down time,NULLon error
#include <time.h>time_t mktime(struct tm *tmptr);Returns: calendar time if OK,?1 on error
#include<stdio.h>#include <time.h>#include<sys/time.h>int main(void){time_t calptr;if (time(&calptr) == -1){printf("Error: time() failed!\n");exit(0);}printf("Time(time_t) now is: %d\n",(long) calptr);/** 转换成可读到时间*/struct tm *gmptr; //国际标准时间struct tm *localptr; //本地时间/* 转换成国际标准时间 */printf("\n\n ----------gm time--------------\n");if (NULL == (gmptr = gmtime(&calptr))){printf("Error: can‘t convert time_t to struct tm by gmtime()!\n");exit(0);}printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", gmptr->tm_year, gmptr->tm_mon, gmptr->tm_mday, gmptr->tm_wday);printf("Hour:%d,\tMin:%d,\t,sec:%d\n", gmptr->tm_hour, gmptr->tm_min, gmptr->tm_sec);/* 转换成本地标准时间 */printf("\n\n ----------local time--------------\n");if (NULL == (localptr = localtime(&calptr))){printf("Error: can‘t convert time_t to struct tm by localtime()!\n");exit(0);}printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", localptr->tm_year, localptr->tm_mon, localptr->tm_mday, localptr->tm_wday);printf("Hour:%d,\tMin:%d,\t,sec:%d\n", localptr->tm_hour, localptr->tm_min, localptr->tm_sec);return 0;}
windeal@ubuntu:~/Windeal/apue$ ./exeTime(time_t) now is: 1409207607----------gm time--------------year:114, Month:7, Day:28, Week:4Hour:6, Min:33, ,sec:27----------local time--------------year:114, Month:7, Day:28, Week:4Hour:14, Min:33, ,sec:27
#include <time.h>size_t strftime(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr);size_t strftime_l(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr,locale_t locale);——Both return: number of characters stored in array if room, 0 otherwise
#include <stdio.h>#include <stdlib.h>#include <time.h>intmain(void){time_t t;struct tm *tmp;char buf1[16];char buf2[64];time(&t);tmp = localtime(&t);if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0)printf("buffer length 16 is too small\n");elseprintf("%s\n", buf1);if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0)printf("buffer length 64 is too small\n");elseprintf("%s\n", buf2);exit(0);}~
windeal@ubuntu:~/Windeal/apue$ ./exebuffer length 16 is too smalltime and date: 02:43:55 PM, Thu Aug 28, 2014
#include <time.h>char *strptime(const char *restrictbuf,const char *restrictformat,struct tm *restricttmptr);Returns: pointer to one character past last character parsed,NULLotherwise

标签:apue unix环境高级编程 time_t strftime localtime
原文地址:http://blog.csdn.net/windeal3203/article/details/38898067