python-3.xdatetimegoogle-tag-managergoogle-api-python-clientatlassian-python-api

converting utc to est time in python


I have two dates, start date and end date, where both needs to used in google tag manager in EST timezone using python.

Currently when I fetch the dates from jira using api, I am getting in the UTC format for start date, but for end date, getting only the date value, that too in class str type.

This is what I got from jira
start date: 2021-09-20T07:16:08.000+0000
end date: 2021-09-21

Now I need to convert these and create a tag using python gtm api, where the start and end date should be in EST timezone.

Time in start date should be 00:00:00 and in end date, it should be 23:59:59 (both the time in EST).

Anyone please help me on this?


Solution

  • If im underderstanding correctly, it are both strings that you are getting back like this?

    startdate = "start date: 2021-09-20T07:16:08.000+0000"
    enddate = "end date: 2021-09-21"
    

    Then first what you want to do, is split on the spaces and select the last item

    justStartDatetimeString = startdate.split(" ")[-1]
    justEndDatetimeString = enddate.split(" ")[-1]
    

    If you just get the datetime as string like this, ignore part above:

    "2021-09-20T07:16:08.000+0000"
    

    Now just parse it towards a datetime by using the dateutil.parser

    from datetime import datetime, time, timezone
    import dateutil.parser 
    startdateDateTime = dateutil.parser.isoparse(justStartDatetimeString)
    startdateDateTime = startdateDateTime.replace(tzinfo=timezone.utc).astimezone(tz=dateutil.tz.gettz('US/Eastern'))
    startdateDateTime = startdateDateTime.replace(hour=0, minute=0, second=0)
    

    For the enddate string

    enddateDateTime = dateutil.parser.isoparse(justEndDatetimeString)
    enddateDateTime = enddateDateTime.replace(tzinfo=dateutil.tz.gettz('US/Eastern'))astimezone(tz=dateutil.tz.gettz('US/Eastern'))
    enddateDateTime = enddateDateTime.replace(hour=23, minute=59, second=59)