How to get time in GMT? - python

So far I have:
>>> import time
>>> time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime())
'Tue, 10 Sep 2013 22:55:08 Mitteleurop\xe4ische Sommerzeit'
But what I need is:
'Tue, 10 Sep 2013 22:55:08 GMT'

>>> import time
>>> time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
'Tue, 10 Sep 2013 20:08:51 GMT

Related

Change datetime format to hours only in Pandas

I have a list of strings date. Formatted in like
Fri Apr 23 12:38:07 +0000 2021
How can I change its format? I want to take only the hours. I checked other source before, but you need to change the date format, which obviously I'm struggling rn
As I know, you can write the code like
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%A %b %d %H:%M:%S %z %Y')
to change its format. But idk what +0000 means.
If you only want to take the hours from the date strings, you can use .dt.strftime() after the pd.to_datetime() call, as follows:
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
Note that your format string for pd.to_datetime() is not correct and need to replace %A by %a.
+0000 is the time zone, which you can parse with %z in the format string.
Demo
ds = pd.DataFrame({'tanggal': ['Fri Apr 23 12:38:07 +0000 2021', 'Thu Apr 22 11:28:17 +0000 2021']})
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
print(ds)
tanggal waktu
0 Fri Apr 23 12:38:07 +0000 2021 12:38:07
1 Thu Apr 22 11:28:17 +0000 2021 11:28:17

Python Datetime Parse UT timezone

Trying to parse a date like, "Thu, 22 Oct 2020 17:33:00 UT" with datetime.strptime(). I've tried:
datetime.strptime('Thu, 22 Oct 2020 17:33:00 UT', '%a, %d %b %Y %H:%M:%S %Z')
However, it appears 'UT' is not a recognized timezone. Is this possible without any string manipulation? Any help is much appreciated.
you could use dateutil's parser and supply a tzinfos mapping dict:
import dateutil
timezone_infos = {"UT": dateutil.tz.UTC}
dt = dateutil.parser.parse('Thu, 22 Oct 2020 17:33:00 UT', tzinfos=timezone_infos)
print(dt)
>>> 2020-10-22 17:33:00+00:00

Django Python - Convert to datetime from Wed Dec 14 2016 14:39:16 GMT+0300 (AST)

I have a function in my backend and I receive the date from the front end in the formart Wed Dec 14 2016 14:39:16 GMT+0300 (AST)
date = request.body['date']
d = datetime.strptime(date, '%a %b %d %Y %X %Z%z')
I know strptime converts to the datetime object but I keep getting the following
'z' is a bad directive in format '%a %b %d %Y %X %Z%z'
What should my string format be?
You can do like this:
from dateutil import parser
parser.parse("Wed Dec 14 2016 14:39:16 GMT+0300 (AST)")

How to convert "Tue Aug 25 10:00:00 2015" this time stamp to "2015-08-25 10:00" in python

How to convert "Tue Aug 25 10:00:00 2015" this time stamp to ‍‍"2015-08-25 10:00" in python.
from datetime import datetime
date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
With the correct format string, you can use datetime.strptime to parse the string and format it again:
import datetime
date = datetime.datetime.strptime('Tue Aug 25 10:00:00 2015', '%a %b %d %H:%M:%S %Y')
print date.strftime('%Y-%m-%d %H:%M')
use parser using pip install python-dateutil
>>>from dateutil import parser
>>>str(parser.parse("Tue Aug 25 10:00:00 2015"))
'2015-08-25 10:00:00'

python time string does not match format

def deadlines(t):
'''shows pretty time to deadlines'''
fmt = '%a %d %m %Y %I:%M %p %Z'
dt = datetime.strptime( t , fmt )
print 'dt ', repr(dt)
first = 'Sun 11 May 2014 05:00 PM PDT'
deadlines(first)
ValueError: time data 'Sun 11 May 2014 02:00 PM PDT' does not match format ' %a %d %m %Y %I:%M %p %Z '
Whats wrong with this?
%m matches months represent as a two-digit decimal (in [01, 12]). Use %b for abbreviated month names, or %B for full month names instead:
fmt = '%a %d %b %Y %I:%M %p %Z'
A table showing the date format directives and their meanings can be found here.
If you're having trouble parsing PDT using %Z:
Per the time.strptime docs:
Support for the %Z directive is based on the values contained in
tzname and whether daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are always
known (and are considered to be non-daylight savings timezones).
So, if parsing the date string without PDT works:
In [73]: datetime.strptime('Sun 11 May 2014 05:00 PM', '%a %d %b %Y %I:%M %p')
Out[73]: datetime.datetime(2014, 5, 11, 17, 0)
but
datetime.strptime('Sun 11 May 2014 05:00 PM PDT', '%a %d %b %Y %I:%M %p %Z')
raises a ValueError, then you may need strip off the timezone name (they are, in general, ambiguous anyway):
In [10]: datestring = 'Sun 11 May 2014 05:00 PM PDT'
In [11]: datestring, _ = datestring.rsplit(' ', 1)
In [12]: datestring
Out[12]: 'Sun 11 May 2014 05:00 PM'
In [13]: datetime.strptime(datestring, '%a %d %b %Y %I:%M %p')
Out[13]: datetime.datetime(2014, 5, 11, 17, 0)
or use dateutil:
In [1]: import dateutil.parser as parser
In [2]: parser.parse('Sun 11 May 2014 05:00 PM PDT')
Out[2]: datetime.datetime(2014, 5, 11, 17, 0)

Categories