strptimeのヘルプ・マニュアル
日本語 英語
strptime --help
man strptime
STRPTIME(3) Linux Programmer’s Manual STRPTIME(3)
名前
strptime - 文字列であらわされている時間を tm 構造体の時間に変換する
書式
#define _XOPEN_SOURCE /* glibc2 では、これが必要。 */
#include
char *strptime(const char *s, const char *format, struct tm *tm);
説明
strptime() 関数は strftime(3) の逆関数であり、ポインタ s が指す文字列を
時間に変換する。変換の際には format で指定したフォーマットを用い、値 は
ポ インタ tm が指す tm 構造体に収められる。 format は、 scanf(3) で使わ
れているような、フィールドディスクリプターとテキスト文字で構成されて い
る文字列である。個々のフィールドディスクリプターは % とそれに続く文字か
らなり、後者にフィールドディスクリプターを置き換える内容を指定 す る 。
format 文字列中の他の全ての文字には、入力文字列にマッチする文字がなけれ
ばならない。フォーマット文字列中にある空白は例外であり、入力文字列中 の
0 個以上の空白とマッチする。 2 つのフィールドディスクリプターの間には、
空白・英字・数字がなければならない。
strptime() 関数は、入力文字列を左から右へ処理する。入力された 3 つの 要
素 (空白・文字・フォーマット) は、順に処理される。入力がフォーマット文
字列とマッチできない場合、関数は停止する。残りのフォーマット文字列と 入
力文字列は処理されない。
使 用可能なフィールドディスクリプターを以下に挙げる。 (曜日や月の名前と
いった) 文字列をマッチさせる場合、大文字と小文字は区別せずに比較する 。
数をマッチさせる場合、前に 0 をつけても構わないが必ずしも必要ではない。
%% 文字としての %。
%a または %A
現在のロケールでの曜日名 (省略名または完全な名前)。
%b または %B または %h
現在のロケールでの月名 (省略名または完全な名前)。
%c 現在のロケールでの日付と時刻の表現。
%C 1 世紀中の年 (0-99)。
%d または %e
月内の日付 (1-31)。
%D 日付。 %m/%d/%y と同じ。 (これはアメリカ式の日付形式で、ヨーロッ
パでは特に %d/%m/%y という形式が広く使われているために、アメリカ
人以外には紛らわしく感じられる。 ISO 8601 規格では %Y-%m-%d とい
う形式である。)
%H 時間 (0-23)。
%I 12 時間制での時間 (1-12)。
%j 年の初めからの通算の日付 (1-366)。
%m 数字表現の月 (1-12)。
%M 分 (0-59)。
%n 任意の空白。
%p ロケールの AM (午前) と PM (午後) に対応するもの。 (注意: 対応す
るものがないかもしれない。)
%r (ロケールの AM と PM を使った) 12 時間制の時間。 POSIX ロケー ル
では %I:%M:%S %p と同じ。現在のロケールにおいて LC_TIME パートの
t_fmt_ampm が定義されていない場合、動作は未定義である。
%R %H:%M と同じ。
%S 秒 (0-60; 60 は閏秒を示す。以前は 61 も指定できた)。
%t 任意の空白。
%T %H:%M:%S と同じ。
%U 日曜日を週の始まりとした年通算での週数 (0-53)。 1 月の最初の日曜
日を第 1 週目の最初の日する。
%w 日曜日を 0 とした数字表記の曜日 (0-6)。
%W 月曜日を週の始まりとした年通算での週数 (0-53)。 1 月の最初の月曜
日を第 1 週目の最初の日する。
%x 日付。ロケールの日付フォーマットを使う。
%X 時刻。ロケールの時刻フォーマットを使う。
%y 1 世紀中の年 (0-99)。世紀が指定されない場合、値が 69-99 の範囲の
と きは 20 世紀の年 (1969-1999)、値が 00-68 の範囲のときは 21 世
紀の年 (2000-2068) とする。
%Y 年。世紀の部分を含む (例: 1991)。
E や O という修正子を使うことで変更できるフィールドディスクリプタもある
。 これらの修正子は、別のフォーマットや仕様を使うことを指示する。別のフ
ォーマットや仕様が現在のロケールに存在しないときは、変更していないフ ィ
ールドディスクリプタが使われる。
E 修正子は、ロケールに依存した日付と時刻の別の表現形式が入力文字列に含
まれていることを指定する。
%Ec 日付と時刻。ロケールに依存した別の表現形式を使う。
%EC 基準年 (期間) の名前。ロケールに依存した別の表現形式を使う。
%Ex 日付。ロケールに依存した別の表現形式を使う。
%EX 時刻。ロケールに依存した別の表現形式を使う。
%Ey %EC (年のみ) からのオフセット。ロケールに依存した別の表現形式 を
使う。
%EY 完全な形式の年。別の表現型式を使う。
O 修正子は、ロケールに依存した別のフォーマットの中に数値の入力があるこ
とを指定する。
%Od または %Oe
月の初めからの通算の日付。ロケールに依存した別の数値シンボルを使
う。 0 を頭につけてもよいが、必須ではない。
%OH 時間 (24 時間制)。ロケールに依存した別の数値シンボルを使う。
%OI 時間 (12 時間制)。ロケールに依存した別の数値シンボルを使う。
%Om 月。ロケールに依存した別の数値シンボルを使う。
%OM 分。ロケールに依存した別の数値シンボルを使う。
%OS 秒。ロケールに依存した別の数値シンボルを使う。
%OU 年の初めからの通算の週数 (日曜日を週の始めとする)。ロケールに依
存した別の数値シンボルを使う。
%Ow 数値表記の曜日 (日曜日を 0 とする)。ロケールに依存した別の数値シ
ンボルを使う。
%OW 年の初めからの通算の週数 (月曜日を週の始めとする)。ロケールに依
存した別の数値シンボルを使う。
%Oy 年 (%C からのオフセット)。ロケールに依存した別の数値シンボルを使
う。
要素別の時刻構造体 tm は 内で以下の様に定義されている。
struct tm {
int tm_sec; /* 秒 */
int tm_min; /* 分 */
int tm_hour; /* 時間 */
int tm_mday; /* 日 */
int tm_mon; /* 月 */
int tm_year; /* 年 */
int tm_wday; /* 曜日 */
int tm_yday; /* 年内通算日 */
int tm_isdst; /* 夏時間 */
};
返り値
こ の関数の返り値は、関数の中で処理されなかった最初の文字へのポインタで
ある。フォーマット文字列が必要する以上の文字が入力文字列に含まれてい る
場 合、返り値は最後に処理された入力文字の次の文字を指す。すべての入力文
字列が処理された場合、返り値は文字列末尾の NULL バイトを指 す 。 strp-
time() がフォーマット文字列のすべての比較に失敗し、エラーが起こった場合
、関数は NULL ポインタを返す。
準拠
SUSv2, POSIX.1-2001.
注意
原則として、この関数は tm の初期化はせずに、指定された値のみを入れる 。
つ ま り 、この関数の呼び出しの前に tm を初期化しなければならない。他の
Unix システムとは、細かい点で異なる。 glibc の実装では、明示的に指定 さ
れ ないフィールドは変更されない。例外として、年・月・日のいずれかの要素
が変更された場合に tm_wday と tm_yday が再計算される。
この関数は、libc 4.6.8 以降で使用できる。 Linux の libc4 と libc5 の イ
ン クルードファイルは、この関数のプロトタイプを常に定義する。 glibc2 の
インクルードファイルは、 _XOPEN_SOURCE または _GNU_SOURCE が定義され た
場合のみ、この関数のプロトタイプを提供する。
libc 5.4.13 より前では、空白 (と 'n' または 't' 指定) は扱われなかった
。ロケールの修正子 'E' と 'O' は受け付けられなかった。また、'C' の指 定
は 'c' の指定と同じ意味で扱われた。
'y' (1 世紀中の年) の指定は、libc4 と libc5 では 20 世紀の年として解釈
される。 glibc 2.0 では 1950-2049 の範囲として解釈される。 glibc 2.1 か
らは 1969-2068 の範囲として解釈される。
glibc での注意
対象性のために、glibc では strptime() に strftime(3) と同じフォーマット
文字をサポートさせようとしている。多くの場合、対応するフィールドが解 釈
さ れるが、 tm フィールドは変更されない。使用可能なフォーマット文字を以
下に示す。
%F %Y-%m-%d と同じ。ISO 8601 の日付形式。
%g ISO 週数に対応した西暦年。世紀は含まず (0-99) の範囲。
%G ISO 週数に対応した西暦年 (例えば 1991)。
%u 10 進数表記の曜日 (1-7 で月曜日を 1 とする)。
%V ISO 8601:1988 形式での年通算の 10 進数表記での週数 (1-53)。 1 月
1 日を含む (月曜日から始まる) 週に 4 日以上が含まれている場合は
、その週を第 1 週とする。 3 日以下しか含まれていない場合は、1 月
1 日を含む週を前年の最終の週として、次の週を第 1 週とする。
%z RFC-822/ISO 8601 標準タイムゾーンを指定する。
%Z タイムゾーン名。
同 様に、 strftime(3) の GNU 版での拡張に対応するために、 %k は %H、 %P
は %p と等価に扱われる。また、 %l は %I と等価に扱われるようになるは ず
である。
さらに以下も定義されている。
%s 紀元年 (例えば 1970-01-01 00:00:00 UTC) からの通算の秒数。閏秒が
サポートされていない限り、閏秒はカウントしない。
glibc における実装では、2 つのフィールド間の空白は必要ない。
例
以下の例は strptime() と strftime(3) の使用法を示している。
#define _XOPEN_SOURCE
#include
#include
#include
int
main(void)
{
struct tm tm;
char buf[255];
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
puts(buf);
exit(EXIT_SUCCESS);
}
関連項目
time(2), getdate(3), scanf(3), setlocale(3), strftime(3), fea-
ture_test_macros(7)
GNU 2001-11-12 STRPTIME(3)
STRPTIME(3) Linux Programmer’s Manual STRPTIME(3)
NAME
strptime - convert a string representation of time to a time tm struc-
ture
SYNOPSIS
#define _XOPEN_SOURCE /* glibc2 needs this */
#include
char *strptime(const char *s, const char *format, struct tm *tm);
DESCRIPTION
The strptime() function is the converse function to strftime(3) and
converts the character string pointed to by s to values which are
stored in the tm structure pointed to by tm, using the format specified
by format. Here format is a character string that consists of field
descriptors and text characters, reminiscent of scanf(3). Each field
descriptor consists of a % character followed by another character that
specifies the replacement for the field descriptor. All other charac-
ters in the format string must have a matching character in the input
string, except for whitespace, which matches zero or more whitespace
characters in the input string. There should be whitespace or other
alphanumeric characters between any two field descriptors.
The strptime() function processes the input string from left to right.
Each of the three possible input elements (whitespace, literal, or for-
mat) are handled one after the other. If the input cannot be matched
to the format string the function stops. The remainder of the format
and input strings are not processed.
The supported input field descriptors are listed below. In case a text
string (such as a weekday or month name) is to be matched, the compari-
son is case insensitive. In case a number is to be matched, leading
zeros are permitted but not required.
%% The % character.
%a or %A
The weekday name according to the current locale, in abbreviated
form or the full name.
%b or %B or %h
The month name according to the current locale, in abbreviated
form or the full name.
%c The date and time representation for the current locale.
%C The century number (0-99).
%d or %e
The day of month (1-31).
%D Equivalent to %m/%d/%y. (This is the American style date, very
confusing to non-Americans, especially since %d/%m/%y is widely
used in Europe. The ISO 8601 standard format is %Y-%m-%d.)
%H The hour (0-23).
%I The hour on a 12-hour clock (1-12).
%j The day number in the year (1-366).
%m The month number (1-12).
%M The minute (0-59).
%n Arbitrary whitespace.
%p The locale’s equivalent of AM or PM. (Note: there may be none.)
%r The 12-hour clock time (using the locale’s AM or PM). In the
POSIX locale equivalent to %I:%M:%S %p. If t_fmt_ampm is empty
in the LC_TIME part of the current locale then the behavior is
undefined.
%R Equivalent to %H:%M.
%S The second (0-60; 60 may occur for leap seconds; earlier also 61
was allowed).
%t Arbitrary whitespace.
%T Equivalent to %H:%M:%S.
%U The week number with Sunday the first day of the week (0-53).
The first Sunday of January is the first day of week 1.
%w The weekday number (0-6) with Sunday = 0.
%W The week number with Monday the first day of the week (0-53).
The first Monday of January is the first day of week 1.
%x The date, using the locale’s date format.
%X The time, using the locale’s time format.
%y The year within century (0-99). When a century is not otherwise
specified, values in the range 69-99 refer to years in the twen-
tieth century (1969-1999); values in the range 00-68 refer to
years in the twenty-first century (2000-2068).
%Y The year, including century (for example, 1991).
Some field descriptors can be modified by the E or O modifier charac-
ters to indicate that an alternative format or specification should be
used. If the alternative format or specification does not exist in the
current locale, the unmodified field descriptor is used.
The E modifier specifies that the input string may contain alternative
locale-dependent versions of the date and time representation:
%Ec The locale’s alternative date and time representation.
%EC The name of the base year (period) in the locale’s alternative
representation.
%Ex The locale’s alternative date representation.
%EX The locale’s alternative time representation.
%Ey The offset from %EC (year only) in the locale’s alternative rep-
resentation.
%EY The full alternative year representation.
The O modifier specifies that the numerical input may be in an alterna-
tive locale-dependent format:
%Od or %Oe
The day of the month using the locale’s alternative numeric sym-
bols; leading zeros are permitted but not required.
%OH The hour (24-hour clock) using the locale’s alternative numeric
symbols.
%OI The hour (12-hour clock) using the locale’s alternative numeric
symbols.
%Om The month using the locale’s alternative numeric symbols.
%OM The minutes using the locale’s alternative numeric symbols.
%OS The seconds using the locale’s alternative numeric symbols.
%OU The week number of the year (Sunday as the first day of the
week) using the locale’s alternative numeric symbols.
%Ow The number of the weekday (Sunday=0) using the locale’s alterna-
tive numeric symbols.
%OW The week number of the year (Monday as the first day of the
week) using the locale’s alternative numeric symbols.
%Oy The year (offset from %C) using the locale’s alternative numeric
symbols.
The broken-down time structure tm is defined in as follows:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
RETURN VALUE
The return value of the function is a pointer to the first character
not processed in this function call. In case the input string contains
more characters than required by the format string the return value
points right after the last consumed input character. In case the
whole input string is consumed the return value points to the null byte
at the end of the string. If strptime() fails to match all of the for-
mat string and therefore an error occurred the function returns NULL.
CONFORMING TO
SUSv2, POSIX.1-2001.
NOTES
In principle, this function does not initialize tm but only stores the
values specified. This means that tm should be initialized before the
call. Details differ a bit between different Unix systems. The glibc
implementation does not touch those fields which are not explicitly
specified, except that it recomputes the tm_wday and tm_yday field if
any of the year, month, or day elements changed.
This function is available since libc 4.6.8. Linux libc4 and libc5
includes define the prototype unconditionally; glibc2 includes provide
a prototype only when _XOPEN_SOURCE or _GNU_SOURCE are defined.
Before libc 5.4.13 whitespace (and the 'n' and 't' specifications) was
not handled, no 'E' and 'O' locale modifier characters were accepted,
and the 'C' specification was a synonym for the 'c' specification.
The 'y' (year in century) specification is taken to specify a year in
the 20th century by libc4 and libc5. It is taken to be a year in the
range 1950-2049 by glibc 2.0. It is taken to be a year in 1969-2068
since glibc 2.1.
Glibc Notes
For reasons of symmetry, glibc tries to support for strptime() the same
format characters as for strftime(3). (In most cases the corresponding
fields are parsed, but no field in tm is changed.) This leads to
%F Equivalent to %Y-%m-%d, the ISO 8601 date format.
%g The year corresponding to the ISO week number, but without the
century (0-99).
%G The year corresponding to the ISO week number. (For example,
1991.)
%u The day of the week as a decimal number (1-7, where Monday = 1).
%V The ISO 8601:1988 week number as a decimal number (1-53). If
the week (starting on Monday) containing 1 January has four or
more days in the new year, then it is considered week 1. Other-
wise, it is the last week of the previous year, and the next
week is week 1.
%z An RFC-822/ISO 8601 standard timezone specification.
%Z The timezone name.
Similarly, because of GNU extensions to strftime(3), %k is accepted as
a synonym for %H, and %l should be accepted as a synonym for %I, and %P
is accepted as a synonym for %p. Finally
%s The number of seconds since the Epoch, that is, since 1970-01-01
00:00:00 UTC. Leap seconds are not counted unless leap second
support is available.
The glibc implementation does not require whitespace between two field
descriptors.
EXAMPLE
The following example demonstrates the use of strptime() and strf-
time(3).
#define _XOPEN_SOURCE
#include
#include
#include
int
main(void)
{
struct tm tm;
char buf[255];
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
puts(buf);
exit(EXIT_SUCCESS);
}
SEE ALSO
time(2), getdate(3), scanf(3), setlocale(3), strftime(3), fea-
ture_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2001-11-12 STRPTIME(3)