Convert String to timestamp in Sparklyr [duplicate] - r

This question already has an answer here:
changing the JVM timezone in sparklyr
(1 answer)
Closed 3 years ago.
I'd like to convert String to time stamp, I used the below code but it added 4 hours to the timestamp, I really appreciate if you could help me to figure it out the issue
I used Sparklyr.
fds<- fds %>%
mutate(time2 = from_unixtime(
unix_timestamp(transaction_time, "yyyy-MM-ddHH:mm:ss"), "yyyy-MM-dd HH:mm:ss"))
fds<- fds %>% mutate(transaction_time_2 = to_timestamp(time2))
transaction_time is string with this format '2018-05-3016:00:40'
by using from_unixtime, it has been converted to ''2018-05-30 16:00:40' and by using to_timestamp it has been converted to 2018-05-30 20:00:40. everything is correct except 20 hour, I still expected 16.

I encountered the same problem, I think it relates to the timezone settings
try the following at the top of your program, then the problem should disappear:
sparklyr::invoke_static(sc, "java.util.TimeZone", "getTimeZone", "GMT") %>%
sparklyr::invoke_static(sc, "java.util.TimeZone", "setDefault", .)
check out this answer for more details :
changing the JVM timezone in sparklyr

Related

How to convert Date and time into unix timestamp in google sheet [duplicate]

This question already has answers here:
How to get Unix time as integer format in Google Apps Script
(2 answers)
Closed 1 year ago.
I have a google sheet where I have one column contains Date and time in the following format
15-08-2021 12:30:00
I want to convert whole column into unix timestamp including hours , minutes and seconds . above date and time should give following result
1629012600
I tried to find solution but there are clear solutions of converting unix timestamp into human readable format or conversion of Date and time into unix timestamp in other languages like php, python , js etc. but I didn't find any solution for google sheet.
how to convert Date and time into unix timestamp in google sheet.
(new Date(2021,7,15,12,30,0).valueOf()/1000).toFixed();

Reading dates with JuliaDB

I am very new to Julia, and I am having an issue with JuliaDB loadtable("myfile").If I understand well, the problem is that the dates are in the format dd/mm/yyyy, e.g. 21/07/1985. I am told 'ArgumentError:Month: 14 out of range (1:12). How can I tell Julia the format of the date of the file I am reading?
As answered here, you can use the argument colparsers to solve your issue:
loadtable(path, colparsers=Dict(:datecol => dateformat"MM/DD/YYYY"))

How to convert the UTC date into local date in Moment.js?

I have a date in this format "2020-12-16T02:48:00" that came from the server. How can I convert this into local date and time? I tried some code but couldn't succeed.
Below is the attempt that I had made in angular after receiving date from the server.
response.data.map(date=>{
var centralDate = moment( date).zone("-06:00");
date = moment(centralDate).local().format('YYYY-MM-DD hh:mm:ss');
})
If indeed the value is in UTC (as per the title of your question), and it looks like "2020-12-16T02:48:00", and you want to convert it to local time, then you should do the following:
moment.utc(date).local().format('YYYY-MM-DD HH:mm:ss');
That does the following:
Parses the input in terms of UTC
Converts it to local time
Formats it as a string in the given format
Note also that you had hh in your original format. That is for hours in a 12-hour time format and thus you shouldn't use it without also using either A or a to indicate AM/PM or am/pm. Otherwise HH is for hours in a 24-hour time format.
If your issue is that the timezone doesn't change you can resolve using utcOffset (https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/) in this way:
response.data.map(date=>{
date = moment( date).utcOffset(-360);
})
Where 360 is the conversion fo the hours in minutes
var d= new Date();
d = new Date(d+ "Z")
I am not an expert in angular but I guess the trouble in your date is the word “T”. May be using string removal function you can remove the word “T” and then it becomes a proper date time value?

unable to retrieve POSIXct from numeric format after ifelse statement (R)

I have a table like so:
dtab<-data.table(Arr.Dep=c("A","D"),
time=c("2017-05-01 04:50:00","2017-05-01 04:55:00"))
dtab[,time:=parse_date_time(dtab$time, c("%y-%m-%d %H:%M%S"))]
Operations on the date and time column seem successful:
dtab[,time2:=time+2]
But if I try to run an ifelse statement, the POSIXct format goes back to numeric and I seem to be unable to bring it back to date and time.
dtab[,time3:=ifelse(dtab[,Arr.Dep]=="A",dtab[,time]+2,"hello")]
I saw the issue has already been raised:
R- date time variable loses format after ifelse
Unfortunately it's not of great help to me, as when I try to follow the example - adding 2 seconds rather than replacing with NA as in the OP -, I hit an error anyway.
Any help?
Use library(lubridate) and add time dtab$time2 <- dtab$time2 + seconds(2). With this method, the format does not change.

as.Date showing character string in unambiguous format even when strptime specified R [duplicate]

This question already has answers here:
Convert yyyymmdd string to Date class in R
(5 answers)
Closed 6 years ago.
I have this date column of form:
20160812
20160813
Basically YYYYMMDD.
I converted it to a date using strptime and as.Date
weather_dataset$DATE = as.Date(weather_dataset$DATE,"%Y%m%d")
But I get
Error in charToDate(x) :
character string is not in a standard unambiguous format
Huh? I've clearly specified the format to use, but its still throwing me this error for some reason. Would appreciate help! :)
Thank you!
Your data has to be a character. So use this workaround:
weather_dataset$DATE <- as.Date(as.character(weather_dataset$DATE),"%Y%m%d")
Another possibility is the library(lubridate):
weather_dataset$DATE <- lubridate::ymd(weather_dataset$DATE)
library(anytime)
anydate(20160812)

Resources