C/C++语言函数学习(5)time.h
来源:互联网 发布:天津广电网络机顶盒 编辑:程序博客网 时间:2024/06/05 11:50
//http://blog.csdn.net/NoExcuse/article/details/1843270
Used by other time functions.
struct tm
{
int tm_sec; /* Seconds.[0-60] (1 leap second) */
int tm_min; /* Minutes.[0-59] */
int tm_hour; /* Hours.[0-23] */
int tm_mday; /* Day.[1-31] */
int tm_mon; /* Month.[0-11] */
int tm_year; /* Year- 1900. */
int tm_wday; /* Day of week.[0-6] */
int tm_yday; /* Days in year.[0-365]*/
int tm_isdst; /* DST.[-1/0/1]*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
//获得日历时间
time_t time(time_t * timer);
//将日历时间转化为世界标准时间(即格林尼治时间)
struct tm * gmtime(const time_t *timer);
//转化为本地时间
struct tm * localtime(const time_t * timer);
//转换为日历时间
time_t mktime(struct tm * timeptr);
//转化为字符串,固定的时间格式
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
//根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,
//最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);
格式列表如下:
%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 时区名称,如果不能得到时区名称则返回空字符。
The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and hence are not thread-safe. The thread-safe versions, asctime_r(), ctime_r(), gmtime_r() and localtime_r(), are specified by SUSv2.
在内部实现中,gmtime()、localtime()函数返回内部的同一个全局变量,在多线程中使用localtime(),gmtime()函数式不安全的.
同样的asctime(), ctime()返回的也是同一个全局变量。
同样的测试可以对,time_t now用localtime(),gmtime()转换后的结构是一样的。
Used by other time functions.
struct tm
{
int tm_sec; /* Seconds.[0-60] (1 leap second) */
int tm_min; /* Minutes.[0-59] */
int tm_hour; /* Hours.[0-23] */
int tm_mday; /* Day.[1-31] */
int tm_mon; /* Month.[0-11] */
int tm_year; /* Year- 1900. */
int tm_wday; /* Day of week.[0-6] */
int tm_yday; /* Days in year.[0-365]*/
int tm_isdst; /* DST.[-1/0/1]*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
//获得日历时间
time_t time(time_t * timer);
//将日历时间转化为世界标准时间(即格林尼治时间)
struct tm * gmtime(const time_t *timer);
//转化为本地时间
struct tm * localtime(const time_t * timer);
//转换为日历时间
time_t mktime(struct tm * timeptr);
//转化为字符串,固定的时间格式
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
//根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,
//最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);
格式列表如下:
%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 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
#include <time.h>#include <stdio.h>int main(int argc, char *argv[]){clock_t start,finish;long i = 1000000L;char str[100];struct tm *local;struct tm *gm;time_t seconds ;time(&seconds);//printf("%s",ctime(seconds));start = clock();while(i--);finish = clock();printf("%f\n",(double)(finish - start)/CLOCKS_PER_SEC);printf("%d\n",seconds);local = localtime(&seconds);gm = gmtime(&seconds);printf("%d\n",local->tm_hour);printf("%d\n",gm->tm_hour);printf(asctime(local));printf(ctime(&seconds));strftime(str,100,"It is %R %p\n",local);printf(str);return 0;}
The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and hence are not thread-safe. The thread-safe versions, asctime_r(), ctime_r(), gmtime_r() and localtime_r(), are specified by SUSv2.
在内部实现中,gmtime()、localtime()函数返回内部的同一个全局变量,在多线程中使用localtime(),gmtime()函数式不安全的.
同样的asctime(), ctime()返回的也是同一个全局变量。
在使用中尽量使用其对应的线程安全函数asctime_r(), ctime_r(), gmtime_r() and localtime_r()
#include <stdio.h>#include <time.h>int main(int argc, char *argv[]){time_t now = time(NULL);time_t now_3 = now + 120;struct tm *tm_now = localtime(&now);struct tm *tm_now_3 = localtime(&now_3);printf("tm_now: %d %02d %02d %02d:%02d:%02d\n", \tm_now->tm_year+1900, tm_now->tm_mon, tm_now->tm_mday,\tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);printf("tm_now_3: %d %02d %02d %02d:%02d:%02d\n", \tm_now_3->tm_year+1900, tm_now_3->tm_mon, tm_now_3->tm_mday, \tm_now_3->tm_hour, tm_now_3->tm_min, tm_now_3->tm_sec);struct tm rtm_now = {0};struct tm rtm_now_3 = {0};struct tm rgm_now = {0};struct tm rgm_now_3 = {0};char tmTmp[50] = {0};localtime_r(&now, &rtm_now);localtime_r(&now_3, &rtm_now_3);gmtime_r(&now, &rgm_now);gmtime_r(&now_3, &rgm_now_3);strftime(tmTmp,50,"%H:%M:%S",&rtm_now);printf("rtm_now\t%s\n", tmTmp);strftime(tmTmp,50,"%H:%M:%S",&rtm_now_3);printf("rtm_now_3\t%s\n", tmTmp);strftime(tmTmp,50,"%H:%M:%S",&rgm_now);printf("rgm_now\t%s\n", tmTmp);strftime(tmTmp,50,"%H:%M:%S",&rgm_now_3);printf("rgm_now_3\t%s\n", tmTmp);return 0;}测试结果:
tm_now: 2014 09 14 12:36:44tm_now_3: 2014 09 14 12:36:44rtm_now12:34:44rtm_now_312:36:44rgm_now04:34:44rgm_now_304:36:44
同样的测试可以对,time_t now用localtime(),gmtime()转换后的结构是一样的。
- C/C++语言函数学习(5)time.h
- c语言 time.h
- C语言-time.h
- C语言 <time.h>
- C语言time.h学习笔记
- C语言time.h库学习
- C语言time.h函数库
- C语言time.h函数库
- C语言time.h函数库
- C语言time库(#include<time.h>)
- c中time.h函数
- C语言time.h中clock()函数的使用
- C语言time.h中clock()函数的使用
- C语言Time函数
- C语言time函数
- C语言函数--H
- C语言中的time.h 文件的全面学习(转)
- 趣学 C 语言 (一)—— time.h
- 怀念过去的伤感爱情日志:某一天你是否记得我
- ggplot2 学习笔记
- 20个代码生成框架
- git 管理详解
- Oracle常用性能监控语句解析
- C/C++语言函数学习(5)time.h
- 多个DIV之间不换行
- 蝶舞的伤感心情日志:天灰黑,人会变,三分情,七分骗
- oracle 后台进程(background process)详解
- WebService学习笔记(一)
- hibernate配置多数据源
- 八.Sql server中intersect, minus的用法
- 浅谈java内存机制
- Reverse Nodes in k-Group