[R] Incorrect Conversion of Datetime

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Jan 8 19:27:39 CET 2020


a) R cannot deal with POSIXt vectors having more than one timezone in one vector. Won't happen. You need to separate the data into different data frames if you need to deal with importing data with different timezones.

b) You say you included the  lines

Sys.timezone()
str(OlsonNames())

in your R code... but these are both used for you to extract information that could be useful to you in understanding how things are set up... they do NOT alter how R will do anything. To do that you would need to do something like

Sys.setenv( TZ="Africa/Lagos" )

and then convert some character data to POSIXct like

dta$timestamp <- as.POSIXct( dta$timestring, format=...


On January 8, 2020 8:55:19 AM PST, Ogbos Okike <giftedlife2014 using gmail.com> wrote:
>Dear Jeff,
>Thank you very much for looking into this.
>
>I have made some search on ?OlsonNames.
>System time zones indicates Africa/Lagos whereas the data I am trying
>to convert are Cosmic ray data prepared at different time zones
>including Oulu station (Russia), Climax station (America), many parts
>of Europe, etc.
>
>Including:
> Sys.timezone()
>str(OlsonNames())
>
>in my code gives:
>[1] "1998-01-05 02:00:00 WAT" "1998-01-05 03:00:00 WAT"
> [3] "1998-01-05 04:00:00 WAT" "1998-01-05 05:00:00 WAT"
> [5] "1998-01-05 06:00:00 WAT" "1998-01-05 07:00:00 WAT"
> [7] "1998-01-05 08:00:00 WAT" "1998-01-05 09:00:00 WAT"
> [9] "1998-01-05 10:00:00 WAT" "1998-01-05 11:00:00 WAT"
>[11] "1998-01-05 12:00:00 WAT" "1998-01-05 13:00:00 WAT"
>[13] "1998-01-05 14:00:00 WAT" "1998-01-05 15:00:00 WAT"
>[15] "1998-01-05 16:00:00 WAT" "1998-01-05 17:00:00 WAT"
>[17] "1998-01-05 18:00:00 WAT" "1998-01-05 19:00:00 WAT"
>[19] "1998-01-05 20:00:00 WAT" "1998-01-05 21:00:00 WAT"
>[21] "1998-01-05 22:00:00 WAT" "1998-01-05 23:00:00 WAT"
>[23] "1998-01-06 00:00:00 WAT" "1998-01-06 01:00:00 WAT"
>[25] "1998-01-06 02:00:00 WAT" "1998-01-06 03:00:00 WAT"
>[27] "1998-01-06 04:00:00 WAT" "1998-01-06 05:00:00 WAT"
>[29] "1998-01-06 06:00:00 WAT"
>
>Replacing the two lines above with:
>Sys.setenv( TZ="" )
>gives:
>[1] "1998-01-05 02:00:00 UTC" "1998-01-05 03:00:00 UTC"
> [3] "1998-01-05 04:00:00 UTC" "1998-01-05 05:00:00 UTC"
> [5] "1998-01-05 06:00:00 UTC" "1998-01-05 07:00:00 UTC"
> [7] "1998-01-05 08:00:00 UTC" "1998-01-05 09:00:00 UTC"
> [9] "1998-01-05 10:00:00 UTC" "1998-01-05 11:00:00 UTC"
>[11] "1998-01-05 12:00:00 UTC" "1998-01-05 13:00:00 UTC"
>[13] "1998-01-05 14:00:00 UTC" "1998-01-05 15:00:00 UTC"
>[15] "1998-01-05 16:00:00 UTC" "1998-01-05 17:00:00 UTC"
>[17] "1998-01-05 18:00:00 UTC" "1998-01-05 19:00:00 UTC"
>[19] "1998-01-05 20:00:00 UTC" "1998-01-05 21:00:00 UTC"
>[21] "1998-01-05 22:00:00 UTC" "1998-01-05 23:00:00 UTC"
>[23] "1998-01-06 00:00:00 UTC" "1998-01-06 01:00:00 UTC"
>[25] "1998-01-06 02:00:00 UTC" "1998-01-06 03:00:00 UTC"
>[27] "1998-01-06 04:00:00 UTC" "1998-01-06 05:00:00 UTC"
>[29] "1998-01-06 06:00:00 UTC"
>
>All these are still not correct and I am yet longing for further help.
>
>Best regards
>Ogbos
>
>On Wed, Jan 8, 2020 at 4:22 PM Jeff Newmiller
><jdnewmil using dcn.davis.ca.us> wrote:
>>
>> In your first email you said you were using
>>
>> Sys.setenv( TZ="GMT" )
>>
>> in your code, which defines the default assumption for time
>conversion timezone (at least until you change it). Keep in mind that
>you may be dealing with data from other timezones than your local one
>that the operating system uses, so the OS timezone only gets used if TZ
>is blank (and even that behavior can be OS-dependent I think).
>>
>> Read
>>
>> ?OlsonNames
>>
>> because AEST may not be a valid specification for TZ.
>>
>> On January 8, 2020 4:09:01 AM PST, Ogbos Okike
><giftedlife2014 using gmail.com> wrote:
>> >Dear Jim,
>> >In order to check whether I have a correct time on my system, I run:
>> >$ timedatectl
>> >
>> > and it gives:
>> >Local time: Wed 2020-01-08 13:03:44 WAT
>> >                  Universal time: Wed 2020-01-08 12:03:44 UTC
>> >                        RTC time: Wed 2020-01-08 12:03:44
>> >                       Time zone: Africa/Lagos (WAT, +0100)
>> >       System clock synchronized: yes
>> >systemd-timesyncd.service active: yes
>> >                 RTC in local TZ: no.
>> >
>> >The time is correct as it agrees with the system time.
>> >
>> >I don't know if there are other things I need to change.
>> >
>> >Thank you for any suggestions.
>> >Ogbos
>> >
>> >On Wed, Jan 8, 2020 at 12:55 PM Ogbos Okike
><giftedlife2014 using gmail.com>
>> >wrote:
>> >>
>> >> Dear Jim,
>> >> Thank you for coming my assist me.
>> >> I have tried all you suggested but the same result keep coming.
>> >> I tried, for example:
>> >> dta <- read.table("Ohr1may98", col.names = c("year", "month",
>"day",
>> >> "hour", "counts"))
>> >>
>> >> dta$year <- with( dta, ifelse(year < 50, year + 2000, year +
>1900))
>> >> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >>  "%Y %m %d %H")
>> >> a =  dta$date
>> >> and obtained:
>> >> > a
>> >>  [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
>> >>  [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
>> >>  [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
>> >>  [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
>> >>  [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
>> >> [11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
>> >> [13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
>> >> [15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
>> >> [17] "1998-01-06 06:00:00 GMT"
>> >>
>> >> Instead of getting AEST as in your own result, mine remains GMT.
>> >>
>> >> I think the problem is coming from my system or location, I am not
>> >sure.
>> >>
>> >> Please have a look again and advise further.
>> >>
>> >> Thank you.
>> >>
>> >> On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <drjimlemon using gmail.com>
>> >wrote:
>> >> >
>> >> > Hi again,
>> >> > Small typo, should be
>> >> >
>> >> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >> >  "%Y %m %d %H"
>> >> >
>> >> > as I tried it both with and without the century just to check
>and
>> >> > copied the wrong line.
>> >> >
>> >> > On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <drjimlemon using gmail.com>
>> >wrote:
>> >> > >
>> >> > > Hi Ogbos,
>> >> > > I get the correct result using strptime:
>> >> > >
>> >> > > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >> > >  "%y %m %d %H"
>> >> > > )
>> >> > > > dta$date
>> >> > > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
>> >> > > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
>> >> > > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
>> >> > > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
>> >> > > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
>> >> > > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
>> >> > > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
>> >> > > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
>> >> > > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
>> >> > > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
>> >> > > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
>> >> > > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
>> >> > > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
>> >> > > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
>> >> > > [29] "1998-05-02 06:00:00 AEST"
>> >> > >
>> >> > > Same result with
>> >> > >
>> >> > > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
>> >> > >
>>
>>as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
>> >> > >
>> >> > > As it should be as the lower two call strptime. I can't see
>from
>> >your
>> >> > > code why the month and day are transcribed.
>> >> > >
>> >> > > Jim
>> >
>> >______________________________________________
>> >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >https://stat.ethz.ch/mailman/listinfo/r-help
>> >PLEASE do read the posting guide
>> >http://www.R-project.org/posting-guide.html
>> >and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Sent from my phone. Please excuse my brevity.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list