Skip to content

Commit

Permalink
fix: standardize on UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
meetbryce committed Jun 19, 2024
1 parent 0de4ba1 commit 7c742f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
21 changes: 11 additions & 10 deletions ossai/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import re
import uuid
from time import mktime, localtime, strptime
from time import mktime, gmtime, strptime
import calendar

from datetime import date
from dotenv import load_dotenv
Expand Down Expand Up @@ -264,16 +265,16 @@ def get_workspace_name(client: WebClient):

def get_since_timeframe_presets():
DAY_OF_SECONDS = 86400
now = localtime()
today = int(mktime(strptime(f"{now.tm_year}-{now.tm_mon}-{now.tm_mday}", "%Y-%m-%d")))
now = gmtime()
today = calendar.timegm(strptime(f"{now.tm_year}-{now.tm_mon}-{now.tm_mday}", "%Y-%m-%d"))
options = [
('Last 7 days', str(int(today - 7 * DAY_OF_SECONDS))), # 86400 seconds in a day
('Last 14 days', str(int(today - 14 * DAY_OF_SECONDS))),
('Last 30 days', str(int(today - 30 * DAY_OF_SECONDS))),
('This week', str(int(today - (now.tm_wday * DAY_OF_SECONDS)))), # Monday at 00:00:00
('Last week', str(int(today - (now.tm_wday * DAY_OF_SECONDS) - 7 * DAY_OF_SECONDS))), # From the start of last week
('This month', str(int(mktime(strptime(f"{now.tm_year}-{now.tm_mon}-01", "%Y-%m-%d"))))), # From the start of this month
('Last month', str(int(mktime(strptime(f"{now.tm_year if now.tm_mon > 1 else now.tm_year - 1}-{now.tm_mon - 1 if now.tm_mon > 1 else 12}-01", "%Y-%m-%d"))))), # From the start of last month
('Last 7 days', str(today - 7 * DAY_OF_SECONDS)),
('Last 14 days', str(today - 14 * DAY_OF_SECONDS)),
('Last 30 days', str(today - 30 * DAY_OF_SECONDS)),
('This week', str(today - (now.tm_wday * DAY_OF_SECONDS))), # Monday at 00:00:00
('Last week', str(today - (now.tm_wday * DAY_OF_SECONDS) - 7 * DAY_OF_SECONDS)), # From the start of last week
('This month', str(calendar.timegm(strptime(f"{now.tm_year}-{now.tm_mon}-01", "%Y-%m-%d")))), # From the start of this month
('Last month', str(calendar.timegm(strptime(f"{now.tm_year if now.tm_mon > 1 else now.tm_year - 1}-{now.tm_mon - 1 if now.tm_mon > 1 else 12}-01", "%Y-%m-%d")))), # From the start of last month
]
return {
"type": "static_select",
Expand Down
27 changes: 14 additions & 13 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ async def test_get_user_context_success(mock_client):

# todo: test get_text_and_blocks_for_say()

@patch('ossai.utils.localtime')
def test_get_since_timeframe_presets_structure(mock_localtime):
@patch('ossai.utils.gmtime')
def test_get_since_timeframe_presets_structure(mock_gmtime):
# Mock the current time to a fixed timestamp
fixed_time = 1718825962 # This corresponds to 2024-06-19 19:39:22 UTC
mock_localtime.return_value = time.localtime(fixed_time)
mock_gmtime.return_value = time.gmtime(fixed_time)

presets = utils.get_since_timeframe_presets()
assert isinstance(presets, dict)
Expand All @@ -227,25 +227,26 @@ def test_get_since_timeframe_presets_structure(mock_localtime):
assert len(options) == 7 # Expecting 7 time frame options


@patch('ossai.utils.localtime')
def test_get_since_timeframe_presets_values(mock_localtime):
@patch('ossai.utils.gmtime')
def test_get_since_timeframe_presets_values(mock_gmtime):
fixed_time = 1718825962 # This corresponds to 2024-06-19 19:39:22 UTC
mock_localtime.return_value = time.localtime(fixed_time)
mock_gmtime.return_value = time.gmtime(fixed_time)

presets = utils.get_since_timeframe_presets()
values = presets['options']

expected_values = [
('Last 7 days', "1718164800"), # Last 7 days
('Last 14 days', "1717560000"), # Last 14 days
('Last 30 days', "1716177600"), # Last 30 days
('This week', "1718596800"), # This week (Monday at 00:00:00)
('Last week', "1717992000"), # Last week (start of last week)
('This month', "1717214400"), # This month (start of this month)
('Last month', "1714536000"), # Last month (start of last month)
('Last 7 days', "1718150400"), # Last 7 days
('Last 14 days', "1717545600"), # Last 14 days
('Last 30 days', "1716163200"), # Last 30 days
('This week', "1718582400"), # This week (Monday at 00:00:00)
('Last week', "1717977600"), # Last week (start of last week)
('This month', "1717200000"), # This month (start of this month)
('Last month', "1714521600"), # Last month (start of last month)
]

# Check if all options have the correct structure and types
for (expected_text, expected_value), actual in zip(expected_values, values):
assert expected_text == actual['text']['text'], f"Expected text {expected_text}, got {actual['text']['text']}"
assert expected_value == actual['value'], f"'{expected_text}': Expected value {expected_value}, got {actual['value']}"

0 comments on commit 7c742f8

Please sign in to comment.