dst() returns the daylight saving time adjustment in effect for the datetime, as a timedelta: usually 1:00:00 in summer and 0:00:00 in winter.
It returns None for naive values, and also for the built-in timezone class, which has fixed offsets and never knows about DST. Zones with rules, such as a custom tzinfo subclass or zoneinfo.ZoneInfo, return real values. The standard offset is utcoffset() minus dst().
datetime.dst() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.dst(...). Import the class first, for example from datetime import datetime. The time class has a method with the same name.
Syntax
Call it with no arguments.
python
shift = moment.dst()
Call
What it does
datetime(2024, 7, 1).dst()
Returns None: naive.
datetime(2024, 7, 1, tzinfo=timezone.utc).dst()
Returns None: fixed-offset zones never report DST.
datetime(2024, 7, 1, tzinfo=zone).dst()
Gives whatever zone.dst(dt) returns, such as timedelta(hours=1) in summer.
timetuple() turns the result into tm_isdst: None gives -1, zero gives 0, and anything else gives 1.
Method signatures
python
datetime.dst()
time.dst()
tzinfo.dst(dt) # implemented by tzinfo subclasses
# returns: timedelta or None
Parameters
dst() takes no parameters on datetime and time objects.
Return Value
Return Value
Description
timedelta
The DST shift currently in effect (zero when it is standard time).
None
Naive value, fixed-offset timezone, or a tzinfo that does not know.
Python Examples
Example 1 — See dst() Return None for Fixed Offsets
Even in July, naive values and the built-in timezone class report no DST information.
Naive. Without any tzinfo, there is nothing to ask.
UTC.timezone.utc.dst() returns None by design.
Summer offset, still None. UTC-04:00 might be summer time in New York, but a fixed timezone only stores the offset, not why it has it.
Example 2 — Report Summer Time from a Custom tzinfo
A toy harbor zone at UTC-05:00 adds one hour from April through October. Print the DST shift, the total offset, and the name for four months.
python
from datetime import datetime, timedelta, tzinfo
class Harbor(tzinfo):
"""Toy zone: UTC-5, one hour of DST from April through October."""
def utcoffset(self, dt):
return timedelta(hours=-5) + self.dst(dt)
def dst(self, dt):
if dt is not None and 4 <= dt.month <= 10:
return timedelta(hours=1)
return timedelta(0)
def tzname(self, dt):
return "HRDT" if self.dst(dt) else "HRST"
harbor = Harbor()
for month in (1, 4, 10, 11):
moment = datetime(2024, month, 15, 12, 0, tzinfo=harbor)
print(moment.strftime("%b"), moment.dst(), moment.utcoffset(), moment.tzname())
Output
Jan 0:00:00 -1 day, 19:00:00 HRST
Apr 1:00:00 -1 day, 20:00:00 HRDT
Oct 1:00:00 -1 day, 20:00:00 HRDT
Nov 0:00:00 -1 day, 19:00:00 HRST
How It Works
The rule.dst() returns one hour for months 4 to 10 and zero otherwise; it also copes with dt being None.
Offset builds on dst().utcoffset() adds the DST shift to the standard -5 hours, giving -4 hours in summer.
Negative offsets print oddly. -5 hours shows as -1 day, 19:00:00 and -4 hours as -1 day, 20:00:00.
Names follow DST.tzname() switches between the standard and the summer label; month names assume the C/English locale.
Example 3 — Derive the Standard Offset and the DST Flag
A toy southern-hemisphere zone has summer time from December to February. Subtract dst() to get the standard offset.
python
from datetime import datetime, time, timedelta, tzinfo
class Studio(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=10) + self.dst(dt)
def dst(self, dt):
if dt is None:
return timedelta(0)
return timedelta(hours=1) if dt.month in (12, 1, 2) else timedelta(0)
def tzname(self, dt):
return "STU"
studio = Studio()
summer = datetime(2024, 1, 20, 9, 0, tzinfo=studio)
print(bool(summer.dst()), summer.timetuple().tm_isdst)
print(time(9, 0, tzinfo=studio).dst())
print(summer.utcoffset() - summer.dst())
Output
True 1
0:00:00
10:00:00
How It Works
January is summer here. The rule gives one hour of DST, so bool() is True and timetuple() sets the flag to 1.
Time objects pass None. An aware time has no date, so the tzinfo receives None and returns zero.
Standard offset. 11 hours total minus 1 hour of DST leaves the standard UTC+10:00.
Frequently Asked Questions
The datetime is naive, or its tzinfo is the built-in timezone class, which only knows a fixed offset.
bool(moment.dst()) is True during DST for zones that support it, such as zoneinfo.ZoneInfo objects.
utcoffset() is the full offset from UTC; dst() is only the extra summer shift included in it.
Subclass tzinfo and implement utcoffset(), dst(), and tzname(). For real places, use zoneinfo instead of your own rules.
Yes for aware time objects, but the tzinfo receives None instead of a datetime, so it cannot know the season.