How do remove the UTC from the date using R - r

I am trying to remove the utc from this data and just keep it in single quotes this is the function i am using in R.
date.start = as.Date(Sys.Date())
But i am getting this result

I guess date.start is Sys.time() therefore do:
date.start = as.Date(Sys.time())
Sys.Date()
Sys.time()
Sys.timezone()
as.Date(Sys.time())
Output:
> Sys.Date()
[1] "2021-08-17"
> Sys.time()
[1] "2021-08-17 09:14:33 CEST"
> Sys.timezone()
[1] "Europe/Berlin"
> as.Date(Sys.time())
[1] "2021-08-17"

I think that the timezone 'UTC' is being posited there by your system settings. I believe that generating the system date with lubridate might sidestep the issue within R:
date.start = lubridate::today(tzone = "")

Use sub:
sub(" UTC", "", date)
[1] "2021-08-17" "2020-12-12"
Test data:
date <- c("2021-08-17 UTC", "2020-12-12 UTC")

Try using different time formats when getting data.
format(Sys.time(),"%d-%m-%y")
For better understanding you can read rbloggers article on Date Formats in R here:
https://www.r-bloggers.com/2013/08/date-formats-in-r/

I'm not sure why you want to remove it. That would help. Another answer showed you how to convert it to a string.
But you'll want it in date format to do something like seq(Sys.Date(), Sys.Date() + 24, by = 'day').
If the reason you want it in a particular time zone is to to join data set at midnight, you should use lubridate's force_tz ala force_tz(Sys.Date(), 'America/Chicago'). Be careful, here because it the timezone will change as needed due to daylight savings. That's why it's usually better to stick with UTC anyways.
Otherwise, as the other poster mentioned, just convert to string and format it ala format(Sys.Date(),"%Y-%m-%d").

Related

Convert Javascript new Date().getTime(); into a R recognised timestamp (ideally with lubridate)

If I open the Javascript console and type:
new Date().getTime();
I get:
1607534517803
I have a data frame in R with timestamps of this format. I'd like to convert this into a meaningful datetime.
Tried:
lubridate::as_datetime(1607534517803)
[1] "52910-09-28 14:03:23 UTC"
Expected today's date and time (At the time of posting, 12/9/20 at 11:24am USA central time)
How can I return a meaningful timestamp in R?
Try any of these options:
#Code1
as.POSIXlt.POSIXct(1607534517803/1000,origin = "1960-01-01")
#Code2
as.POSIXct(1607534517803/1000,origin = "1960-01-01")
The OP solution (many thanks for hard working):
#Code2
strtime_to_dt <- function(x) { as.POSIXct(as.numeric(x) / 1000, origin = "1960-01-01", formats = "%Y-%m-%d %H:%M:%OS") }
We can use as_datetime from lubridate
library(lubridate)
as_datetime(1607534517803/1000,origin = "1960-01-01")
#[1] "2010-12-09 17:21:57 UTC"

R time format conversion

I have almost finished my script but I have a problem with my dates format.
I installed lubridate package used the as_date function, but it doesn't give me what I want (a date).
"time" is my variable, I put its description below.
I do not put my entire script since the concern is only about this format question (and it implies a huge netcdf file impossible to download)
Could you help me please ?
class(time)
[1] "array"
head(time)
[1] 3573763200 3573774000 3573784800 3573795600 3573806400 3573817200
tunits
$long_name
[1] "time in seconds (UT)"
$standard_name
[1] "time"
$units
[1] "seconds since 1900-01-01T00:00:00Z"
$axis
[1] "T"
$time_origin
[1] "01-JAN-1900 00:00:00"
$conventions
[1] "relative number of seconds with no decimal part"
#conversion
date = as_date(time,tz="UTC",origin = "1900-01-01")
head(date)
[1] "-5877641-06-23" "-5877641-06-23" "-5877641-06-23" "-5877641-06-23"
[5] "-5877641-06-23" "-5877641-06-23"
Time is in seconds since 01/01/1900. Converting a value in time to an actual date would work as follows, using the seconds methods in lubridate:
lubridate::ymd("1900-01-01") + lubridate::seconds(3573763200)
You can vectorize it:
lubridate::ymd("1900-01-01") + lubridate::seconds(time)
as_date() calculates the date using the number of days since the origin.
What you are looking for seems to be as_datetime() also from the lubridate package which calculates the date using the number of seconds since the origin. In your example this would be:
time <- c(3573763200,3573774000,3573784800,3573795600,3573806400,3573817200)
date <- as_datetime(time, tz = "UTC", origin = "1900-01-01") %>% date()
Using a dplyr pipe and the date() function from lubridate to extract the date from the as_datetime() function.
date <- as_date(time/(24*60*60), tz = "UTC", origin = "1900-01-01")
date

date_format timezone strangeness

I defined a date with a timezone, but when I print it out using the scales package date_format it gives me the time in UTC, which is awkward for my purpose.
> library(scales)
> st <- as.POSIXct("2015-10-31 00:00:00",tz="US/Pacific")
> st
[1] "2015-10-31 PDT"
> fmt <- date_format("%Y-%m-%d %R %Z")
> fmt(st)
[1] "2015-10-31 07:00 UTC"
Interestingly this works (so POSIXct seems to understand the timezone) - but does not give me enough control over the format:
> format(st,usetz=T)
[1] "2015-10-31 PDT"
This unreliability is hinted at in the help for ?date_format:
When %z or %Z is used for output with an object with an assigned time
zone an attempt is made to use the values for that time zone — but it
is not guaranteed to succeed.
So my question is, how do I make it succeed?
Suggesting workarounds is fine and may attact upvotes, but please understand the point of this question is that I want to obtain insight as to what is going on with date_format.
The definition of date_format is very short:
function (format = "%Y-%m-%d", tz = "UTC")
{
function(x) format(x, format, tz = tz)
}
It should be obvious why the timezone is changed if you don't change the default.

Trouble dealing with POSIXct timezones and truncating the time out of POSIXct objects

I have the following piece of R-code:
formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString))
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString))
I have the problem that when I do as.Date(y) then I get 2015-07-16 (although its date is one day later!). Apparently the problem is the timezone. So I checked the timezones:
> x
[1] "2013-11-23 23:10:38 CET"
> y
[1] "2015-07-17 01:43:38 CEST"
>
Ok, so they deviate in their timezone. This is weird, because why does R decide that one timestamp (given without any timezone at all) lies in a different timezone than another (given without any timezone at all)?
Ok, so lets set the timezone. Googling revealed that attr(y, "tzone") <- "CET" should do the deal. Lets try this:
> attr(y, "tzone") <- "CET"
> y
[1] "2015-07-17 01:43:38 CEST"
>
Ok, that did not work. Let see what the timezone actually is in the beginning:
> formatString = "%Y-%m-%d %H:%M:%OS"
> x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString))
> y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString))
> unclass(x)
[1] 1385244638
attr(,"tzone")
[1] ""
> unclass(y)
[1] 1437090218
attr(,"tzone")
[1] ""
>
So... they dont have a timezone at all but their timezones are different????
--> here are my natural questions:
1) why are they initialized with a different timezone when I dont specify a timezone at all?
2) why do both objects apparently not have a timezone and at the same time... how come they have different timezones?
3) How can I make as.Date(y) == "2015-07-17" true? I.e. how can I set both to the current timezone? Sys.timezone() results in 'NA'... (EDIT: my timezone [Germany] seems to be "CET" --> how can I set both to CET?)
I'm scratching my head here... Thanks for any thoughts on this you share with me :-)
FW
If you don't specify a timezone then R will use your system's locale as POSIXct objects must have a timezone. The difference between CEST and CET is that one is summertime and one is not. That means if you define a date during the part of the year defined as summertime then R will decide to use the summertime version of the timezone. If you want to set dates that don't use summertime versions then define them as GMT from the beginning.
formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString), tz="GMT")
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString), tz="GMT")
If you want to truncate out the time, don't use as.Date on a POSIXct object since as.Date is meant to convert character objects to Date objects (which aren't the same as POSIXct objects). If you want to truncate POSIXct objects with base R then you'll have to wrap either round or trunc in as.POSIXct but I would recommend checking out the lubridate package for dealing with dates and times (specifically POSIXct objects).
If you want to keep CET but never use CEST you can use a location that doesn't observe daylight savings. According to http://www.timeanddate.com/time/zones/cet your only options are Algeria and Tunisia. According to https://en.wikipedia.org/wiki/List_of_tz_database_time_zones the valid tz would be "Africa/Algiers". Therefore you could do
formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString), tz="Africa/Algiers")
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString), tz="Africa/Algiers")
and both x and y would be in CET.
One more thing about setting timezones. If you tell R you want a generic timezone then it won't override daylight savings settings. That's why setting attr(y, "tzone") <- "CET" didn't have the desired result. If you did attr(y, "tzone") <- "Africa/Algiers" then it would have worked as you expected. Do be careful with conversions though because when you change the timezone it will change the time to account for the new timezone. The package lubridate has the function force_tz which changes the timezone without changing the time for cases where the initial timezone setting was wrong but the time was right.
Complementary answer:
1) Just use the right timezone throughout from the beginning. Since I live in Hamburg, Germany, the right timezone for me is "Europe/Berlin", see this list as said by Dean.
2) For extracting information from POSIXct, for example, the date, I use
as.Date(format(timeStamp, "%Y-%m-%d"))
which is slow but seems to give the correct answer... plus I dont have to install new packages [which is a bit complicated in my situation].
I ran into the same issue and found your Question here.
While all the given answers are valid and get an upvote from me, I'd like to share another - not too elegant - but working solution:
When ever you want to transform from class 'Date' to 'POSIXct' or vice versa, use as.character() before you convert:
x = as.POSIXct("2022-01-01")
y = as.POSIXct("2022-06-01")
x_Date <- as.Date(x)
x_POSIXct_again <- as.POSIXct(x_Date)
identical(x, x_POSIXct_again)
# FALSE!
y_Date <- as.Date(y)
y_POSIXct_again <- as.POSIXct(y_Date)
identical(y, y_POSIXct_again)
# FALSE!
x_Date <- as.Date(as.character(x))
x_POSIXct_again <- as.POSIXct(as.character(x_Date))
identical(x, x_POSIXct_again)
# TRUE!
y_Date <- as.Date(as.character(y))
y_POSIXct_again <- as.POSIXct(as.character(y_Date))
identical(y, y_POSIXct_again)
# TRUE!
# little helpers
as_Date2 <- function(x, ...) {
if("POSIXct" %in% class(x)) x <- as.character(x)
as.Date(x, ...)
}
as_POSIXct2 <- function(x, ...) {
if("Date" %in% class(x)) x <- as.character(x)
as.POSIXct(x, ...)
}
Obviously - time information gets lost when converting from POSIXct to DATE. But no more day-switching at last.

Adding lubridate dates to vector changes timezone and thus date

In the R package lubridate, I can easily create a date with the following syntax:
> mdy("5/4/2015")
As expected, it produces the following result:
[1] "2015-05-04 UTC"
However, if I try to add that very value to an array, it seems to change from UTC to my local time (EDT):
> c(mdy("5/4/2015"))
[1] "2015-05-03 20:00:00 EDT"
Since I don't care about times this wouldn't affect me much except that this results in the date shifting back by 1, as follows:
> day(mdy("5/4/2015"))
[1] 4
> day(c(mdy("5/4/2015")))
[1] 3
To me, the act of adding something to an array should not change the value of that something. Am I missing something here, and is there a way to resolve this issue?
That's because lubridate::mdy assumes UTC. When you wrap it around c(), it reverts back to your local timezone EDT because c() does not pass on the timezone attribute:
> attr(mdy("5/4/2015", tz = "EDT"), "tzone")
# [1] "EDT"
> attr(c(mdy("5/4/2015", tz = "EDT")), "tzone")
# NULL
You can do:
Sys.setenv(TZ = "UTC")
To set your local timezone to UTC.
Alternatively, you can specity the timezone explicitly in mdy():
mdy("5/4/2015", tz = "UTC")
Apart from Steven's solution, you could also store your dates in a list
list(mdy("5/4/2015"))[[1]]
#[1] "2015-05-04 UTC"
This won't remove the timezone and you don't have to mess around with environment variables.
I agree with you: If you look at c as some form of constructor for a "vector" and you come from a C++ or similar background, the removal of attributes (except for names) certainly seems strange.

Resources