Skip to content

Commit

Permalink
Improve util.datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
rdgfuentes committed Nov 15, 2024
1 parent e67b9b7 commit 0c11676
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import unittest
from datetime import datetime, timezone

from utils.dates import prev_month_from_YYYYMMDD
from utils.datetime import parse_yyyy_mm_dd_param, prev_month_from_YYYYMMDD


class TestDates(unittest.TestCase):
class TestUtilsDatetime(unittest.TestCase):
def test_prev_month_from_YYYYMMDD(self):
result = prev_month_from_YYYYMMDD("20220101")

Expand All @@ -13,6 +14,11 @@ def test_prev_month_from_YYYYMMDD_with_invalid_input(self):
with self.assertRaises(Exception):
prev_month_from_YYYYMMDD("wrong_date")

def test_parse_yyyy_mm_dd_param(self):
value = "2023-01-01"
result = parse_yyyy_mm_dd_param(value)
self.assertEqual(result, datetime(2023, 1, 1, tzinfo=timezone.utc))


if __name__ == "__main__":
unittest.main()
22 changes: 0 additions & 22 deletions packages/libs/utils/utils/dates.py

This file was deleted.

43 changes: 43 additions & 0 deletions packages/libs/utils/utils/datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from datetime import datetime, timedelta, timezone


def prev_month_from_YYYYMMDD(dt_str):
"""
Returns the previous month from a given date in YYYYMMDD format.
Args:
dt_str (str): The date in YYYYMMDD format.
Returns:
str: The previous month from the given date.
"""
dt = datetime.strptime(dt_str, "%Y%m%d")
dt = dt - timedelta(days=1)
return datetime.strftime(dt.replace(day=1), "%Y%m%d")


def parse_yyyy_mm_dd_param(value, tzinfo=timezone.utc):
"""
Parse a date string in the format "yyyy-mm-dd" and return a `datetime` object with the specified tzinfo.
Args:
value (str): The date string to parse.
tzinfo (timezone, optional): The time zone info to use for the resulting datetime object. Defaults to timezone.utc.
Returns:
`datetime`: A `datetime` object representing the parsed date and time.
"""
return datetime.strptime(value, "%Y-%m-%d").replace(tzinfo=tzinfo)


def now(tz=timezone.utc):
"""
Return the current date and time in the specified timezone.
Args:
tz (timezone, optional): The time zone to use for the resulting datetime object. Defaults to timezone.utc.
Returns:
`datetime`: A `datetime` object representing the current date and time in the specified timezone.
"""
return datetime.now(tz=tz)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bigquery.table import clear_records, ensure_table_exists
from common.transforms.pick_output_fields import PickOutputFields
from utils.convert import list_to_dict
from utils.dates import parse_yyyy_mm_dd_param
from utils.datetime import parse_yyyy_mm_dd_param
from vms_ingestion.ingestion.excel_to_bq.feed_ingestion_factory import (
FeedIngestionFactory,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bigquery.table import clear_records, ensure_table_exists
from common.transforms.pick_output_fields import PickOutputFields
from utils.convert import list_to_dict
from utils.dates import parse_yyyy_mm_dd_param
from utils.datetime import parse_yyyy_mm_dd_param
from vms_ingestion.normalization.feed_normalization_factory import (
FeedNormalizationFactory,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
standardize_str,
)
from utils.convert import to_string
from utils.dates import now
from utils.datetime import now
from vms_ingestion.normalization.transforms.calculate_msgid import get_message_id
from vms_ingestion.normalization.transforms.calculate_ssvid import get_ssvid

Expand Down

0 comments on commit 0c11676

Please sign in to comment.