Skip to content

Commit

Permalink
Merge branch 'working'
Browse files Browse the repository at this point in the history
  • Loading branch information
dagraham committed Dec 25, 2024
2 parents adb14f0 + 5f518d9 commit e9066fd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
22 changes: 11 additions & 11 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
Recent tagged changes as of 2024-12-25 13:37:41.531423:
- 0 seconds ago (HEAD -> working, tag: 0.0.19) Daniel Graham
Recent tagged changes as of 2024-12-25 13:57:20.794587:
- 0 seconds ago (HEAD -> working, tag: 0.0.20) Daniel Graham
a93e205 2024-12-25 13:57:20 -0500
Tagged version 0.0.20.

- 20 minutes ago (tag: 0.0.19) Daniel Graham
65cd59a 2024-12-25 13:37:41 -0500
Tagged version 0.0.19.

- 25 minutes ago (tag: 0.0.18) Daniel Graham
- 45 minutes ago (tag: 0.0.18) Daniel Graham
d202d08 2024-12-25 13:12:30 -0500
Tagged version 0.0.18.

- 32 minutes ago (tag: 0.0.17) Daniel Graham
- 52 minutes ago (tag: 0.0.17) Daniel Graham
f8677ea 2024-12-25 13:05:29 -0500
Tagged version 0.0.17.

- 41 minutes ago (tag: 0.0.16) Daniel Graham
- 61 minutes ago (tag: 0.0.16) Daniel Graham
040ad27 2024-12-25 12:56:14 -0500
Tagged version 0.0.16.

- 24 hours ago (tag: 0.0.15) Daniel Graham
- 25 hours ago (tag: 0.0.15) Daniel Graham
051142c 2024-12-24 13:24:38 -0500
Tagged version 0.0.15.

- 24 hours ago (tag: 0.0.14) Daniel Graham
- 25 hours ago (tag: 0.0.14) Daniel Graham
8776f71 2024-12-24 13:15:27 -0500
Tagged version 0.0.14.

Expand Down Expand Up @@ -74,7 +78,3 @@ Recent tagged changes as of 2024-12-25 13:37:41.531423:
- 9 days ago (tag: 0.0.1) Daniel Graham
85ad4e4 2024-12-16 12:23:37 -0500
Tagged version 0.0.1.

- 11 days ago (tag: 0.0.0rc8) Daniel Graham
0809426 2024-12-14 17:13:52 -0500
Tagged version 0.0.0rc8.
2 changes: 1 addition & 1 deletion modules/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.0.19'
version = '0.0.20'
88 changes: 45 additions & 43 deletions modules/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
import re
from datetime import datetime, timezone

import pytz
from dateutil.parser import parse, parserinfo
from dateutil.tz import gettz


def datetime_to_seconds(input_str: str) -> int:
"""
Parses a datetime string with an optional timezone and returns the corresponding
seconds since the epoch as a positive or negative integer.
Args:
input_str (str): The input string in the format "<datetime> z<timezone>"
or "<datetime> zNaive".
Returns:
int: Positive seconds for aware (with timezone), negative for naive (zNaive).
"""
if "z" in input_str:
datetime_part, timezone_part = input_str.split("z", 1)
else:
datetime_part, timezone_part = input_str, None

# Create custom parserinfo with desired settings
info = parserinfo(dayfirst=False, yearfirst=True)

# Parse the datetime part
dt = parse(datetime_part.strip(), parserinfo=info)

if timezone_part:
timezone_part = timezone_part.strip()
if timezone_part.lower() == "float":
# Handle zNaive: Treat as UTC first, then negate
dt_utc = dt.replace(tzinfo=timezone.utc)
naive_seconds = int(dt_utc.timestamp())
return -naive_seconds
else:
# Handle other timezones: Aware datetime
tz = gettz(timezone_part)
if tz is None:
raise ValueError(f"Invalid timezone: {timezone_part}")
dt = dt.replace(tzinfo=tz)
else:
# Default to local timezone if no timezone is specified
dt = dt.astimezone()

# Return positive seconds for aware datetimes
return int(dt.timestamp())


def time_to_seconds(time_str: str) -> int:
Expand Down Expand Up @@ -73,48 +117,6 @@ def seconds_to_time(seconds: int) -> str:
return "".join(result) or "0s" # Return '0s' for input 0


def datetime_to_seconds(input_str: str) -> int:
"""
Parses a datetime string with an optional timezone and returns the corresponding
seconds since the epoch as a positive or negative integer.
Args:
input_str (str): The input string in the format "<datetime> z<timezone>"
or "<datetime> zFloat".
Returns:
int: Positive seconds for aware (with timezone), negative for float (zFloat).
"""
if "z" in input_str:
datetime_part, timezone_part = input_str.split("z", 1)
else:
datetime_part, timezone_part = input_str, None

# Create custom parserinfo with desired settings
info = parserinfo(dayfirst=False, yearfirst=True)

# Parse the datetime part
dt = parse(datetime_part.strip(), parserinfo=info)

if timezone_part:
timezone_part = timezone_part.strip()
if timezone_part.lower() == "float":
# Handle zfloat: Treat as UTC first, then negate
dt_utc = dt.replace(tzinfo=timezone.utc)
float_seconds = int(dt_utc.timestamp())
return -float_seconds
else:
# Handle other timezones: Aware datetime
tz = pytz.timezone(timezone_part)
dt = tz.localize(dt)
else:
# Default to local timezone if no timezone is specified
dt = dt.astimezone()

# Return positive seconds for aware datetimes
return int(dt.timestamp())


def seconds_to_datetime(seconds: int) -> datetime:
"""
Converts an integer seconds (positive for aware, negative for float)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def run(self):
"python-dateutil",
"rich",
"pyyaml",
"pytz",
],
python_requires=">=3.9",
url="https://github.com/dagraham/timemate", # Adjust based on the minimum Python version your app supports
Expand Down

0 comments on commit e9066fd

Please sign in to comment.