找回密码
 注册
查看: 10739|回复: 0

R语言:strptime()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-17 10:07:55 | 显示全部楼层 |阅读模式
strptime(base)
strptime()所属R语言包:base

                                        Date-time Conversion Functions to and from Character
                                         日期时间转换功能和字符

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Functions to convert between character representations and objects of classes "POSIXlt" and "POSIXct" representing calendar dates and times.
字符表示的类的对象之间的转换的功能"POSIXlt"和"POSIXct"代表日历日期和时间。


用法----------Usage----------


## S3 method for class 'POSIXct'
format(x, format = "", tz = "", usetz = FALSE, ...)
## S3 method for class 'POSIXlt'
format(x, format = "", usetz = FALSE, ...)

## S3 method for class 'POSIXt'
as.character(x, ...)

strftime(x, format="", tz = "", usetz = FALSE, ...)
strptime(x, format, tz = "")



参数----------Arguments----------

参数:x
An object to be converted.
要转换的对象。


参数:tz
A character string specifying the timezone to be used for the conversion.  System-specific (see as.POSIXlt), but "" is the current time zone, and "GMT" is UTC.
一个字符串,指定要使用的时区转换。特定系统(见as.POSIXlt),但""是当前的时区,"GMT"是UTC。


参数:format
A character string.  The default for the format methods is "%Y-%m-%d %H:%M:%S" if any component has a time component which is not midnight, and "%Y-%m-%d" otherwise.  If options("digits.secs") is set, up to the specified number of digits will be printed for seconds.
一个字符串。默认为format方法如果有任何组件时间组件,这是不是午夜,"%Y-%m-%d %H:%M:%S"否则"%Y-%m-%d"。如果options("digits.secs")设置,指定位数将印秒。


参数:...
Further arguments to be passed from or to other methods.
进一步的参数被传递或其他方法。


参数:usetz
logical.  Should the timezone be appended to the output? This is used in printing times, and as a workaround for problems with using "%Z" on some Linux systems.
逻辑。应被追加到输出的时区?这是用来在印刷时代,作为一个使用"%Z"在某些Linux系统的问题的解决方法。


Details

详情----------Details----------

The format and as.character methods and strftime convert objects from the classes "POSIXlt" and "POSIXct" (not strftime) to character vectors.
format和as.character方法和类的strftime对象转换"POSIXlt"和"POSIXct"(strftime)字符向量。

strptime converts character vectors to class "POSIXlt": its input x is first converted by as.character. Each input string is processed as far as necessary for the format specified: any trailing characters are ignored.
strptime转换特征向量类"POSIXlt":输入x首先as.character的转换。每个输入字符串处理,尽可能为指定的格式必要:任何尾随的字符将被忽略。

strftime is a wrapper for format.POSIXlt, and it and format.POSIXct first convert to class "POSIXlt" by calling as.POSIXlt.  Note that only that conversion depends on the time zone.
strftime是format.POSIXlt的包装,它和format.POSIXct首先转换"POSIXlt"通过调用as.POSIXlt类。请注意,只是转换时区。

The usual vector re-cycling rules are applied to x and format so the answer will be of length that of the longer of the vectors.
通常的矢量循环再用的规则适用于x和format所以答案是长度的向量。

Locale-specific conversions to and from character strings are used where appropriate and available.  This affects the names of the days and months, the AM/PM indicator (if used) and the separators in formats such as %x and %X (via the setting of the LC_TIME locale category).
酌情提供使用区域设置特定的转换和字符串。这会影响天,月,AM / PM指示(如果使用)和格式,如%x和%X(通过LC_TIME语言环境类别设置分隔符的名称)。

The details of the formats are system-specific, but the following are defined by the ISO C99 / POSIX standard for strftime and are likely to be widely available.  A conversion specification is introduced by %, usually followed by a single letter or O or E and then a single letter. Any character in the format string not part of a conversion specification is interpreted literally (and %% gives %).  Widely implemented conversion specifications include
是系统特定的格式的细节,但下面的strftime的ISO C99 / POSIX标准定义,并可能是广泛使用。介绍%,通常是由一个单一的字母或O或E然后一个字母A转换规范。格式字符串转换规范的一部分的任何字符字面解释(%%给%)。广泛推行的转换规格包括




%a Abbreviated weekday name in the current
%a在当前的缩写星期名




%A Full weekday name in the current locale.  (Also
%A在当前语言环境的星期名全称。 (也




%b Abbreviated month name in the current
%b在当前的缩写的月份名称




%B Full month name in the current locale.  (Also
%B在当前语言环境的完整月份名称。 (也




%c Date and time.   Locale-specific on output,
%c日期和时间。在输出特定的语言环境,




%d Day of the month as decimal number (01–31).
%d日一个月为十进制数(01-31)。




%H Hours as decimal number (00–23).  As a special exception times such as 24:00:00 are accepted for input,
%H时间为十进制数(00-23)。作为特殊例外,如倍24:00:00接受输入,




%I Hours as decimal number (01–12).
%I时间为十进制数(01-12)。




%j Day of year as decimal number (001–366).
%j为十进制数(001-366)年日。




%m Month as decimal number (01–12).
%m为十进制数(01-12)月。




%M Minute as decimal number (00–59).
%M为十进制数(00-59)分钟。




%p AM/PM indicator in the locale.  Used in conjunction with %I and not with %H.  An
%pAM / PM的语言环境中的指标。在%I不%H用一起使用。一个




%S Second as decimal number (00–61), allowing for up to two leap-seconds (but POSIX-compliant implementations
%S第二,允许最多两个闰秒(但POSIX兼容的实现十进制数(00-61)




%U Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the
%U今年为十进制数(00-53周)星期日作为一周的第一天1(通常与




%w Weekday as decimal number (0–6, Sunday is 0).
%w平日为十进制数(0-6,星期日为0)。




%W Week of the year as decimal number (00–53) using Monday as the first day of week (and typically with the
%W今年为十进制数(00-53周)周一作为一周的第一天(通常与




%x Date.  Locale-specific on output,
%x日期。在输出特定的语言环境,




%X Time.  Locale-specific on output,
%X时间。在输出特定的语言环境,




%y Year without century (00–99).  On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say "it is expected that in a future version the
%y一年没有世纪(00-99)。输入上,20和69的前缀值00至68 99 19  - 这是由2004年和2008年POSIX标准中指定的行为,但他们也说:“预计,在未来的版本




%Y Year with century.  Note that whereas there was no zero in the original Gregorian calendar, ISO 8601:2004 defines it to be valid (interpreted as 1BC): see http://en.wikipedia.org/wiki/0_(year).  Note that the standard also says that years before 1582 in its calendar should only be used
%Y一年世纪。请注意,而没有在原公历零,ISO 8601:2004定义它是有效的(1BC解释):请参阅http://en.wikipedia.org/wiki/0_(年)。请注意,该标准还表示,应该只用于日历前在其1582年




%z Signed offset in hours and minutes from UTC, so
%z符号偏移量从UTC在小时和分钟,所以




%Z (output only.) Time zone as a character
%Z(仅限输出)。作为一个字符时区

Where leading zeros are shown they will be used on output but are optional on input.
显示前导零的地方,他们将被用来输出,但输入可选。

Note that when %z or %Z is used for output with an object with an assigned timezone an attempt is made to use the values for that timezone — but it is not guaranteed to succeed.
请注意,当%z或%Z用于输出与分配的时区,企图使用时区的值对象 - 但它不能保证成功。

Also defined in the current standards but less widely implemented (e.g. not for output on Windows) are
此外,在现行标准定义,但较广泛推行(例如Windows上不输出)




%C Century (00–99): the integer part of the year
%C世纪(00-99):今年的整数部分




%D Date format such as %m/%d/%y: ISO C99
%D%m/%d/%y如日期格式:ISO C99中




%e Day of the month as decimal number (1–31), with
%e日为十进制数(1-31)月,




%F Equivalent to %Y-%m-%d (the ISO 8601 date
%F%Y%M-%d的等效(ISO 8601日期




%g The last two digits of the week-based year
%g最后两位数字的一周一年




%G The week-based year (see %V) as a decimal
%G为期一周的基础一年(见%V)作为一个小数




%h Equivalent to %b.
%h%b。




%k The 24-hour clock time with single digits preceded
%k单位的24小时时钟时间之前




%l The 12-hour clock time with single digits preceded
%l单位的12小时时钟时间之前




%n Newline on output, arbitrary whitespace on input.
%n输出,任意输入空格换行。




%r The 12-hour clock time (using the locale's AM or PM).
%r12小时时钟时间(使用语言环境的上午或下午)。




%R Equivalent to %H:%M.
%R%H:%M。




%t Tab on output, arbitrary whitespace on input.
%t标签输出,输入任意空白。




%T Equivalent to %H:%M:%S.
%T%H:%M:%S。




%u Weekday as a decimal number (1–7, Monday is 1).
%u平日为十进制数(1-7,周一是1)。




%V Week of the year as decimal number (00–53) as defined in ISO 8601. If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1.  Otherwise, it is the last week of the previous year, and the next week is week
%V今年周为十进制数(00-53)在ISO 8601中定义。如果包含1月1日起的一周(周一开始)在新的一年有四个或更多天,然后它被认为是1周。否则,它是前一年的最后一周,下周是周

For output there are also %O[dHImMUVwWy] which may emit numbers in an alternative locale-dependent format (e.g. roman numerals), and %E[cCyYxX] which can use an alternative "era" (e.g. a different religious calendar).  Which of these are supported is OS-dependent.  These are accepted for input, but with the standard interpretation.
输出也有%O[dHImMUVwWy]可能会发出另一种语言环境相关的格式(如罗马数字)的数字,和%E[cCyYxX]可以用另一种“时代”(如不同的宗教日历)。这些支持,这是操作系统的依赖。这些接受输入,但与标准的解释。

Specific to R is %OSn, which for output gives the seconds truncated to 0 <= n <= 6 decimal places (and if %OS is not followed by a digit, it uses the setting of getOption("digits.secs"), or if that is unset, n =   3).  Further, for strptime %OS will input seconds including fractional seconds.  Note that %S ignores (and not rounds) fractional parts on output.
具体到R%OSn,这给输出(和0 <= n <= 6小数截断秒,如果%OS不是由一个数字,使用getOption("digits.secs")的设置或者,如果没有设置,n =   3)。此外,strptime%OS输入秒,包括秒的小数部分。请注意,%S忽略小数部分(而不是轮)输出。

The behaviour of other conversion specifications (and even if other character sequences commencing with % are conversion specifications) is system-specific. For output on Windows, a conversion specification is % optionally followed by # and then by a single letter.  Any conversion specification which is unimplemented is ignored.
其他转换规格(即使其他字符序列,以%开始转换规格)的行为是特定的系统。在Windows上的输出,转换规格是%可选#,然后由一个字母。未实现任何转换规范,将被忽略。


值----------Value----------

The format methods and strftime return character vectors representing the time.  NA times are returned as NA_character_.
format方法strftime回报的特征向量,代表时间。返回NANA_character_倍。

strptime turns character representations into an object of class "POSIXlt".  The timezone is used to set the isdst component and to set the "tzone" attribute if tz != "".  If the specified time is invalid (for example "2010-02-30 08:00") all the components of the result are NA.  (NB: this does means exactly what it says &ndash; if it is an invalid time, not just a time that does not exist in some timezone.)
strptime变成一个对象的类"POSIXlt"字符表示。使用时区设置isdst组件和设置如果"tzone"tz != ""属性。如果在指定的时间无效(例如"2010-02-30 08:00")结果的所有组件NA。 (注:这并不意味着它说什么 - 如果它是一个无效的时间,不只是一个不存在在某些时区的时间。)


注意----------Note----------

The default formats follow the rules of the ISO 8601 international standard which expresses a day as "2001-02-28" and a time as "14:01:02" using leading zeroes as here.  The ISO form uses no space to separate dates and times.
遵循了ISO 8601国际标准,它体现为"2001-02-28"一天和"14:01:02"使用前导零的时间,默认格式的规则。 ISO格式没有使用单独的日期和时间空间。

For strptime the input string need not specify the date completely: it is assumed that unspecified seconds, minutes or hours are zero, and an unspecified year, month or day is the current one.
strptime输入字符串需要不完全指定的日期:不明的几秒钟,几分钟或几小时的零假设,未指定的一年,一个月或一天是当前的一个。

If the timezone specified is invalid on your system, what happens is system-specific but it will probably be ignored.
如果指定的时区是无效的,您的系统上,会发生什么是特定系统的,但它可能会被忽略。

OS facilities will probably not print years before 1 CE (aka 1 AD) correctly.
OS设施将可能无法打印1日前CE(又名公元1)正确。

Remember that in most timezones some times do not occur and some occur twice because of transitions to/from summer time.  strptime does not validate such times (it does not assume a specific timezone), but conversion by as.POSIXct) will do so.  Conversion by strftime and formatting/printing uses OS facilities and may (and does on Windows) return nonsensical results for non-existent times at DST transitions.
请记住,在大多数时区的时间不发生一些发生两次因/夏时制的转换。 strptime不验证(它不会假设一个特定的时区)的时间,但转换as.POSIXct)将这样做。 strftime“格式/打印转换使用OS设施和五月(Windows上)返回DST转换不存在时间的荒谬结果。


参考文献----------References----------

...) ISO 8601.  Data elements and interchange formats &ndash; Information interchange &ndash; Representation of dates and times. For links to versions available on-line see (at the time of writing) http://www.qsl.net/g1smd/isopdf.htm; for information on the current official version, see http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html.


参见----------See Also----------

DateTimeClasses for details of the date-time classes; locales to query or set a locale.
DateTimeClasses;语言环境的日期时间类的详细信息,查询或设置一个区域。

Your system's help pages on strftime and strptime to see how to specify their formats.
系统的帮助页面上strftime和strptime来看看如何指定其格式。

(On some Unix-like systems strptime is replaced by corrected code from glibc, when all the conversion specifications described here are supported, but with no alternative number representation nor era available in any locale.)
(在一些类Unix系统strptime从glibc时,这里所描述的所有转换规格的支持,但没有替代的数字表示,也没有在任何语言环境的时代。更正代码)被替换

Windows users will find no help page for strptime: code based on glibc is used (with corrections), so all the conversion specifications described here are supported, but with no alternative number representation nor era available in any locale.
Windows用户会发现没有帮助strptime页:代码的基础上glibc(更正),所以这里所描述的所有转换规格支持,但没有其他的数字表示,也没有在任何可用的时代语言环境。


举例----------Examples----------


## locale-specific version of date()[#区域设置特定的日期(版)]
format(Sys.time(), "%a %b %d %X %Y %Z")

## time to sub-second accuracy (if supported by the OS)[#亚秒级精度时间(如果操作系统支持)]
format(Sys.time(), "%H:%M:%OS3")

## read in date info in format 'ddmmmyyyy'[#日期格式“ddmmmyyyy”信息阅读]
## This will give NA(s) in some locales; setting the C locale[#这将给在某些语言环境中NA(S);设置C语言环境]
## as in the commented lines will overcome this on most systems.[#注释行,将克服在大多数系统上。]
## lct &lt;- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")[#LCT < -  Sys.getlocale(“LC_TIME的”); Sys.setlocale(“LC_TIME的”,“C”类)]
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- strptime(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)[#Sys.setlocale(“LC_TIME的”,LCT)]
z

## read in date/time info in format 'm/d/y h:m:s'[#读取日期/时间信息格式“M / D / Y&#292;:M:S]
dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
times <- c("23:03:20", "22:29:56", "01:03:30", "18:21:03", "16:56:26")
x <- paste(dates, times)
strptime(x, "%m/%d/%y %H:%M:%S")

## time with fractional seconds[#小数秒的时间]
z <- strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS")
z # prints without fractional seconds[没有分数秒的打印]
op <- options(digits.secs=3)
z
options(op)

## timezones are not portable, but 'EST5EDT' comes pretty close.[#时区是不可移植的,但EST5EDT“的意思非常接近。]
(x <- strptime(c("2006-01-08 10:07:52", "2006-08-07 19:33:02"),
               "%Y-%m-%d %H:%M:%S", tz="EST5EDT"))
attr(x, "tzone")

## An RFC 822 header (Eastern Canada, during DST)[#的RFC 822头(加拿大东部区域,在夏令时)]
strptime("Tue, 23 Mar 2010 14:36:38 -0400",  "%a, %d %b %Y %H:%M:%S %z")

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 01:07 , Processed in 0.025498 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表