Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/aniso8601/utcoffset.py: 36%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
3# Copyright (c) 2026, Brandon Nielsen
4# SPDX-License-Identifier: BSD-3-Clause
6import datetime
9class UTCOffset(datetime.tzinfo):
10 def __init__(self, name=None, minutes=None):
11 # We build an offset in this manner since the
12 # tzinfo class must have an init
13 # "method that can be called with no arguments"
14 self._name = name
16 if minutes is not None:
17 self._utcdelta = datetime.timedelta(minutes=minutes)
18 else:
19 self._utcdelta = None
21 def __repr__(self):
22 if self._utcdelta >= datetime.timedelta(hours=0):
23 return "+{0} UTC".format(self._utcdelta)
25 # From the docs:
26 # String representations of timedelta objects are normalized
27 # similarly to their internal representation. This leads to
28 # somewhat unusual results for negative timedeltas.
30 # Clean this up for printing purposes
31 # Negative deltas start at -1 day
32 correcteddays = abs(self._utcdelta.days + 1)
34 # Negative deltas have a positive seconds
35 deltaseconds = (24 * 60 * 60) - self._utcdelta.seconds
37 # (24 hours / day) * (60 minutes / hour) * (60 seconds / hour)
38 days, remainder = divmod(deltaseconds, 24 * 60 * 60)
40 # (1 hour) * (60 minutes / hour) * (60 seconds / hour)
41 hours, remainder = divmod(remainder, 1 * 60 * 60)
43 # (1 minute) * (60 seconds / minute)
44 minutes, seconds = divmod(remainder, 1 * 60)
46 # Add any remaining days to the correcteddays count
47 correcteddays += days
49 if correcteddays == 0:
50 return "-{0}:{1:02}:{2:02} UTC".format(hours, minutes, seconds)
51 if correcteddays == 1:
52 return "-1 day, {0}:{1:02}:{2:02} UTC".format(hours, minutes, seconds)
54 return "-{0} days, {1}:{2:02}:{3:02} UTC".format(
55 correcteddays, hours, minutes, seconds
56 )
58 def utcoffset(self, dt):
59 return self._utcdelta
61 def tzname(self, dt):
62 return self._name
64 def dst(self, dt):
65 # ISO 8601 specifies offsets should be different if DST is required,
66 # instead of allowing for a DST to be specified
67 # https://docs.python.org/2/library/datetime.html#datetime.tzinfo.dst
68 return datetime.timedelta(0)