13.时间转换函数

time_t转换为可打印格式

1.ctime()

#include <time.h>
char *ctime(const time_t *timep);
	Returns pointer to statically allocated string terminated
		by newline and \0 on success, or NULL on error

会返回一个长达26字节的字符串,内含标准格式的日期和时间,
如:Thu Feb 10 23:11:07 2022
该字符串包含换行符和终止空字节各一。
ctime()函数在转换时,会自动对本地时区和DST设置加以考虑,返回的字符串经由静态分配,下一次对ctime()的调用会将其覆盖。

SUSv3标准规定,调用ctime()gmtime()localTime()asctime()中的任一函数,都可能会覆盖由其他函数的返回结果(静态分配的数据结构)。也就是说,这些函数可以共享返回的字符数组和tm结构体。

time_t和分解时间的转换

time_t转分解时间

函数gmtime()localtime()可将一time_t值转换为一个所谓分解时间(broken-down time)。分解时间被置于一个经由静态分配的结构中,其地址则作为函数结果返回。

#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
	Both returns a pointer to a statically allocated broken-down time
		structure on success, or NULL on error.

gmtime()把日历时间转换为对应于UTC的分解时间。(字母GM表示格林威治标准时间)。

相比之下,函数localtime()需要考虑时区和夏令时设置,返回对应于系统本地时间的一个分解时间。

tm结构
struct tm{
	int tm_sec;	/*Seconds (00-60)*/
	int tm_min;	/*Minutes (0-59)*/
	int tm_hour;	/*Hours (0-23)*/
	int tm_mday;	/*Day of the month (1-31)*/
	int tm_mon;	/*Month (0-11)*/
	int tm_year;	/*Year since 1900*/
	int tm_wday;	/*Day of the week (Sundat = 0)*/
	int tm_yday;	/*Day in the year (0-365; 1 Jan = 0)*/
	int tm_isdst;	/*Daylight saving time flag
									>0 :DST is effect
									=0 :DST is not effect
									<0 :DST infomation not available*/
};

分解时间转time_t

函数mktime()将一个本地时区的分解时间翻译为time_t值,并将其作为函数结果返回,调用者将分解时间置于一个tm结构,再以timeptr指针指向该结构。这一转换会忽略输入tm结构中的tm_wdaytm_yday字段。

#include <time.h>
time_t mktime(struct tm *timeptr);
	Returns  seconds since the Epoch corresponding to timeptr
		on success, or (time_t)-1 on error

函数mktime()可能会修改timeptr所指向的结构体,至少会确保对tm_mdaytm_wday字段值的设置,会与其它输入字段能对应起来

此外,mktime()不要求tm结构体的其他字段受到范围的限制,任何一个字段的值超出范围,mktime()会将其调整会有效范围之内,并适当地调整其他字段。所有这些调整,均发生于mktime()更新tm_mdaytm_wday字段并计算返回值time_t之前。

例如:
tm_sec为123,那么在返回此字段的值将为3,同时tm_min字段值会+2(如果导致tm_min溢出,则会继续调整,递增tm_hour的值,以此类推。)这些调整甚至适用于字段负值,例如,指定tm_sec为-1即意味着前一分钟的第59秒。

此功能允许以分解时间来计算日期和时间,故而非常有用。

分解时间和打印个时之间的转换

分解时间转打印格式

asctime()
#include <time.h>
char *asctime(const struct tm *timeptr);
	Returns pointer to statically allocated string terminated by newline
		and \0 on success, or NULL on error

相比于ctime(),本地时区设置对asctime()没有影响,因为其所转换的是一个分解时间。
asctime()可重入版本为asctime_r()

strftime()
#include <time.h>
size_t strftime(char *outstr, size_t maxsize, const char *format,
			    const struct tm *timrptr);
	Returns number of bytes placed in outstr (excluding terminating null
		byte) on success, or 0 on error.

format参数是一个个是字符串,详见strftime(3)手册页,这些转换说明符都符合SUSv3标准。

示例
#include <iostream>
#include <ctime>

using namespace std;

int main (void)
{
	time_t current = time (NULL);
	char buf[1024];
	strftime (buf, sizeof (buf),
		"%Y/%m/%d %H:%M:%S"
			,  localtime(&current));
	cout << buf;
}

运行结果:

2023/02/28 13:53:23
手册

感觉常用的:

  • %y/%Y
    • 年份(不含世纪 / 含世纪)
  • %m
    • 月份(01~12)
  • %d
    • 天数(所在月的天数)
  • %H / %I
    • 小时数( 24小时制(00 ~ 23) / 12小时制(01 ~ 12) )
  • %M
    • 分钟数(00 ~ 59)
  • %S
    • 秒数(00 ~ 60)
  • %s
    • 自epoch以来的秒数
%a     The  abbreviated  name of the day of the week according to the current locale.
       (Calculated from tm_wday.)

%A     The full name of the day of the week according to the current locale.  (Calcu‐
       lated from tm_wday.)

%b     The  abbreviated month name according to the current locale.  (Calculated from
       tm_mon.)

%B     The full month  name  according  to  the  current  locale.   (Calculated  from
       tm_mon.)

%c     The preferred date and time representation for the current locale.

%C     The  century  number  (year/100)  as  a 2-digit integer. (SU) (Calculated from
       tm_year.)

%d     The day of the month as a decimal number (range 01 to 31).   (Calculated  from
       tm_mday.)

%D     Equivalent  to  %m/%d/%y.   (Yecch—for  Americans only.  Americans should note
       that in other countries %d/%m/%y is rather common.  This means that in  inter‐
       national context this format is ambiguous and should not be used.) (SU)

%e     Like  %d,  the day of the month as a decimal number, but a leading zero is re‐
       placed by a space. (SU) (Calculated from tm_mday.)

%E     Modifier: use alternative format, see below. (SU)

%F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%G     The ISO 8601 week-based year (see NOTES) with century  as  a  decimal  number.
       The  4-digit year corresponding to the ISO week number (see %V).  This has the
       same format and value as %Y, except that if the ISO week number belongs to the
       previous  or  next  year,  that  year  is  used instead. (TZ) (Calculated from
       tm_year, tm_yday, and tm_wday.)

%g     Like %G, but without century, that is, with a 2-digit year (00–99). (TZ) (Cal‐
       culated from tm_year, tm_yday, and tm_wday.)

%h     Equivalent to %b.  (SU)

%H     The  hour as a decimal number using a 24-hour clock (range 00 to 23).  (Calcu‐
       lated from tm_hour.)

%I     The hour as a decimal number using a 12-hour clock (range 01 to 12).   (Calcu‐
       lated from tm_hour.)

%j     The  day of the year as a decimal number (range 001 to 366).  (Calculated from
       tm_yday.)

%k     The hour (24-hour clock) as a decimal number (range 0 to  23);  single  digits
       are preceded by a blank.  (See also %H.)  (Calculated from tm_hour.)  (TZ)

%l     The  hour  (12-hour  clock) as a decimal number (range 1 to 12); single digits
       are preceded by a blank.  (See also %I.)  (Calculated from tm_hour.)  (TZ)

%m     The month as a decimal number (range 01 to 12).  (Calculated from tm_mon.)

%M     The minute as a decimal number (range 00 to 59).  (Calculated from tm_min.)

%n     A newline character. (SU)

%O     Modifier: use alternative format, see below. (SU)

%p     Either "AM" or "PM" according to the given time value,  or  the  corresponding
       strings for the current locale.  Noon is treated as "PM" and midnight as "AM".
       (Calculated from tm_hour.)

%P     Like %p but in lowercase: "am" or "pm" or a corresponding string for the  cur‐
       rent locale.  (Calculated from tm_hour.)  (GNU)

%r     The  time in a.m. or p.m. notation.  In the POSIX locale this is equivalent to
       %I:%M:%S %p.  (SU)

%R     The time in 24-hour notation (%H:%M).  (SU) For a version including  the  sec‐
       onds, see %T below.

%s     The  number  of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
       (Calculated from mktime(tm).)

%S     The second as a decimal number (range 00 to 60).  (The range is up  to  60  to
       allow for occasional leap seconds.)  (Calculated from tm_sec.)

%t     A tab character. (SU)

%T     The time in 24-hour notation (%H:%M:%S).  (SU)

%u     The  day of the week as a decimal, range 1 to 7, Monday being 1.  See also %w.
       (Calculated from tm_wday.)  (SU)

%U     The week number of the current year as a  decimal  number,  range  00  to  53,
       starting  with  the first Sunday as the first day of week 01.  See also %V and
       %W.  (Calculated from tm_yday and tm_wday.)

%V     The ISO 8601 week number (see NOTES) of the current year as a decimal  number,
       range 01 to 53, where week 1 is the first week that has at least 4 days in the
       new year.  See also  %U  and  %W.   (Calculated  from  tm_year,  tm_yday,  and
       tm_wday.)  (SU)

%w     The  day of the week as a decimal, range 0 to 6, Sunday being 0.  See also %u.
       (Calculated from tm_wday.)

%W     The week number of the current year as a  decimal  number,  range  00  to  53,
       starting  with the first Monday as the first day of week 01.  (Calculated from
       tm_yday and tm_wday.)

%x     The preferred date representation for the current locale without the time.

%X     The preferred time representation for the current locale without the date.

%y     The year as a decimal number without a century (range 00 to 99).   (Calculated
       from tm_year)

%Y     The year as a decimal number including the century.  (Calculated from tm_year)

%z     The  +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from
       UTC). (SU)

%Z     The timezone name or abbreviation.

%+     The date and time in date(1) format. (TZ) (Not supported in glibc2.)

%%     A literal '%' character.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

barbyQAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值