Skip to content

Commit

Permalink
Fixed bug in datetime_utils, closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Sep 1, 2020
1 parent 3fb54ce commit b2dc473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion osxmetadata/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.99.9"
__version__ = "0.99.10"
44 changes: 22 additions & 22 deletions osxmetadata/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@

import datetime

def get_local_tz():
""" return local timezone as datetime.timezone tzinfo """
local_tz = (
datetime.datetime.now(datetime.timezone(datetime.timedelta(0)))
.astimezone()
.tzinfo
)
return local_tz
def get_local_tz(dt):
""" return local timezone as datetime.timezone tzinfo for dt
Args:
dt: datetime.datetime
Returns:
local timezone for dt as datetime.timezone
Raises:
ValueError if dt is not timezone naive
"""
if not datetime_has_tz(dt):
return dt.astimezone().tzinfo
else:
raise ValueError("dt must be naive datetime.datetime object")


def datetime_has_tz(dt):
Expand All @@ -21,9 +29,7 @@ def datetime_has_tz(dt):
if type(dt) != datetime.datetime:
raise TypeError(f"dt must be type datetime.datetime, not {type(dt)}")

if dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None:
return True
return False
return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None


def datetime_tz_to_utc(dt):
Expand All @@ -34,10 +40,8 @@ def datetime_tz_to_utc(dt):
if type(dt) != datetime.datetime:
raise TypeError(f"dt must be type datetime.datetime, not {type(dt)}")

local_tz = get_local_tz()
if dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None:
dt_utc = dt.replace(tzinfo=dt.tzinfo).astimezone(tz=datetime.timezone.utc)
return dt_utc
return dt.replace(tzinfo=dt.tzinfo).astimezone(tz=datetime.timezone.utc)
else:
raise ValueError(f"dt does not have timezone info")

Expand All @@ -50,8 +54,7 @@ def datetime_remove_tz(dt):
if type(dt) != datetime.datetime:
raise TypeError(f"dt must be type datetime.datetime, not {type(dt)}")

dt_new = dt.replace(tzinfo=None)
return dt_new
return dt.replace(tzinfo=None)


def datetime_naive_to_utc(dt):
Expand All @@ -70,8 +73,7 @@ def datetime_naive_to_utc(dt):
f"{dt} has tzinfo {dt.tzinfo} and offset {dt.tzinfo.utcoffset(dt)}"
)

dt_utc = dt.replace(tzinfo=datetime.timezone.utc)
return dt_utc
return dt.replace(tzinfo=datetime.timezone.utc)


def datetime_naive_to_local(dt):
Expand All @@ -90,8 +92,7 @@ def datetime_naive_to_local(dt):
f"{dt} has tzinfo {dt.tzinfo} and offset {dt.tizinfo.utcoffset(dt)}"
)

dt_local = dt.replace(tzinfo=get_local_tz())
return dt_local
return dt.replace(tzinfo=get_local_tz(dt))


def datetime_utc_to_local(dt):
Expand All @@ -105,5 +106,4 @@ def datetime_utc_to_local(dt):
if dt.tzinfo is not datetime.timezone.utc:
raise ValueError(f"{dt} must be in UTC timezone: timezone = {dt.tzinfo}")

dt_local = dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=get_local_tz())
return dt_local
return dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)

0 comments on commit b2dc473

Please sign in to comment.