Skip to content

Commit

Permalink
Merge pull request #94 from kinow/use-enum-for-tz-format-mode
Browse files Browse the repository at this point in the history
Use an enum for the timezone format mode
  • Loading branch information
oliver-sanders authored Oct 4, 2018
2 parents 1f04892 + 35ef563 commit 087bfff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion isodatetime/parser_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
if LOCAL_TIME_ZONE_BASIC_NO_Z == "Z":
LOCAL_TIME_ZONE_BASIC_NO_Z = "+0000"
LOCAL_TIME_ZONE_EXTENDED = timezone.get_local_time_zone_format(
extended_mode=True)
timezone.TimeZoneFormatMode.extended)
LOCAL_TIME_ZONE_EXTENDED_NO_Z = LOCAL_TIME_ZONE_EXTENDED
if LOCAL_TIME_ZONE_EXTENDED_NO_Z == "Z":
LOCAL_TIME_ZONE_EXTENDED_NO_Z = "+0000"
Expand Down
18 changes: 14 additions & 4 deletions isodatetime/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import math


class TimeZoneFormatMode(object):
normal = "normal"
reduced = "reduced"
extended = "extended"


def get_local_time_zone():
"""Return the current local UTC offset in hours and minutes."""
utc_offset_seconds = -time.timezone
Expand All @@ -33,17 +39,21 @@ def get_local_time_zone():
return int(utc_offset_hours), utc_offset_minutes


def get_local_time_zone_format(extended_mode=False, reduced_mode=False):
"""Return a string denoting the current local UTC offset."""
def get_local_time_zone_format(tz_fmt_mode=TimeZoneFormatMode.normal):
"""Return a string denoting the current local UTC offset.
:param tz_fmt_mode:
:type tz_fmt_mode: TimeZoneFormat:
"""
utc_offset_hours, utc_offset_minutes = get_local_time_zone()
if utc_offset_hours == 0 and utc_offset_minutes == 0:
return "Z"
reduced_timezone_template = "%s%02d"
timezone_template = "%s%02d%02d"
if extended_mode:
if tz_fmt_mode == TimeZoneFormatMode.extended:
timezone_template = "%s%02d:%02d"
sign = "-" if (utc_offset_hours < 0 or utc_offset_minutes < 0) else "+"
if reduced_mode and utc_offset_minutes == 0:
if tz_fmt_mode == TimeZoneFormatMode.reduced and utc_offset_minutes == 0:
return reduced_timezone_template % (sign, abs(utc_offset_hours))
return timezone_template % (
sign, abs(utc_offset_hours), abs(utc_offset_minutes))

0 comments on commit 087bfff

Please sign in to comment.