converting utc format to required format using python - datetime

I am trying to convert UTC format date format to required format using python. Simply I have the datetime in the format(Fri Dec 07 19:06:06 +0000 2012), I need to convert this into my format(2012-12-07 19:06:06:546 +0000)
Code:
created_at = "Fri Dec 07 19:06:06 +0000 2012"
d = datetime.strptime(created_at, '%a %b %d %H:%M:%S %z %Y')
date_object = d.strftime('%y-%m-%d %H:%M:%S')
result:
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'
The below link from python bugs says its fixed but i didn understand what is fixed and in which version i can use %z
http://bugs.python.org/issue6641
I am not able to use %z . Is there any other way to handle this??

Do it like that:
from datetime import datetime
TIMESTAMP = datetime.utcnow().strftime('%d/%m/%Y %H:%M:%S')

Related

Confusion regarding DateTime conversion in R

I am trying to convert a character string into a dateTime object in R
Sample data:
Following is the code that I am using for conversion
sample$Tweet_Timestamp <- lapply(sample$Tweet_Timestamp, function(x) as.POSIXct(strptime(x, "%a %b %d %H:%M:%S %z %Y")))
sample<-sample%>%unnest(Tweet_Timestamp)
The result I am getting is as follows:
Now in the result we can see that the date has converted from 18th Feb to 19th Feb. I cannot understand the reason why I am getting such result.Can someone help me decipher this?
as.POSIXct will automatically convert the date-time to the time zone of your local system. If you wish to retain the original time zone, you can do so by adding tz = "UTC" which is the defualt Universal Time.
For instance, the following code (using 1st row of your sample data):
as.POSIXct(strptime("Tue Feb 18 23:09:57 +0000 2014", "%a %b %d %H:%M:%S %z %Y", tz = "UTC"))
will produce the following output (without altering the time zone):
[1] "2014-02-18 23:09:57 UTC"

Convert Uncommon Date Format

Is there an easy way to transform this string into a meaningful date format?
date <- "Tue Apr 04 10:18:33 +0000 2017"
Right now I would use some regex and then the lubridate package. But I guess there is a less complicated way.
Try this:
as.POSIXct(date, format = "%a %b %d %H:%M:%S %z %Y")

How to convert string to date format in R

I have a column of strings in the following format:
Wed, 6 Dec 2000 08:47:00 -0800 (PST)
How can I convert this into date format using lubridate or another package? I have done this before, but there was no -0800 (PST) at the end.
Thank you.
I was able to get a result using strptime() without even worrying about the timezone name at the end:
> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x, "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
However, if you want to remove the timezone name, you can use substr() to do this:
> strptime(substr(x, 1, nchar(x)-6), "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
We can also use parse_date_time
library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

Converting time string with abbreviated timezone info in R

I have a data file that has timezone info in the datetime field. It looks something like this
"Wed May 18 13:42:29 MDT 2016"
I'm trying to convert this to a POSIXct object. Unfortunately %Z is allowed only for output. For eg.
> timestr <- "Wed May 18 13:42:29 MDT 2016"
> tm.posixct <- as.POSIXct(timestr, tz="US/Mountain", format = "%a %b %d %H:%M:%S MDT %Y")
My data file contains MDT or MST, hence I can't use that in the format string. Is there anyway to use the TZ info in that timefield for conversion instead of doing string manipulation and removing the timezone info before passing it to as.POSIXct?

R convert string in date format

I'm having some problems converting string vectors into date format. I have found a lot of information here and here but they haven't worked in my case (R 3.2.3).
> strptime("Fri Feb 05 14:10:10 +0000 2016",
format="%a %b %d %H:%M:%S %z %Y", tz="GMT")
[1] NA

Resources