background-test3.jpg
Search

How to make your workflows DST-aware

The Medium One workflows operate on UTC time, which means that they cannot tell when it is Daylight Savings Time or not. Typically, the pytz library is used to work around this, but this library is not compatible with our workflow system.


In order to make our workflows work with Daylight Savings Time, we'll implement a workflow library that can be used to determine whether it is DST or not based on the current date.


NOTE: This example only works for places that observe DST in the United States. However, you can expand this example to work for your international needs.


Workflow Library

This section walks you through how to set up a workflow library to determine if it is currently Daylight Savings Time.


Step 1: Create a workflow library called timezone_dst

For more information on how to create a workflow library, click here.


Step 2: Import datetime

We will use the datetime library to process our dates.


from datetime import datetime


Step 3: Create list of dates when Daylight Savings Time switches

We used this site to get our information.

It is important to make sure the dates are in chronological order and none are skipped in order for our function to work. It is also important that the first date is when Daylight Savings Time begins for that year (for the US, this is the date in March).

Note that these dates are for the United States DST.


# list of dates DST switches. even idxs = DST is active, odd = DST is inactive
# NOTE: these are the dates of DST changes in the USA. you can expand upon this for international changes if needed.
dst_changes_date = [
    "2021-03-14",
    "2021-11-07",
    "2022-03-13",
    "2022-11-07",
    "2023-03-12",
    "2023-11-05",
    "2024-03-10",
    "2024-11-03",
    "2025-03-09",
    "2025-11-02",
    "2026-03-08",
    "2026-11-01",
    "2027-03-14",
    "2027-11-07",
    "2028-03-12",
    "2028-11-05",
    "2029-03-11",
    "2029-11-04",
    "2030-03-10",
    "2030-11-03"
]

Step 4: Add time to the dates and set the format

In the US, Daylight Savings always changes at 2 AM, so we add that information to our dates.

We also set a variable date_fmt which contains the format of the date we are using. If you make changes to the format in the list, make sure to change this variable as well.


dst_changes = [dst_date + "T02:00" for dst_date in dst_changes_date] # change is always at 2 AM

date_fmt = "%Y-%m-%dT%H:%M" # format for dst_changes list to convert to datetime obj

Step 5: Create is_dst function

This function accepts a datetime and returns True/False depending on if it is currently Daylight Savings Time.


To do this, the function compares incoming_date to each date in the DST list we created.

Since we created the list in order starting from a date when Daylight Savings Time begins, we know that indexes 0, 2, 4, ... (even indexes) are all times when DST begins. Conversely, we know that odd indexes are all times when DST ends.


Our function looks for the first date in the list that is later than incoming_date. If the index of that date is odd, then we know it is Daylight Savings Time, because incoming_date is before the DST end time for the year, but after the DST start time.


For example, if incoming_date is only after the first date in our list, "2021-03-14", then we know it's currently DST because Daylight Savings Time started on 2021-03-14 and incoming_date is before the end date for that year, "2021-11-07".

# given a datetime obj, return True if is in daylight savings, otherwise return False
# NOTE: some time zones do not observe DST. you must account for this when using this fn
def is_dst(incoming_date):
    idx = 0
    while True:
        if idx < len(dst_changes):
            last_dst_switch = datetime.strptime(dst_changes[idx], date_fmt)
            if incoming_date < last_dst_switch:
                break
            idx += 1
        else:
            raise Exception("URGENT: DST changes list must be updated. All dates are out of date.")
   
    return idx % 2 != 0 # if index is odd, then we are currently in DST. otherwise we are not.

Note that there are places in the US that do not observe Daylight Savings Time. Your code must know whether or not the location you are calculating for observes DST.


Example Usage

The following example shows how to convert the UTC time into the local time using a time zone offset and our custom workflow library.

from datetime import datetime, timedelta
from my import timezone_dst
    
timezone = "-08:00" # Pacific Daylight Time (CA)
dst_offset_bool = True # CA does observe DST

# adjust UTC time by time zone hours + minutes
tz_split = timezone.split(':')
hr_change = int(tz_split[0])
min_change = int(tz_split[1])
min_change *= -1 if tz_split[0][0] == "-" else 1
now = datetime.utcnow() + timedelta(hours = hr_change, minutes = min_change) 

# add 1 hour to time if DST
if dst_offset_bool:
    dst_offset = 1 if timezone_dst.is_dst(now) else 0
    now += timedelta(hours = dst_offset) 


Recent Posts

See All