Skip to content

Conversation

@ono-max
Copy link
Contributor

@ono-max ono-max commented Jun 13, 2025

…ateutil

The dateutil package does not support timezone abbreviations like "PDT" by default.

We can reproduce the warning in the following script:

from dateutil import parser

date_str = "2024-04-01 10:00:00 PDT"
parsed = parser.parse(date_str)
print(parsed)
$ python foo.py 
/Users/ono-max/.local/share/virtualenvs/cli-tPknK2Me/lib/python3.12/site-packages/dateutil/parser/_parser.py:1207: UnknownTimezoneWarning: tzname PDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.
  warnings.warn("tzname {tzname} identified but not understood.  "
2024-04-01 10:00:00

As a workaround, we need to pass the map to tzinfos parameter.

See: dateutil/dateutil#932

from dateutil import parser
from dateutil.tz import gettz

COMMON_TIMEZONES = {
    "UTC": gettz("UTC"),
    # https://time.now/timezones/pdt/
    "PDT": gettz("America/Los_Angeles"),
    # https://time.now/timezones/jst/
    "JST": gettz("Asia/Tokyo"),
}

date_str = "2024-04-01 10:00:00 PDT"
parsed = parser.parse(date_str, tzinfos=COMMON_TIMEZONES)
print(parsed)

$ python foo.py
2024-04-01 10:00:00-07:00

@ono-max ono-max requested a review from Konboi June 13, 2025 02:22
@Konboi
Copy link
Contributor

Konboi commented Jun 17, 2025

I understand the cause of problem.
Can you write a test to confirm that this change suppresses the warning message?
If we set a time zone that isn't registered, a warning message should appear. On the other hand, if we set a registered time zone (e.g. JST), not warning should be shown.

@launchable-app

This comment has been minimized.

@ono-max
Copy link
Contributor Author

ono-max commented Jun 24, 2025

@Konboi

I added new tests: 842f235.

Can you please review this PR, again?

@Konboi Konboi requested a review from Copilot June 25, 2025 00:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request addresses the issue with handling timezone abbreviations by mapping them via the tzinfos parameter in dateutil.parser.parse. Key changes include:

  • Adding tests for timestamps with both known and unknown timezone abbreviations.
  • Introducing a utility module (COMMON_TIMEZONES) to map common abbreviations.
  • Modifying the case_event timestamp parsing to use the COMMON_TIMEZONES mapping.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/commands/record/test_case_event.py Adds tests to verify proper handling of known and unknown timezone abbreviations.
launchable/utils/common_tz.py Introduces a mapping for common timezone abbreviations using dateutil.tz.gettz.
launchable/commands/record/case_event.py Updates timestamp parsing to use the tzinfos parameter with the COMMON_TIMEZONES map.
Comments suppressed due to low confidence (1)

launchable/commands/record/case_event.py:162

  • Consider adding a brief comment explaining the use of the 'timestr' and 'tzinfos' keyword parameters to clarify that this change is to support proper parsing of timezone abbreviations as per the updated dateutil behavior.
                date = dateutil.parser.parse(timestr=ts, tzinfos=COMMON_TIMEZONES)

Copy link
Contributor

@Konboi Konboi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


But I left a question as comment

test_path=[], duration_secs=1.0, status=CaseEvent.TEST_PASSED, timestamp=ts
)
# Should parse to ISO format with correct offset
self.assertTrue(result["createdAt"].startswith("2024-06-23T12:34:56.789"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: Why did you use assertTrue and assertIn instead of assertEqual(result["createdAt"], "2024-06-23T12:34:56.789 -7:00")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just feel like it's clearer to test them separately. The first line, assertTrue guarantees whether the time isn't changed. The second line, assertIn guarantees that the timezone is parsed correctly.

But, I don't have a strong opinion, so I'm gonna change that so that it uses assertEqual(result["createdAt"], "2024-06-23T12:34:56.789 -7:00")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Should output a warning to stderr
self.assertIn(UNKNOWN_TIMEZONE_WARNING, stderr.getvalue())
# The createdAt may still start with the input timestamp, but should not have a timezone offset
self.assertTrue(result["createdAt"].startswith("2024-06-23T12:34:56.789"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because dateutil.parser uses the default timezone if the timezone is not supported. In my local env, it's something like this: 2024-06-23T12:34:56.789000+09:00

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ono-max ono-max requested a review from Konboi June 25, 2025 02:08
@sonarqubecloud
Copy link

@ono-max ono-max merged commit 5b51375 into main Jun 25, 2025
15 checks passed
@ono-max ono-max deleted the LCHIB-612 branch June 25, 2025 02:17
@github-actions github-actions bot mentioned this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants