2016-06-07 6 views
5
t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z") 
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z") 
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z") 

Questi sembrano essere gli stessi tempi:determinare e impostare fuso orario in POSIXct, POSIXlt, strptime, ecc in R

> t.ct -t.lt 
Time difference of 0 secs 
> t.ct -t.st 
Time difference of 0 secs 
> str(t.ct) 
POSIXct[1:1], format: "2009-01-04 21:19:00" 
> str(t.lt) 
POSIXlt[1:1], format: "2009-01-04 21:19:00" 
> str(t.st) 
POSIXlt[1:1], format: "2009-01-04 21:19:00" 
> 

Ma questi sembrano avere le informazioni fuso orario differente in loro, ed è non ciò che mi aspettavo:

>  strftime(t.ct,"%Y-%m-%d %H:%M:%S %z") 
[1] "2009-01-04 21:19:00 -0500" 
>  strftime(t.lt,"%Y-%m-%d %H:%M:%S %z") 
[1] "2009-01-04 21:19:00 +1200" 
>  strftime(t.st,"%Y-%m-%d %H:%M:%S %z") 
[1] "2009-01-04 21:19:00 +1200" 
> 

Il fuso orario sul mio Mac è:

> Sys.timezone() 
[1] "America/New_York" 

Le domande Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt e as.POSIXlt ignores tz argument sembravano correlate, ma non lo chiarivano per me.

Come impostare un orario e usarlo definitivamente?

Aggiornamento:

Dalla risposta di user3293236 sotto, sembra uno dovrebbe sempre dichiarare il fuso orario della stringa, e se si sta analizzando la '-hhmm' offset, quindi utilizzare sempre tz="UTC":

t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC") 
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC") 
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC") 

risposta

5

Se non si utilizza specificamente un fuso orario, POSIXct e POSIXlt faranno riferimento al fuso orario locale. Tuttavia, questo non è del tutto affidabile. POSIXlt non visualizzerà il fuso orario nella stringa di output.

Nota, l'argomento tzone non è impostato.

t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z") 
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z") 
t.ct 
t.lt 
attr(t.ct,"tzone") #"" 
attr(t.lt,"tzone") #NULL 

Se si desidera evitare un comportamento ambiguo, è necessario specificare un fuso orario. La stringa di uscita sarà ancora diversa (per impostazione predefinita POSIXlt non mostra alcun fuso orario), ma l'attributo è lo stesso

t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki") 
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki") 
t.ct 
t.lt 
attr(t.ct,"tzone") #Europe/Helsinki 
attr(t.lt,"tzone") #Europe/Helsinki 

Ora, se si desidera cambiare fuso orario dopo l'assegnazione originale:

attr(t.ct, "tzone") <- "UTC" #this will SHIFT the time zone to UTC 
attr(t.lt, "tzone") <- "UTC" #this will REPLACE the time zone to UTC 
t.ct 
t.lt 

Per quanto riguarda il tuo problema con strftime e %z, questo non ti dà l'attributo del fuso orario. La differenza nel tuo caso deriva probabilmente da una combinazione di formattazione di stringhe, conversioni di oggetti e formattazione di fuso orario, IMO. Ma forse qualcuno più esperto, può chiarirlo.

+0

Oh questo è un difetto terribile. L'esempio '' strptime' 'strptime (" Tue, 23 mar 2010 14:36:38 -0400 ","% a,% d% b% Y% H:% M:% S% z ") ' deve essere impostato 'tz = 'UTC', e nota che significa locale sugli altri esempi. Grazie per la nota attr (x, 'tzone'). Suppongo che per POSIXct, l'attributo tzone sia il fuso orario di segnalazione predefinito per l'oggetto, ma per POSIXlt sembra regolare l'origine. –

Problemi correlati