POSIXct or POSIXlt

POSIXct are easier to work with as data (e.g. a column in a dataframe).

POSIXlt is easier to work with as dates & times, and has a bigger range.

To remember which one is doing what: lt is for list.

'ct' is great for representing date/time holistically (2021-04-26 13:21:27) and is numeric under the hood in seconds since the big bang (just kidding), kind of like julian seconds.

[note that "Julian seconds" is misleading. It should be rather ordinal seconds]

'lt' is a list of 7 elements that often require manipulation (e.g. lt$mon = month of the year starting with Jan = 0) but it's great for extracting values like day of the year, day of the week, etc. w/o having to do it yourself - saves a heap of error-fraught coding.

One draw back is the handling of daylight savings shifts. In March, an hour gets lost (jumps from 0159 to 0300) and in October you get an hour twice, so if you're using code that requires 1 hour steps, you get hiccups twice/year, so that requires special handling. Don't overlook 'strptime' to convert non-standard dates (e.g. '4/26/21' isn't recognized by as.POSIXct but strptime can do the conversions for you.) 

See details of the comparison in the ?POSIXt help page.

(Extracted from Duncan Murdoch and David Stevens from r-help discussion list)

There are two basic classes of date/times.

Class "POSIXct" represents the (signed) number of seconds since the beginning of 1970 (in the UTC time zone) as a numeric vector.

Class "POSIXlt" is a named list of vectors representing:

sec    0–61: seconds.

min    0–59: minutes.

hour    0–23: hours.

mday    1–31: day of the month

mon    0–11: months after the first of the year.

year    years since 1900.

wday    0–6 day of the week, starting on Sunday.

yday    0–365: day of the year (365 only in leap years).

isdst    Daylight Saving Time flag. Positive if in force, zero if not, negative if unknown.

zone    (Optional.) The abbreviation for the time zone in force at that time: "" if unknown (but "" might also be used for UTC).

gmtoff    (Optional.) The offset in seconds from GMT: positive values are East of the meridian. Usually NA if unknown, but 0 could mean unknown.


> Sys.time()

[1] "2021-04-27 09:35:32 CEST"

> class(Sys.time())

[1] "POSIXct" "POSIXt" 

> str(Sys.time())

POSIXct[1:1], format: "2021-04-27 09:35:55"

> as.numeric(Sys.time())

[1] 1619508987

> as.POSIXlt(Sys.time())

[1] "2021-04-27 09:37:07 CEST"

> class(as.POSIXlt(Sys.time()))

[1] "POSIXlt" "POSIXt" 

> str(as.POSIXlt(Sys.time()))

POSIXlt[1:1], format: "2021-04-27 09:37:29"

> as.POSIXlt(Sys.time())$sec

[1] 40.18149

> as.POSIXlt(Sys.time())$min

[1] 39

> as.POSIXlt(Sys.time())$hour

[1] 9

> as.POSIXlt(Sys.time())$mday

[1] 27

> as.POSIXlt(Sys.time())$mon

[1] 3

> as.POSIXlt(Sys.time())$year

[1] 121

> as.POSIXlt(Sys.time())$wday

[1] 2

> as.POSIXlt(Sys.time())$yday

[1] 116

> as.POSIXlt(Sys.time())$isdst

[1] 1

> as.POSIXlt(Sys.time())$zone

[1] "CEST"

> as.POSIXlt(Sys.time())$gmtoff

[1] 7200

Update 3/8/2021:

There are two POSIXt types, POSIXct and POSIXlt. "ct" can stand for calendar time, it stores the number of seconds since the origin. "lt", or local time, keeps the date as a list of time attributes (such as "hour" and "mon").

I prefer remember lt as "list type" to remember the difference.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04