Daylight Saving Time image showing clock on a world

Why Do We Care About Daylight Saving Time (DST)?

Seva Cloud Logo A Light Grey Cloud With the word Seva Inside

In my role as a Systems Development Engineer, I work with on-premises servers (not virtual or Cloud based) all over the world managed from the Amazon Web Services (The Cloud) and I have a daily requirement to calculate Daylight Saving Time (DST) to trigger jobs at the right time on my servers. The languages I’m using in this article are Powershell and Python, but the concepts are reusable across different coding languages.

The physical servers I work with have a very specific maintenance window for Operating System (OS) security patches. Between 04:00-07:00 Local Time (including in Daylight Saving Time (DST) timezones), wherever that server is located, our Cloud Systems need to orchestrate and execute the patching process globally beginning at 04:00 local time. Server reboots cannot occur after 07:00 unless performed during a controlled change management.

Thus, we have a need to know and use the exact time on the local server at any given moment and that means being aware of Daylight Saving Time (DST), rather than simply observing the timezone so we can start patch installation at 04:00 and ensure any required reboots are not triggered after 07:00.

Note:

For the rest of this article, any references to The Cloud are in reference to AWS but similar concepts will apply on other Cloud platforms and I’ll try to be as generic as possible when referencing AWS Services. The languages used are Powershell and Python, but again, these concepts will likely be language agnostic.

What is Daylight Saving Time (DST)?

Seva Cloud Logo

In some regions of the world, Daylight Saving Time (DST), involves setting your clocks to go forward one hour from the local standard time during the summer months and back again in the winter months, to make better use of natural daylight.

If you’re a human, to adjust your clocks, you simply turn the dials or modify the settings. If you’re a computer, or network connected digital clock, we’ll refer to these synonymously as the ‘Local Machine’, the Operating System is usually aware of DST via the Network Time Protocol (NTP) which can automatically adjust the time based on DST.

However, if you’re running a script (piece of code) somewhere other than the local machine that is not aware of the time on that machine, things can get a little tricky when scheduling jobs on a server under the influence of DST (Sounds like I’m referring to some new cool substance all the kids are using…).

The Problem With Daylight Saving Time (DST) on Windows Servers

Seva Cloud Logo

An Operating System has a Standard Time and a Daylight Time. For example, in Windows OS for a server located in England, UK, during the summer months when the UK is experiencing Daylight Saving Time (DST) you will see that the time is 16:24 when calling [DateTime]::Now but 15:24 when calling [DateTime]::UtcNow in Powershell. However, the BaseUtcOffset property shows 00:00:00. This is NO GOOD for me when I need to know what time it is on the server after I collected the timezone from Get-Timezone or some other method.

Edit: Turns out that Powershell has a super helpful System.TimeZoneInfo class that has a very handy method to work out the corrected UTC Offset [System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date))

Powershell console window showing the output of Get-Timezone and the date in local timezone and in relation to UTC and Daylight Saving Time

The old skool way of running tasks was good ol’ Task Scheduler running on the server – which knows the local time for obvious reasons – It’s running on the server. But these days, we all use orchestration tools like Ansible, Terraform and AWS Systems Manager (…come on, if you don’t you need to get with the times) that perform actions on demand, based on events (event driven) or on schedule (rate or cron) and reach down to the servers from above. These systems, particularly AWS Systems Manager, are unaware of the local time when executing scheduled jobs since they’re effectively running on another server, sorry, I mean the Cloud, which usually follows UTC (Which reminds me, there’s a crazy issue about the IANA time formats that’s totally bonkers I need to write about – Note to self).

It is noted (particularly in the pytz documentation) that in Systems Engineering, systems should use UTC time for everything “The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans.” which is no good when you have servers situated physically in timezones that have very specific operational hours providing business critical functions.

Maintenance Windows in AWS

Seva Cloud Logo

To schedule jobs in The Cloud, there are several tools outside the scope of this article. In this context we need a Maintenance Window. A time period designated for maintenance during which planned downtime can occur, for example, rebooting a server. This time is chosen to minimise disruption to the usual business operations and is approved by the business leaders.

In AWS, to trigger a job to run within a Maintenance Window, you must include target servers based on a text tag value. This tag value is static UNLIKE DST! We check the timezone on the local server and using the example in the Powershell above, we get a BaseUtcOffset for a server in UK during Daylight Saving Time (DST) of 00:00:00. So we apply the tag that says “Hey Maintenance Window, this server is in GMT timezone, start the job at 04:00” (To confirm, that’s not the actual tag value. The actual value would be more like Timezone: GMT. A couple of hours later, the high sev alarm rocks our on-call engineer out of bed (I know, they should be up already right!?) because servers in UK started rebooting at 07:30.

The tag tells the Maintenance Window to trigger the job at 04:00 but as I demonstrated above, the time on the server would be 05:00 (UTC+1) and the time in AWS is 04:00 (UTC).

Imagine the operation takes 2.5h to complete. This would take the time in AWS to 06.30, within our Maintenance Window, which if you remember is 04:00-07:00. However, on the server its magically 07:30, outside of the Maintenance Window. Oh boy, the business is not happy you just rebooted an email server at 07:30, 15 minutes after the top boss got to their desk to send a super critical email.

This is about time where our minds start to get a bit garbled under the influence DST – The on-call engineer has been paged. Nobody has a clue what’s going on. Maybe DST is what we think it is after all. No, come back down, its still means Daylight Saving Time.

Working Out UTC Offset Whilst Being DST Aware Using Python – The Solution

Seva Cloud Logo

When the boss ain’t happy, boy to we know about it. There’s never a pat on the back though when things are working swimmingly though is there!?

Queue the need for some Python automation that works and takes away that stress so we can recede back into the cocoon of Systems Engineering. I manage a huge fleet of servers around the world, a large portion of them in Daylight Saving Time (DST) influenced timezones. Thus, a large portion of them with the possibility of rebooting upto an hour outside of the Maintenance Window during the summer months. So we need a system in The Cloud, running in Python, that understands if a server is currently in a timezone that is experiencing DST and sets a tag based on the DST aware (corrected) UTC Offset, again in the Powershell example above, this would mean a function that returns +1 and not 0.

Now, there are several methods to fetch timezone from a server. Firstly, we could perform this logic on the server using Powershell and post to AWS (push) or we could perform the compute in AWS using Python and an additional data source (pull). Computing in AWS is the most logical so that the server is free to perform its actual duties. To do this, AWS needs to be aware of the timezone, we gather this via an alternative process (outside the scope of this article) and store it in a separate database, we’ll call this TimezoneInfo.

The timezone, from the pytz module in Python, is provided in the Unix format, for example Europe/London. This can then be used as demonstrated in the below function get_utc_offset This will output a list of all timezones with the corrected UTC Offset for Daylight Saving Time (DST) affected timezones. You could refactor this easily to pass in a single timezone name by removing the for loop, for example, if you had a server name and performed a lookup for the timezone info on another database.

This is How To Calculate Daylight Saving Time (DST) in Python:

Or if you don’t care about the DST Unaware, this can be simplified

To use this data, use the returned object to get the utc_offset_dst_aware value which will either be a positive or a negative value. This is your Timezone offset from UTC that is Daylight Saving Time (DST) aware.

Conclusion

Seva Cloud Logo
Sunset over a tranquil lake with stars is what you would imagine after solving Daylight Saving Time in Python

So there you have it, the solution to calculating Daylight Saving Time (DST) in Python, a problem that any engineer who’s worked with a fleet of servers across multiple timezones has experienced. We’ve been through the lows and the highs together and we’ve come out the other side all blissful and warm inside.

This article was preceded with months of turmoil, months of agitation and distress. Turns out that all I need is a bit of Python code…ahhh. Time for a cup of tea.

And here’s a nice image of a tranquil lake to cherish the moment with.

I’d be interested to know if you’ve experienced this issue, how you solved it, what your primary stressors were, how you coped and ultimately, how you’re feeling right now because of it.

Please leave a comment on my solution, whether its useless or if it helped you!

DESIGN | Development | HOSTING | SEO | CONSULTANCY

Website Services

Hello, My name is Liamarjit Bhogal, I run Seva Cloud, an all-in-one Socially Conscious, Ethical and Sustainable Web Design, Website Management and SEO service for individuals, small businesses and not-for-profits. We position ourselves as a web design business, but we design the full tech stack, not just the bits you see.

Web Design & Development

Lets create an authentic, stunning and enchanting website that speaks directly to your audience. A well planned, well designed and well implemented website will attract more visitors, increase engagement, and ultimately drive more support for your cause. Connect with your community and serve them better by leveraging our expertise in web design and development.

Website Management

With 13 years in the Industry, Seva Cloud has enterprise level experience to help your organization deliver high-quality results on a low budget. Don’t sacrifice your mission-driven work because of technical challenges. You deserve access to effective online tools, that reduce the complexity of managing a website so you can fulfil your purpose.

Search Engine Optimization

A website crafted with SEO by design, optimization and ongoing management, helps you achieve your website goals and increases your impact in the community. This establishes a strong foundation for your site, improves the website’s visibility and increases the chance of people discovering your important work by expanding your reach to a broader audience right from launch.

Seva Cloud is an impact-driven business that strives to provide essential, world class online solutions to not-for-profit organizations and small businesses. We exist to make them feel empowered and inspired to fulfil their missions, alleviating the frustration and complexity of managing their online presence and maximizing their impact in the world.

Leave a Reply

Your email address will not be published. Required fields are marked *