Convert local timestamp to RFC 3339 format in R - r

I pull the current datetime using Sys.time() and I get "2018-05-12 11:52:21 EDT". How do I convert this to an RFC 3339 formatted string taking into account the local time zone is not UTC?
Note: The RFC 3339 format of this date is: 2018-05-12T15:52:21Z

This is the simplest thing I could come up with:
format(lubridate::as_datetime(Sys.time()), "%Y-%m-%dT%H:%M:%SZ")
The pseudocode:
Use as_datetime() function from lubridate package, which by default, converts the timestamp into a POSIXct datetime representation using UTC as the timezone
Format that time using format()

Just wanted to add that if you want an offset with the colon you can use the following:
.dt <- format(lubridate::now(), "%Y-%m-%dT%H:%M:%S%z")
paste0(stringr::str_sub(.dt, 1, -3),":", stringr::str_sub(.dt, -2, nchar(.dt)))

Related

Nifi convert UTC to unix time

I have a flowfile attribute which is a UTC datetime in the format of yyyy-MM-dd HH:mm:ss.SSS
I need to convert this to a unix timestamp.
How can this be done? I know its possible to convert Unix to the above format using Jolt:
"time": "${time:format('yyyy-MM-dd HH:mm:ss.SSS')}"
however, im not sure how to do this in reverse?
Working with attributes in this way uses the NiFi Expression Langauge (not Jolt).
See the docs here https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html
${time:format('yyyy-MM-dd HH:mm:ss.SSS')}
Uses Expression Language to format the time attribute to the given SimpleDateFormat string.
${time:toNumber()}
Uses Expression Language to convert the given Date object to Epoch Millis.
https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#tonumber

In R, take a date string and convert it to datetime format (using lubridate)

In R I am trying to take a date string and convert it to date time format using lubridate but anm getting an error that:
All formats failed to parse. No formats found.
Using this code:
lubridate::as_date("1/2/34")
Shouldn't this just return a formated date time?
as.Date or as_Date needs format. By default, it can parse if the format is %Y-%m-%d. Here, it is not the case. So
lubridate::as_date("1/2/34", format ="%d/%m/%y")
Or more compactly
lubridate::dmy("1/2/34")
Based on the string, it is not clear whether it is day/month/year or month/day/year. Also, for 2-digit year, there is an issue with prefix i.e. it can be either "19" or "20". Here, it would parse at "2034"

Can't format the date using moment.js

Can't format the below date using moment.js, the below statement returns Invalid Date
moment('20171206T062406927Z').format('D-MMM-YYYY');
Please help me on this.
You need to tell moment which format your date string is in:
moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');
Edit: updated as per #VincenzoC comment to ensure the timestamp is parsed in UTC
Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.

Date time format with TZ specfied with datetime in R( ISO 8601 )

I need to get current time in R in this particular format:
2014-01-07T14:57:55+05:30
Sys.time() seems to return in a different format than this. How do I exactly get this ?
Link to the format : https://en.wikipedia.org/wiki/ISO_8601
The function for converting/formatting a time string is as.POSIXct or as.POSIXlt. The documentation for these points to the docs for strptime for format symbols. This reference indicates %F is the correct symbol for ISO-8601 however, implementing that results in a format different from what you suggest.
> as.POSIXct(Sys.time(),format="%F")
[1] "2016-10-02 18:57:58 EDT"
I suspect looking at strptime you will find the combination necessary to output the exact format you need.
Is this what your looking for?
format(Sys.time(), format="%Y-%m-%dT%H:%M:%S+01:00")
format(Sys.time(), format="%Y-%m-%dT%H:%M:%S%z")
The meaning of the letters you find a the documentation of strptime() function

strptime in R returning NA

I have a variable in my dataset called timestamp which is of the form.
mydata$timestamp
2013-08-01 12:00:00
2013-08-01 12:00:00
2013-08-01 12:00:00
I want to modify these and change them to only dd-mm-yy format
dates<-strptime(mydata$timestamp, format="%d:%m:%y")
printing dates is resulting in only NA's. Not sure why.
Could anyone help, please?
Thanks in advance
Pascal's answer is correct, but I'm going to add to it because with all due respect to him I think you need to be aware of the mistakes you are making so you won't make them in the future.
In some programming languages there are internal dates that have associated formats and you can change that format without changing the internal representation of the date. That is probably why you phrased the question as you did, but that is not the way R works. In R you can either have dates represented as a string or as an actual date class that R understands, like Date or POSIXlt. Classes that R understands don't have any specific output format associated with them.
Your input data appears to be a string representation of a date. It appears that you want it in a different string representation. strptime() will change from a string to POSIXlt but this data type isn't formatted one way or the other. If you want to turn it back into a string, you need to use a different command. In Pascal's example, that function is format().
Ok, so you want to use strptime() to turn it into an R date and then use format() to turn it back into a string. Fine, but you have to have the arguments right. The second argument to strptime() is a set of characters that informs the function of what the current format is. Your argument "%d:%m:%y" is not remotely similar to what your data is. That's why are getting NA. As Pascal points out, the correct format is "%Y-%m-%d %H:%M:%S". Check the help file for strptime() to see what the symbols in the formatting strings mean.
Personally I avoid all that local time stuff that strptime() does and just use R's basic Date() class. My solution would be
dates <- format(as.Date(mydata$timestamp,format="%Y-%m-%d %H:%M:%S"),format="%d-%m-%y")
Notice that that format argument in as.Date() informs the function of what the incoming data format is and the format argument in format() tells it what you want the outgoing format to be.
If you want dd-mm-yy format, you need format(mydata$timestamp, "%d-%m-%y"). For example:
x <- strptime(c("2006-01-08 10:07:52", "2006-08-07 19:33:02"), "%Y-%m-%d %H:%M:%S", tz = "EST5EDT")
[1] "2006-01-08 10:07:52 EST" "2006-08-07 19:33:02 EDT"
format(x, "%d-%m-%y")
[1] "08-01-06" "07-08-06"

Resources