How to convert GMT time to string time - python

How to convert GMT time to string time? it's quite strange, when I print the time in back-end, the time is in string time format, but when I transfer it to the front-end via JSON file, the time has changed to GMT format. Why? Could someone help me?
Input: Wed, 25 Jul 2018 19:19:42 GMT
Output: 2018-07-25 19:19:42

When you convert the original time to JSON Format, it will be changed to GMT Time format. To solve this problem, you can change the time to string before converting it to JSON.

You could do something like -
from datetime import datetime
ip = 'Wed, 25 Jul 2018 19:19:42 GMT'
op = datetime.strftime(datetime.strptime(ip,'%a, %d %b %Y %H:%M:%S %Z'), '%Y-%m-%d %H:%M:%S')
# op 2018-07-25 19:19:42
Refer to this beautiful table here to get the formats right and then look into strftime and strptime

You can find resources here on strptime
from datetime import datetime
t = 'Wed, 25 Jul 2018 19:19:42 GMT'
datetime.strptime(t,'%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2018, 7, 25, 19, 19, 42)

Related

Timezone in python date string is not matching

In a script, I am converting the date string into DateTime format, so that I can modify the date, but this timezone part is showing an error.
from datetime import datetime
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %z (%Z)')
print(temp_date)
When I run this I am getting ValueEror.
ValueError: time data 'Wed, 01 Jun 2022 16:44:40 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)'
An extract from the datetime documentation:
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
So someone living in Japan may have JST, UTC, and GMT as valid values,
but probably not EST. It will raise ValueError for invalid values
Try running the below to see if CEST is in your machine's locale:
import time
print(time.tzname)
Seeing the complexity of problem, I suggest using third party library like dateutil which can parse datetime with ease.
from dateutil.parser import parse
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = parse(date_str)
print(temp_date)
temp_date is of type datetime.datetime
https://dateutil.readthedocs.io/en/stable/

What does an unspecified unconverted data mean?

I am trying to use strptime to format a time string that I have, but it gives the error of
"ValueError: unconverted data remains:"
without actually specifying what data it's missing
I'm not quite sure how to fix this problem. Here is what I've tried :
t = 'Tue, 26 Mar 2019 06:25:01 GMT'
def function(t):
timeString = t
date = datetime.datetime.strptime(timeString, '%a, %d %b %Y %H:%M:%s %Z')
I think maybe the issue is something to do with the floating point that's supposed to be there at the end. Any help would be appreciated. Also, any tips to convert this to another time zone would be great.
You can use dateutil library to parse datetime string.
Here is an example:
from dateutil import parser
datetime_string = 'Tue, 26 Mar 2019 06:25:01 GMT'
dt = parser.parse(datetime_string)
print(dt)
Output:
datetime.datetime(2019, 3, 26, 6, 25, 1, tzinfo=tzutc())
"ValueError: unconverted data remains:"
This indicates that datetime was unable to convert the data you supplied, probably due to a format error as it tried to parse an unexpected value.
Turns out that is the case, you have a typo. Change the s in the time format to capital S
date = datetime.datetime.strptime(timeString, '%a, %d %b %Y %H:%M:%S %Z')

Struggling to convert timezones GMT to CST in Python

I've spent hours on this, but my brain can't seem to figure it out, and it seems a lot of info I've found applies to Python 2?
import datetime as dt
from datetime import datetime
from pytz import timezone
import pytz
time_stamp = 'Mon, 17 Dec 2018 18:05:01 GMT'
central = timezone('US/Central')
published_time = datetime.strptime(time_stamp, '%a, %d %b %Y %H:%M:%S %Z')
published_cst = published_time.astimezone(central)
actual_time_published = published_cst.strftime('%a, %b %d %Y at %I:%M:%S %p %Z')
print(time_stamp)
print(published_time)
print(published_cst)
print(actual_time_published)
Expecting acutal_time_published to be CST because published_cst has GMT-6 hours (third result), but here is the actual result for each print command.
Mon, 17 Dec 2018 18:05:01 GMT
2018-12-17 18:05:01
2018-12-17 18:05:01-06:00
Mon, Dec 17 2018 at 06:05:01 PM CST
Pulling my hair out!
EDIT: Yes, I had "entry.published" where I meant to put "time_stamp" for the purposes of this question. Thanks for the edit!
I figured it out! Even though the original time stamp was returning a timezone (GMT), it did not have a tzinfo value (timezone value) assigned to it. It was still a "naive" datetime, even though the string contained a timezone recognized by strptime. So, I just assigned a tzinfo value of UTC to the original time and it seemed to fix it.
time_stamp = 'Mon, 17 Dec 2018 18:05:01 GMT'
utc = timezone('UTC')
central = timezone('US/Central')
published_time = datetime.strptime(time_stamp, '%a, %d %b %Y %H:%M:%S %Z')
published_gmt = published_time.replace(tzinfo=utc)
published_cst = published_gmt.astimezone(central)
actual_time_published = published_cst.strftime('%a, %b %d %Y at %I:%M:%S %p %Z')
So...
print(time_stamp)
print(published_time)
print(published_cst)
print(actual_time_published)
Yeilds this output:
Mon, 17 Dec 2018 18:05:01 GMT
2018-12-17 18:05:01
2018-12-17 18:05:01+00:00
2018-12-17 12:05:01-06:00
Mon, Dec 17 2018 at 12:05:01 PM CST

Python 3.7: converting a string with different timezone into date

I am trying to convert a string into date format in Python.
I am using following statement
datetime_obj = datetime.datetime.strptime("Sun Aug 19 16:24:31 PDT 2018", "%a %b %d %H:%M:%S %Z %Y")
However, I get an error -
ValueError: time data 'Sun Aug 19 16:24:31 PDT 2018' does not match format '%a %b %d %H:%M:%S %Z %Y'
If I remove the timezone from the date string and the format string, the code works perfect. Which leads me to believe that the issue is related to the timezone but I am not sure what actions should be taken.
I am in eastern timezone and the time zone in the string is in Pacific timezone.
Appreciate any help on this.
As mentioned in this answer you can use python-dateutil for this:
>>> from dateutil import parser
>>> datetime_obj = parser.parse("Sun Aug 19 16:24:31 PDT 2018")
datetime.datetime(2018, 8, 19, 16, 24, 31)
Standard datetime module behaves very strangely with parsing timezones, as I see reading this answer in question related to similar problem.

python value error time data %Z

I am getting the following error:
ValueError: time data 'Tue, 17 Jul 2018 11:55:34 EDT' does not match format '%a, %d %b %Y %H:%M:%S %Z'
Code:
import datetime
dt = datetime.datetime.strptime('Tue, 17 Jul 2018 11:55:34 EDT', '%a, %d %b %Y %H:%M:%S %Z')
print(dt.timestamp())
Am I missing something here?
EDT is not identified as a valid timezone string. You may use something other intelligent parser and than automatically return the datetime object without you explicitly specifying the format. My suggestion is to go up with dateutil
>>> from dateutil import parser
>>> parser.parse('Tue, 17 Jul 2018 11:55:34 EDT')
datetime.datetime(2018, 7, 17, 11, 55, 34)

Categories

Resources