strftime (对字符串进行格式化以表示系统日期和时间)

瑞兹 发表于 2020-12-10 09:13
浏览次数:
在手机上阅读

strftime()是 C函数里的.h库的一部分,它对字符串进行格式化以表示系统日期和时间。

查看英文版

目录

1 strftime 运行系统环境

2 strftime 描述

3 strftime 语法

4 strftime 例子

strftime 运行系统环境

Unix&Linux

strftime 描述

所述的strftime() 函数的格式毁掉的时间TM根据格式规范格式,并将结果在字符 数组 小号尺寸的最大值

的格式规格是一个空-终止 的字符串,并且可以包含被称为转换规范特殊字符序列,各由一个“引入 ”字符和终止由被称为转换指定字符某些其它字符。所有其他字符序列都是普通字符序列。

普通字符序列的字符(包括空字节)从 format 逐字复制到s

转换字符替换如下。

The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character array s of size max.

The format specification is a null-terminated string and may contain special character sequences called conversion specifications, each introduced by a '%' character and terminated by some other character known as a conversion specifier character. All other character sequences are ordinary character sequences.

The characters of ordinary character sequences (including the null byte) are copied verbatim from format to s.

Conversion characters are replaced as follows.

查看英文版

查看中文版

strftime 语法

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

转换字符

%a 根据当前语言环境的缩写的工作日名称。
%A 根据当前语言环境的完整工作日名称。
%b 根据当前语言环境的缩写月份名称。
%B 根据当前语言环境的完整月份名称。
%c 当前语言环境的首选日期和时间表示形式。
%C 世纪数字(年/ 100)为2位整数。
%d 以十进制数字表示的月份中的日期(范围为01到31)。
%D 等效于%m / %d / %y。对于美国人。美国人应该注意,在其他国家,%d / %m / %y相当普遍。在国际范围内,这种格式是模棱两可的,不应使用。
%e %d一样,将月中的日期作为十进制数字,但是前导零被空格代替。
%E 修饰符:使用其他格式,请参见下文。
%F 相当于%Y - %间%d(在ISO 8601日期格式)。
%G ISO 8601星期基准年(请参见“注释”),以世纪作为十进制数字。与ISO周编号相对应的4位数字的年份(请参见%V)。此转换的格式和值与%Y相同,不同的是,如果ISO周编号属于上一年或下一年,则使用该年份。
%G %G一样,但没有世纪,也就是说,具有两位数的年份(00-99)。
%H 等效于%b
%H 使用24小时制的小时(十进制数)(范围为00到23)。
%I 使用12小时制(范围为01至12)的小时作为十进制数字。
%j 一年中的天,以十进制数字表示(范围为001至366)。
%k 小时(24小时制)为十进制数字(范围为0到23);单个数字后跟一个空格。(另请参见%H。)
%l 小时(12小时制)为十进制数字(范围为1到12);单个数字后跟一个空格。(另请参见%I。)
%m 以十进制数字表示的月份(范围为01到12)。
%M 分钟,十进制数字(范围为00到59)。
%n 一个换行字符。
%O 修饰符:使用其他格式,请参见下文。
%p 根据给定的时间值,使用“ AM”或“ PM”,或者使用当前语言环境的相应字符串。中午被视为“ PM”,午夜被视为“ AM”。
%P 与%p类似,但小写:“ am”或“ pm”或当前语言环境的相应字符串。
%r 上午或下午表示法中的时间。在POSIX语言环境中,这等效于%I%M%S %p
%R 以24小时制表示的时间(%H%M)。(SU)有关包含秒数的版本,请参见下面的%T
%s 自1970年1月1日00:00:00 +0000(UTC)以来的秒数。
%S 第二个十进制数字(范围为00到60)。范围最大为60,以允许偶尔出现leap秒。
%t 一个选项卡字符。
%T 以24小时制表示的时间(%H%M%S)。
%u 以十进制表示的星期几,范围为1到7,星期一为1。另请参见%w
%U 当前年份的星期数,以十进制数表示,范围为00到53,从第一个星期日作为01周的第一天开始。另请参见%V%W
%V 当年的ISO 8601周编号(请参阅“注释”)为十进制数字,范围为01至53,其中第1周是新年中至少有4天的第一周。另请参见%U%W
%w 以十进制表示的星期几,范围为0到6,星期日为0。另请参见%u
%W 本年的星期数(以十进制数表示),范围为00到53,从第一个星期一开始(第01周的第一天)开始。
%x 当前语言环境的首选日期表示形式,没有时间。
%X 当前区域设置的首选时间表示形式,不带日期。
%y 年份,不带世纪的十进制数字(范围为00到99)。
%Y 以十进制数字表示的年份,包括世纪。
%z + hhmm或-hhmm数字时区(即距UTC的小时和分钟偏移量)。
%Z 时区或名称或缩写。
%+ 日期和时间,采用日期格式。
%% 文字“  ”字符。

可以通过在转换说明符前面加上EO修饰符来修改某些转换规范,以指示应使用其他格式。如果当前语言环境不存在替代格式或规范,则其行为将类似于使用未修改的转换规范。单一UNIX规范(SU)提到%Ec%EC%Ex%EX%Ey%EY%Od%Oe%OH%OI%Om%OM%OS%Ou%OU%OV%Ow%OW%Oy,其中O修饰符的作用是使用替代数字符号(例如,罗马数字),而E修饰符的作用是使用语言环境依赖的替代表示。

细分的时间结构tm中定义。另见ctime

返回值

所述的strftime()函数返回放置在阵列的字节数小号,不包括终止空字节,所提供的串,包括终止空字节,拟合。否则,它返回0,并且数组的内容是不确定的。由于至少libc 4.4.4;因此适用此行为。如果数组太小,非常老的libc版本(例如libc 4.4.1)将返回max。注意,返回值0不一定表示错误;返回值0不一定表示错误。例如,在许多语言环境中,%p会产生一个空字符串,该字符串等于零。

环境

使用环境变量TZLC_TIME

笔记

ISO 8601周日期 %G%g%V产量值是根据ISO 8601标准定义的基于周的年份计算得出的。在此系统中,周从星期一开始,从第一周的01到最后一周的5253进行编号。第1周是新年中四天或以上的第一周(或者,同义,第01周是:一年中的第一周,其中包含星期四;或者是其中包含1月4日的一周)。

当新的一年的第一个日历周的较少几天都在该年之内时,则基于ISO 8601周的系统会将这些天计为上一年的第53周的一部分。例如,2010年1月1日是星期五,这意味着该日历周中只有三天属于2010年。因此,基于ISO 8601周的系统将这些天视为2009年第53周(%V)的一部分(%G); ISO 8601年度2010的第01周从2010年1月4日星期一开始

Glibc笔记

Glibc为转换规范提供了一些扩展。在POSIX.1-2001中未指定这些扩展名,但是其他一些系统也提供了类似的功能。在'  '字符和转换说明符之间,可以指定可选的标志和字段宽度。它们在EO修饰符(如果有)之前。

允许使用以下标志字符:

_ (underscore)

用空格填充数字结果字符串。
-- (dash)

请勿填充数字结果字符串。
0 (zero)

将数字结果字符串填充为零,即使转换说明符字符默认情况下使用空格填充也是如此。
^ (carat)

将结果字符串中的字母字符转换为大写。
(hash)

交换结果字符串的大小写。

该标志仅适用于某些转换说明符,其中只有%Z才真正有用。

可选的十进制宽度说明符可以位于(可能不存在)标志之后。如果字段的自然大小小于此宽度,则将结果字符串填充(在左侧)为指定宽度。

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

Conversion Characters

%a The abbreviated weekday name according to the current locale.
%A The full weekday name according to the current locale.
%b The abbreviated month name according to the current locale.
%B The full month name according to the current locale.
%c The preferred date and time representation for the current locale.
%C The century number (year/100) as a 2-digit integer.
%d The day of the month as a decimal number (range 01 to 31).
%D Equivalent to %m/%d/%y. for Americans. Americans should note that in other countries %d/%m/%y is rather common. In the international context this format is ambiguous and should not be used.
%e Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space.
%E Modifier: use alternative format, see below.
%F Equivalent to %Y-%m-%d (the ISO 8601 date format).
%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 conversion 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.
%g Like %G, but without century, that is, with a 2-digit year (00-99).
%h Equivalent to %b.
%H The hour as a decimal number using a 24-hour clock (range 00 to 23).
%I The hour as a decimal number using a 12-hour clock (range 01 to 12).
%j The day of the year as a decimal number (range 001 to 366).
%k The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also %H.)
%l The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also %I.)
%m The month as a decimal number (range 01 to 12).
%M The minute as a decimal number (range 00 to 59).
%n newline character.
%O Modifier: use alternative format, see below.
%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".
%P Like %p but in lowercase: "am" or "pm" or a corresponding string for the current locale.
%r The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to %I:%M:%S %p.
%R The time in 24-hour notation (%H:%M). (SU) For a version including the seconds, see %T below.
%s The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
%S The second as a decimal number (range 00 to 60). The range is up to 60 to allow for occasional leap seconds.
%t tab character.
%T The time in 24-hour notation (%H:%M:%S).
%u The day of the week as a decimal, range 1 to 7, Monday being 1. See also %w.
%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.
%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.
%w The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
%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.
%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).
%Y The year as a decimal number including the century.
%z The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC).
%Z The timezone or name or abbreviation.
%+ The date and time in date format.
%% A literal '%' character.

Some conversion specifications can be modified by preceding the conversion specifier character by the E or O modifier to indicate that an alternative format should be used. If the alternative format or specification does not exist for the current locale, the behavior will be as if the unmodified conversion specification were used. The Single UNIX Specification (SU) mentions %Ec%EC%Ex%EX%Ey%EY%Od%Oe%OH%OI%Om%OM%OS%Ou%OU%OV%Ow%OW%Oy, where the effect of the O modifier is to use alternative numeric symbols (say, roman numerals), and that of the E modifier is to use a locale-dependent alternative representation.

The broken-down time structure tm is defined in . See also ctime

Return Value

The strftime() function returns the number of bytes placed in the array s, not including the terminating null byte, provided the string, including the terminating null byte, fits. Otherwise, it returns 0, and the contents of the array is undefined. This behavior applies since at least libc 4.4.4; very old versions of libc, such as libc 4.4.1, would return max if the array was too small. Note that the return value 0 does not necessarily indicate an error; for example, in many locales %p yields an empty string, which is equivalent to zero.

Environment

The environment variables TZ and LC_TIME are used.

Notes

ISO 8601 Week Dates %G%g, and %V yield values calculated from the week-based year defined by the ISO 8601 standard. In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week. Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday; or, the week that has 4 January in it).

When three of fewer days of the first calendar week of the new year fall within that year, then the ISO 8601 week-based system counts those days as part of week 53 of the preceding year. For example, 1 January 2010 is a Friday, meaning that just three days of that calendar week fall in 2010. Thus, the ISO 8601 week-based system considers these days to be part of week 53 (%V) of the year 2009 (%G) ; week 01 of ISO 8601 year 2010 starts on Monday, 4 January 2010.

Glibc Notes

Glibc provides some extensions for conversion specifications. These extensions are not specified in POSIX.1-2001, but a few other systems provide similar features. Between the '%' character and the conversion specifier character, an optional flag and field width may be specified. These precede the E or O modifiers, if present.

The following flag characters are permitted:

_ (underscore)

Pad a numeric result string with spaces.
- (dash)

Do not pad a numeric result string.
0 (zero)

Pad a numeric result string with zeros even if the conversion specifier character uses space-padding by default.
^ (carat)

Convert alphabetic characters in result string to upper case.
# (hash)

Swap the case of the result string.

This flag only works with certain conversion specifier characters, and of these, it is only really useful with %Z.

An optional decimal width specifier may follow the (possibly absent) flag. If the natural size of the field is smaller than this width, then the result string is padded (on the left) to the specified width.

查看英文版

查看中文版

strftime 例子

"%a, %b %d %l:%M.%S"

上面的字符串将格式化日期和时间,使其看起来像:星期六,七月13日1:32.59

这是一个使用strftime()解释这样的字符串的程序示例:

#include 
#include 
#include 
int main(int argc, char *argv[])
{
  char outstr[200];
  time_t t;
  struct tm *tmp;
  t = time(NULL);
  tmp = localtime(&t);
  if (tmp == NULL) {
  perror("localtime");
  exit(EXIT_FAILURE);
  }
   if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0)
   {
   fprintf(stderr, "strftime returned 0");
   exit(EXIT_FAILURE);
   }
   printf("Result string is \"%s\"\n", outstr);
   exit(EXIT_SUCCESS);
}

例如,如果上述程序被编译为可执行文件a.out,则可以从命令行使用它,如下所示:

./a.out '%m'

结果字符串为“11”

./a.out '%5m'

结果字符串为“00011”

./a.out '%_5m'

结果字符串为“11”

"%a, %b %d %l:%M.%S"

The above string would format the date and time to look like: Sat, Jul 13 1:32.59

Here's an example of a program that uses strftime() to interpret such a string:

#include 
#include 
#include 
int main(int argc, char *argv[])
{
  char outstr[200];
  time_t t;
  struct tm *tmp;
  t = time(NULL);
  tmp = localtime(&t);
  if (tmp == NULL) {
  perror("localtime");
  exit(EXIT_FAILURE);
  }
   if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0)
   {
   fprintf(stderr, "strftime returned 0");
   exit(EXIT_FAILURE);
   }
   printf("Result string is \"%s\"\n", outstr);
   exit(EXIT_SUCCESS);
}

For example, if the above program were compiled as the executable file a.out, it could be used from the command line like this:

./a.out '%m'

Result string is "11"

./a.out '%5m'

Result string is "00011"

./a.out '%_5m'

Result string is " 11"

查看英文版

查看中文版

其他命令行

sysklogd | s2p | service | sag | set | setenv | setfacl | sfdisk | sha224sum | sha256sum | sha384sum | sha512sum | shutdown | sleep | stat | startx | strip | swapoff | shred |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条