C time

time_t和struct tm

1.time_t为typedef __int64 __time64_t; 
2.struct timeval
{
      uint tv_sec;
      uint tv.usec;
}

----------

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

---------

tm *gmtime(time_t * t);
time_t time(time_t *t);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer); 
把tm指针转换为time_t
time_t mktime(struct tm *timeptr);
localtime和gmtime的区别在于gmtime将时间转换为国际标准格式,也就是相对于1970 00:00:00开始的时间戳
而localtime是相对于本地的时区的格式。

------

size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const  struct tm *timeptr
);
参数说明:
我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向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 每周的第几天,星期一为第一天 (值从1到7,星期一为1)
%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"
intmain(void)
{
structtm* ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(&lt);
strftime(str,sizeof(str),"%Y/%m/%d %X %A 本年第%j天 %z",ptr);
printf("%s ",str);
return0;
}
原文地址:https://www.cnblogs.com/banwhui/p/4642220.html