Skip to content

Commit

Permalink
Update isodatetime to 3.2
Browse files Browse the repository at this point in the history
Fix bug where `next(...)` could return past datetime
  • Loading branch information
MetRonnie committed Jan 16, 2024
1 parent c07392d commit 116e567
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 40 deletions.
2 changes: 1 addition & 1 deletion conda-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- graphviz # for static graphing
# Note: can't pin jinja2 any higher than this until we give up on Cylc 7 back-compat
- jinja2 >=3.0,<3.1
- metomi-isodatetime >=1!3.0.0, <1!3.2.0
- metomi-isodatetime >=1!3.2.0, <1!3.3.0
- packaging
# Constrain protobuf version for compatible Scheduler-UIS comms across hosts
- protobuf >=4.24.4,<4.25.0
Expand Down
32 changes: 1 addition & 31 deletions cylc/flow/cycling/iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import re
from typing import List, Optional, TYPE_CHECKING, Tuple

from metomi.isodatetime.data import Calendar, CALENDAR, Duration
from metomi.isodatetime.data import Calendar, CALENDAR
from metomi.isodatetime.dumpers import TimePointDumper
from metomi.isodatetime.timezone import (
get_local_time_zone, get_local_time_zone_format, TimeZoneFormatMode)
Expand Down Expand Up @@ -699,14 +699,6 @@ def ingest_time(value: str, now: Optional[str] = None) -> str:
now = get_current_time_string()
now_point = parser.parse(now)

# correct for year in 'now' if year is the only date unit specified -
# https://github.com/cylc/cylc-flow/issues/4805#issuecomment-1103928604
if re.search(r"\(-\d{2}[);T]", value):
now_point += Duration(years=1)
# likewise correct for month if year and month are the only date units
elif re.search(r"\(-\d{4}[);T]", value):
now_point += Duration(months=1)

# perform whatever transformation is required
offset = None
if is_prev_next:
Expand Down Expand Up @@ -799,28 +791,6 @@ def prev_next(

cycle_point = timepoints[my_diff.index(min(my_diff))]

# ensure truncated dates do not have time from 'now' included' -
# https://github.com/metomi/isodatetime/issues/212
if 'T' not in value.split(')')[0]:
# NOTE: Strictly speaking we shouldn't forcefully mutate TimePoints
# in this way as they're meant to be immutable since
# https://github.com/metomi/isodatetime/pull/165, however it
# should be ok as long as the TimePoint is not used as a dict key and
# we don't call any of the TimePoint's cached methods until after we've
# finished mutating it.
cycle_point._hour_of_day = 0
cycle_point._minute_of_hour = 0
cycle_point._second_of_minute = 0
# likewise ensure month and day from 'now' are not included
# where they did not appear in the truncated datetime
if re.search(r"\(-\d{2}[);T]", value):
# case 1 - year only
cycle_point._month_of_year = 1
cycle_point._day_of_month = 1
elif re.search(r"\(-(-\d{2}|\d{4})[;T)]", value):
# case 2 - month only or year and month
cycle_point._day_of_month = 1

return cycle_point, offset


Expand Down
2 changes: 1 addition & 1 deletion cylc/flow/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def command_stop(
# schedule shutdown after wallclock time passes provided time
parser = TimePointParser()
self.set_stop_clock(
int(parser.parse(clock_time).seconds_since_unix_epoch)
parser.parse(clock_time).seconds_since_unix_epoch
)
elif task is not None:
# schedule shutdown after task succeeds
Expand Down
5 changes: 2 additions & 3 deletions cylc/flow/task_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def get_point_as_seconds(self):
"""Compute and store my cycle point as seconds since epoch."""
if self.point_as_seconds is None:
iso_timepoint = point_parse(str(self.point))
self.point_as_seconds = int(iso_timepoint.seconds_since_unix_epoch)
self.point_as_seconds = iso_timepoint.seconds_since_unix_epoch
if iso_timepoint.time_zone.unknown:
utc_offset_hours, utc_offset_minutes = (
get_local_time_zone())
Expand Down Expand Up @@ -376,8 +376,7 @@ def get_clock_trigger_time(
else:
trigger_time = point + ISO8601Interval(offset_str)

offset = int(
point_parse(str(trigger_time)).seconds_since_unix_epoch)
offset = point_parse(str(trigger_time)).seconds_since_unix_epoch
self.clock_trigger_times[offset_str] = offset
return self.clock_trigger_times[offset_str]

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ install_requires =
graphene>=2.1,<3
# Note: can't pin jinja2 any higher than this until we give up on Cylc 7 back-compat
jinja2==3.0.*
metomi-isodatetime>=1!3.0.0,<1!3.2.0
metomi-isodatetime>=1!3.2.0,<1!3.3.0
# Constrain protobuf version for compatible Scheduler-UIS comms across hosts
packaging
protobuf>=4.24.4,<4.25.0
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/cycling/test_iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def test_simple(set_cycling_type):
('next(--0325)', '20110325T0000Z'),
('next(---10)', '20100810T0000Z'),
('next(---05T1200Z)', '20100905T1200Z'),
param('next(--08-08)', '20110808T0000Z', marks=pytest.mark.xfail),
('next(--08-08)', '20110808T0000Z'),
('next(T15)', '20100809T1500Z'),
('next(T-41)', '20100808T1541Z'),
]
Expand All @@ -709,7 +709,7 @@ def test_next_simple(value: str, expected: str, set_cycling_type):
('previous(--0325)', '20100325T0000Z'),
('previous(---10)', '20100710T0000Z'),
('previous(---05T1200Z)', '20100805T1200Z'),
param('previous(--08-08)', '20100808T0000Z', marks=pytest.mark.xfail),
('previous(--08-08)', '20100808T0000Z'),
('previous(T15)', '20100808T1500Z'),
('previous(T-41)', '20100808T1441Z'),
]
Expand Down Expand Up @@ -856,7 +856,7 @@ def test_weeks_days(set_cycling_type):
('previous(--1225)', '20171225T0000Z'),
('next(-2006)', '20200601T0000Z'),
('previous(-W101)', '20180305T0000Z'),
('next(-W-1; -W-3; -W-5)', '20180314T0000Z'),
('next(-W-1; -W-3; -W-5)', '20180316T0000Z'),
('next(-001; -091; -181; -271)', '20180401T0000Z'),
('previous(-365T12Z)', '20171231T1200Z'),
]
Expand Down

0 comments on commit 116e567

Please sign in to comment.