Date String to Time tuple - python

I am trying to pass a string into as function and need to convert it into a time tuple:
def sim(startdate, enddate):
# need to convert the date from string to integer time tuple:
dt_start = dt.date(startdate)
print 'Start Date: ', dt_start
dt_end = dt.date(enddate)
print 'End Date: ', dt_end
# in String format
sim('Jan 1, 2011', 'Dec 31, 2011')
# in interger in string format
sim('2011,1,1', '2011,12,31')

Another way would be to use strptime(). Have a time format defined for both of your formats and use them accordingly. This is what I mean:
import datetime as dt
def sim(startdate, enddate):
time_format_one = "%b %d, %Y"
time_format_two = "%Y,%m,%d"
try:
dt_start = dt.datetime.strptime(startdate, time_format_one)
dt_end = dt.datetime.strptime(enddate, time_format_one)
except ValueError:
dt_start = dt.datetime.strptime(startdate, time_format_two)
dt_end = dt.datetime.strptime(enddate, time_format_two)
print 'Start Date: ', dt_start.date()
print 'End Date: ', dt_end.date()
# in String format
sim('Jan 1, 2011', 'Dec 31, 2011')
# in interger in string format
sim('2011,1,1', '2011,12,31')
prints:
Start Date: 2011-01-01
End Date: 2011-12-31
Start Date: 2011-01-01
End Date: 2011-12-31
You could use timetuple() on dt_start and dt_end if you need time tuple.

I assume you want to convert date ('Jan 1, 2011', 'Dec 31, 2011') and ('2011,1,1', '2011,12,31') into timetuple
from datetime import datetime
date_str = "Jan 1, 2011"
fmt = "%b %d, %Y"
# Construct a datetime object
date_obj = datetime.strptime(date_str, fmt)
# Convert it to any string format you want
new_fmt = "%Y, %m, %d"
print date_obj.strftime(new_fmt)
# Prints'2011, 01, 01'
# If you want python timetuple then
t_tuple = date_obj.timetuple()

What you're probably trying to do is the following:
import datetime as dt
year,month,day = map(int, '2011,1,1'.split(','))
dt_start = dt.date(year,month,day)
print dt_start # prints 2011-01-01
The error is because of the use of a string '2011,1,1' instead of integers: 2011,1,1 as an input to: datetime.date()

Related

Unable to convert datetime string with timezone to datetime in UTC using datetime python module

I am having issues converting a datetime string of this format "%d %b %Y %X %Z" to "%Y-%m-%dT%X%z". The timezone information is stripped out. For example:
>> import datetime
>> datetime_string_raw = "18 Nov 2022 08:57:04 EST"
>> datetime_utc = datetime.datetime.strptime(datetime_string_raw, "%d %b %Y %X %Z").strftime("%Y-%m-%dT%X%z")
>> print(datetime_utc)
2022-11-18T08:57:04
How can I get it to print the UTC offset? Why doesn't the %Z and %z have any effect? Thanks!
Using dateutil's parser and a definition which abbreviated names should resemble which time zone:
import datetime
import dateutil # pip install python-dateutil
tzinfos = {"EST": dateutil.tz.gettz("America/New_York"),
"EDT": dateutil.tz.gettz("America/New_York")}
datetime_string_raw = "18 Nov 2022 08:57:04 EST"
datetime_ny = dateutil.parser.parse(datetime_string_raw, tzinfos=tzinfos)
print(datetime_ny)
# 2022-11-18 08:57:04-05:00
datetime_utc = datetime_ny.astimezone(datetime.timezone.utc)
print(datetime_utc)
# 2022-11-18 13:57:04+00:00
You can do basically the same using only the standard library, but it requires some pre-processing of the date/time string. Ex:
import datetime
import zoneinfo # Python >= 3.9
def parse_dt_with_tz(dt_string: str, fmt: str, tzinfos: dict) -> datetime.datetime:
"""Parse date/time string with abbreviated time zone name to aware datetime."""
parts = dt_string.split(" ")
tz = tzinfos.get(parts[-1]) # last element is the tz name
if not tz:
raise ValueError(f"no entry found for {parts[-1]} in tzinfos")
return datetime.datetime.strptime(" ".join(parts[:-1]), fmt).replace(tzinfo=tz)
# usage
tzinfos = {"EST": zoneinfo.ZoneInfo("America/New_York"),
"EDT": zoneinfo.ZoneInfo("America/New_York")}
s = "18 Nov 2022 08:57:04 EST"
dt = parse_dt_with_tz(s, "%d %b %Y %H:%M:%S", tzinfos)
print(dt, repr(dt))
# 2022-11-18 08:57:04-05:00 datetime.datetime(2022, 11, 18, 8, 57, 4, tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))

How to convert datetime.datetime to GMT format python?

I am having an input as:
test_date = 2019-06-15 10:16:55-06:00
the code to convert is:
new_value= datetime.strptime(test_date, '%a, %d %b %Y %H:%M:%S GMT')
After getting the value from new_value> I need to convert it to:
new_date = new_value.strftime("%m/%d/%Y %I:%M:%S %p")
But I am getting an error as below:
TypeError: strptime() argument 1 must be str, not datetime.datetime
When I try to convert test_date as string like str(test_date). It causes
ValueError: time data '2019-06-15 10:16:55-06:00' does not match format '%a, %d %b %Y %H:%M:%S GMT'
How can I achieve this?
%a refers to weekday like Sun, Mon, ..., etc, but it does not appear in your test_date input. Therefore it raises an error. -06:00 means Central Standard Time, e.g. in United states, Chicago. Try the following instead.
from datetime import datetime
test_date = '2019-06-15 10:16:55-06:00'
new_value = datetime.strptime(test_date, '%Y-%m-%d %H:%M:%S%z')
new_value = new_value.timestamp()
new_value = datetime.utcfromtimestamp(new_value) #change from CST to GMT
new_date = new_value.strftime("%m/%d/%Y %I:%M:%S %p")
print(new_date)
06/15/2019 04:16:55 PM
You need to specify you date format at strptime to parse date correctly.
Then you need to convert date to GMT timezone like this
from datetime import datetime
test_date = '2019-06-15 10:16:55-06:00'
new_value = datetime.strptime(test_date, '%Y-%m-%d %H:%M:%S%z')
new_value_timestamp = new_value.timestamp()
gmt_date = datetime.utcfromtimestamp(new_value_timestamp)
new_date = gmt_date.strftime("%m/%d/%Y %I:%M:%S %p")
print(new_date)
Output
06/15/2019 04:16:55 PM
from datetime import datetime
date_string = "21-June-2018"
date_string1 = "2019-06-21 10:16:55-0600"
print("date_string =", date_string)
print("type of date_string =", type(date_string))
date_object = datetime.strptime(date_string, "%d-%B-%Y")
date_object1 = datetime.strptime(date_string1, "%Y-%m-%d %H:%M:%S%z")
print("date_object =", date_object)
print("date_object1 =", date_object1)
print("type of date_object =", type(date_object))
new_value= test_date.strftime("%d %b %Y %H:%M:%S GMT")
new_date = datetime.strptime(new_value,'%d %b %Y %H:%M:%S %Z')
new_date will give you the output.

Trouble Converting string to datetime?

I have this strings with the format "09-FEB-21 05.19.32.871000000 AM".
import datetime
dt_string = "09-FEB-21 05.19.32.871000000 AM"
format = "%d-%m-%y %H.%M.%S.%f %p"
dt_object = datetime.datetime.strptime(dt_string, format)
The above code causes ValueError("unconverted data remains: %s" %
time data '09-FEB-21 05.19.32.871000000 AM' does not match format '%d-%m-%y %H.%M.%S.%f %p'
Any help?
Use %b instead of %m (doc):
dt_string = "09-FEB-21 05.19.32.871000000 AM"
print(pd.to_datetime(dt_string, format="%d-%b-%y %H.%M.%S.%f %p"))
Prints:
2021-02-09 05:19:32.871000
EDIT: Using only datetime: Remove last 000 from the fraction:
import re
import datetime
dt_string = "09-FEB-21 05.19.32.871000000 AM"
dt_string = re.sub(r"000(?=\s)", "", dt_string)
format = "%d-%b-%y %H.%M.%S.%f %p"
dt_object = datetime.datetime.strptime(dt_string, format)
print(dt_object)
Prints:
2021-02-09 05:19:32.871000

Easier Way to get dt obj attributes

I am trying to get detailed calendar information on all my birthdays to 2024(i.e. week #, day of week etc...). I noticed Pandas as date_range function/method, but am trying to do it using time/datetime because I couldn't get "freq=" to work. This is what I have so far, and I think I can get what I need from myBirthdays list, but am wondering if there is/was an easier way? Seems like a lot of extra work.
TIA.
#import pandas as pd
from datetime import date
import time
def BdayList(birthdate, enddate):
print(birthdate, type(birthdate), endDate, type(endDate))
#print(birthdate.weekday(), endDate.isocalendar())
myMonth = date.strftime(birthdate, "%m")
myDay = date.strftime(birthdate, "%d")
myBirthDays = []
daysDelta = (enddate - birthdate)
daysDeltaInt = daysDelta.days / 365
for year in range(int(date.strftime(birthdate, "%Y")), int(date.strftime(enddate, "%Y"))): #13148
year = str(year)
myBirthday = time.strptime(year+" "+myMonth+" "+myDay, "%Y %m %d")
print(myBirthday)
myBirthDays.append(myBirthday)
#dateRange = pd.date_range(start, periods = NumPeriods, freq="A")
return myBirthDays#DaysDelta, type(DaysDelta)
myBday = date(1988, 12, 22)
endDate = date(2024, 12, 22)
BdayList(myBday, endDate)
time.struct_time(tm_year=1988, tm_mon=12, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=357, tm_isdst=-1)
Because it is possible to just replace the year in original birth_date, there is no need to switch between dates and strings. (Note that I have also PEP8'd the code and used slightly different variable names + added type hints)
from datetime import date
from typing import List
from pprint import pprint
def get_birthdays(birth_date: date, end_date: date) -> List[date]:
birthday_list = list()
while birth_date <= end_date:
birthday_list.append(birth_date)
birth_date = birth_date.replace(year=birth_date.year + 1)
return birthday_list
if __name__ == "__main__":
birthdays = get_birthdays(
birth_date=date(1988, month=12, day=22),
end_date=date(2024, month=12, day=22)
)
pprint([(x.strftime("%Y-%m-%d %A, week: %U")) for x in birthdays])
The output should be:
['1988-12-22 Thursday, week: 51',
'1989-12-22 Friday, week: 51',
'1990-12-22 Saturday, week: 50',
'1991-12-22 Sunday, week: 51',
'1992-12-22 Tuesday, week: 51',
'1993-12-22 Wednesday, week: 51',
'1994-12-22 Thursday, week: 51',
'1995-12-22 Friday, week: 51']
To format output, please check datetime documentation. Hopefully this helps!

Why is my timezone datetime wrong?

I use this code to format my time but the time that comes out is 5 hours wrong. I should be 06 something in calcutta now and it formats the time now as 01... something. What is wrong with the code?
def datetimeformat_viewad(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
tzinfo = timezone(timezoneinfo)
month = MONTHS[to_format.month - 1]
input = pytz.timezone(timezoneinfo).localize(
datetime(int(to_format.year), int(to_format.month), int(to_format.day), int(to_format.hour), int(to_format.minute)))
date_str = '{0} {1}'.format(input.day, _(month))
time_str = format_time(input, 'H:mm', tzinfo=tzinfo, locale=locale)
return "{0} {1}".format(date_str, time_str)
Update
This code worked which was according to the answer below.
def datetimeformat_viewad(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
import datetime as DT
import pytz
utc = pytz.utc
to_format = DT.datetime(int(to_format.year), int(to_format.month), int(to_format.day), int(to_format.hour), int(to_format.minute))
utc_date = utc.localize(to_format)
tzone = pytz.timezone(timezoneinfo)
tzone_date = utc_date.astimezone(tzone)
month = MONTHS[int(tzone_date.month) - 1]
time_str = format_time(tzone_date, 'H:mm')
date_str = '{0} {1}'.format(tzone_date.day, _(month))
return "{0} {1}".format(date_str, time_str)
It sounds like to_format is a naive datetime in UTC time.
You want to convert it to Calcutta time.
To do this, you localize to_format to UTC time1, and then use astimezone to convert that timezone-aware time to Calcutta time:
import datetime as DT
import pytz
utc = pytz.utc
to_format = DT.datetime(2015,7,17,1,0)
print(to_format)
# 2015-07-17 01:00:00
utc_date = utc.localize(to_format)
print(utc_date)
# 2015-07-17 01:00:00+00:00
timezoneinfo = 'Asia/Calcutta'
tzone = pytz.timezone(timezoneinfo)
tzone_date = utc_date.astimezone(tzone)
print(tzone_date)
# 2015-07-17 06:30:00+05:30
1The tzone.localize method does not convert between timezones. It
interprets the given localtime as one given in tzone. So if to_format is
meant to be interpreted as a UTC time, then use utc.localize to convert the
naive datetime to a timezone-aware UTC time.

Categories

Resources