Skip to content

Commit 4bb4bc5

Browse files
committed
feat: deprecate timezone argument in favor of utc_offset
1 parent 6f73309 commit 4bb4bc5

File tree

2 files changed

+96
-59
lines changed

2 files changed

+96
-59
lines changed

kosmorrolib/ephemerides.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626

2727
from .model import Position, AsterEphemerides, MoonPhase, Object, ASTERS
2828
from .dateutil import translate_to_timezone, normalize_datetime
29-
from .core import get_skf_objects, get_timescale, get_iau2000b
29+
from .core import get_skf_objects, get_timescale, get_iau2000b, deprecated, alert_deprecation
3030
from .enum import MoonPhaseType
3131
from .exceptions import OutOfRangeDateError
3232

3333
RISEN_ANGLE = -0.8333
3434

3535

3636
def _get_skyfield_to_moon_phase(
37-
times: [Time], vals: [int], now: Time, timezone: int
37+
times: [Time], vals: [int], now: Time, utc_offset: int|float
3838
) -> Union[MoonPhase, None]:
3939
tomorrow = get_timescale().utc(
4040
now.utc_datetime().year, now.utc_datetime().month, now.utc_datetime().day + 1
@@ -58,20 +58,20 @@ def _get_skyfield_to_moon_phase(
5858
MoonPhaseType.FULL_MOON,
5959
MoonPhaseType.LAST_QUARTER,
6060
]:
61-
current_phase_time = translate_to_timezone(times[i].utc_datetime(), timezone)
61+
current_phase_time = translate_to_timezone(times[i].utc_datetime(), utc_offset)
6262
else:
6363
current_phase_time = None
6464

6565
# Find the next moon phase
6666
for j in range(i + 1, len(times)):
6767
if vals[j] in [0, 2, 4, 6]:
68-
next_phase_time = translate_to_timezone(times[j].utc_datetime(), timezone)
68+
next_phase_time = translate_to_timezone(times[j].utc_datetime(), utc_offset)
6969
break
7070

7171
return MoonPhase(current_phase, current_phase_time, next_phase_time)
7272

7373

74-
def get_moon_phase(for_date: date = date.today(), timezone: int = 0) -> MoonPhase:
74+
def get_moon_phase(for_date: date = date.today(), utc_offset: int|float = 0, **argv) -> MoonPhase:
7575
"""Calculate and return the moon phase for the given date, adjusted to the given timezone if any.
7676
7777
Get the moon phase for the 27 March, 2021:
@@ -87,6 +87,11 @@ def get_moon_phase(for_date: date = date.today(), timezone: int = 0) -> MoonPhas
8787
8888
Get the moon phase for the 27 March, 2021, in the UTC+2 timezone:
8989
90+
>>> get_moon_phase(date(2021, 3, 27), utc_offset=2)
91+
<MoonPhase phase_type=MoonPhaseType.WAXING_GIBBOUS time=None next_phase_date=2021-03-28 20:48:10.902298+02:00>
92+
93+
Note that the `utc_timezone` argument was named `timezone` before version 1.1. The old name still works, but will be dropped in the future.
94+
9095
>>> get_moon_phase(date(2021, 3, 27), timezone=2)
9196
<MoonPhase phase_type=MoonPhaseType.WAXING_GIBBOUS time=None next_phase_date=2021-03-28 20:48:10.902298+02:00>
9297
@@ -98,6 +103,11 @@ def get_moon_phase(for_date: date = date.today(), timezone: int = 0) -> MoonPhas
98103
...
99104
kosmorrolib.exceptions.OutOfRangeDateError: The date must be between 1899-08-09 and 2053-09-26
100105
"""
106+
107+
if argv.get("timezone") is not None:
108+
alert_deprecation("'timezone' argument of the get_moon_phase() function is deprecated. Use utc_offset instead.")
109+
utc_offset = argv.get("timezone")
110+
101111
earth = get_skf_objects()["earth"]
102112
moon = get_skf_objects()["moon"]
103113
sun = get_skf_objects()["sun"]
@@ -117,11 +127,11 @@ def moon_phase_at(time: Time):
117127

118128
try:
119129
times, phases = find_discrete(start_time, end_time, moon_phase_at)
120-
return _get_skyfield_to_moon_phase(times, phases, today, timezone)
130+
return _get_skyfield_to_moon_phase(times, phases, today, utc_offset)
121131

122132
except EphemerisRangeError as error:
123-
start = translate_to_timezone(error.start_time.utc_datetime(), timezone)
124-
end = translate_to_timezone(error.end_time.utc_datetime(), timezone)
133+
start = translate_to_timezone(error.start_time.utc_datetime(), utc_offset)
134+
end = translate_to_timezone(error.end_time.utc_datetime(), utc_offset)
125135

126136
start = date(start.year, start.month, start.day) + timedelta(days=12)
127137
end = date(end.year, end.month, end.day) - timedelta(days=12)
@@ -130,7 +140,7 @@ def moon_phase_at(time: Time):
130140

131141

132142
def get_ephemerides(
133-
position: Position, for_date: date = date.today(), timezone: int = 0
143+
position: Position, for_date: date = date.today(), utc_offset: int|float = 0, **argv
134144
) -> [AsterEphemerides]:
135145
"""Compute and return the ephemerides for the given position and date, adjusted to the given timezone if any.
136146
@@ -148,9 +158,9 @@ def get_ephemerides(
148158
<AsterEphemerides rise_time=2022-07-07 22:27:00 culmination_time=2022-07-07 04:25:00 set_time=2022-07-07 10:20:00 aster=<Object type=PLANET name=NEPTUNE />>,
149159
<AsterEphemerides rise_time=2022-07-07 19:46:00 culmination_time=2022-07-07 00:41:00 set_time=2022-07-07 05:33:00 aster=<Object type=PLANET name=PLUTO />>]
150160
151-
Timezone can be optionnaly set to adapt the hours to your location:
161+
UTC offset can be optionnaly set to adapt the hours to your location:
152162
153-
>>> get_ephemerides(Position(36.6794, 4.8555), date(2022, 7, 7), timezone=2)
163+
>>> get_ephemerides(Position(36.6794, 4.8555), date(2022, 7, 7), utc_offset=2)
154164
[<AsterEphemerides rise_time=2022-07-07 06:29:00 culmination_time=2022-07-07 13:46:00 set_time=2022-07-07 21:02:00 aster=<Object type=STAR name=SUN />>,
155165
<AsterEphemerides rise_time=2022-07-07 14:16:00 culmination_time=2022-07-07 20:06:00 set_time=2022-07-07 01:27:00 aster=<Object type=SATELLITE name=MOON />>,
156166
<AsterEphemerides rise_time=2022-07-07 05:36:00 culmination_time=2022-07-07 12:58:00 set_time=2022-07-07 20:20:00 aster=<Object type=PLANET name=MERCURY />>,
@@ -163,6 +173,20 @@ def get_ephemerides(
163173
<AsterEphemerides rise_time=2022-07-07 21:46:00 culmination_time=2022-07-07 02:41:00 set_time=2022-07-07 07:33:00 aster=<Object type=PLANET name=PLUTO />>]
164174
165175
176+
Note that the `utc_timezone` argument was named `timezone` before version 1.1. The old name still works, but will be dropped in the future.
177+
178+
>>> get_ephemerides(Position(36.6794, 4.8555), date(2022, 7, 7), timezone=2)
179+
[<AsterEphemerides rise_time=2022-07-07 06:29:00 culmination_time=2022-07-07 13:46:00 set_time=2022-07-07 21:02:00 aster=<Object type=STAR name=SUN />>,
180+
<AsterEphemerides rise_time=2022-07-07 14:16:00 culmination_time=2022-07-07 20:06:00 set_time=2022-07-07 01:27:00 aster=<Object type=SATELLITE name=MOON />>,
181+
<AsterEphemerides rise_time=2022-07-07 05:36:00 culmination_time=2022-07-07 12:58:00 set_time=2022-07-07 20:20:00 aster=<Object type=PLANET name=MERCURY />>,
182+
<AsterEphemerides rise_time=2022-07-07 04:30:00 culmination_time=2022-07-07 11:44:00 set_time=2022-07-07 18:58:00 aster=<Object type=PLANET name=VENUS />>,
183+
<AsterEphemerides rise_time=2022-07-07 02:05:00 culmination_time=2022-07-07 08:39:00 set_time=2022-07-07 15:14:00 aster=<Object type=PLANET name=MARS />>,
184+
<AsterEphemerides rise_time=2022-07-07 01:02:00 culmination_time=2022-07-07 07:11:00 set_time=2022-07-07 13:20:00 aster=<Object type=PLANET name=JUPITER />>,
185+
<AsterEphemerides rise_time=2022-07-07 23:06:00 culmination_time=2022-07-07 04:29:00 set_time=2022-07-07 09:48:00 aster=<Object type=PLANET name=SATURN />>,
186+
<AsterEphemerides rise_time=2022-07-07 02:47:00 culmination_time=2022-07-07 09:42:00 set_time=2022-07-07 16:38:00 aster=<Object type=PLANET name=URANUS />>,
187+
<AsterEphemerides rise_time=2022-07-07 00:31:00 culmination_time=2022-07-07 06:25:00 set_time=2022-07-07 12:20:00 aster=<Object type=PLANET name=NEPTUNE />>,
188+
<AsterEphemerides rise_time=2022-07-07 21:46:00 culmination_time=2022-07-07 02:41:00 set_time=2022-07-07 07:33:00 aster=<Object type=PLANET name=PLUTO />>]
189+
166190
Objects may not rise or set on the given date (e.g. they rise the previous day or set the next day).
167191
In this case, you will get `None` values on the rise or set time.
168192
@@ -230,6 +254,10 @@ def get_ephemerides(
230254
Using a timezone that does not correspond to the place's actual one can impact the returned times.
231255
"""
232256

257+
if argv.get('timezone') is not None:
258+
alert_deprecation("'timezone' argument of the get_ephemerides() function is deprecated. Use utc_offset instead.")
259+
utc_offset = argv.get('timezone')
260+
233261
def get_angle(for_aster: Object):
234262
def fun(time: Time) -> float:
235263
return (
@@ -255,11 +283,11 @@ def fun(time: Time) -> bool:
255283
# but we need it in UTC. Subtracting the timezone to get it in UTC.
256284

257285
start_time = get_timescale().utc(
258-
for_date.year, for_date.month, for_date.day, -timezone
286+
for_date.year, for_date.month, for_date.day, -utc_offset
259287
)
260288

261289
end_time = get_timescale().utc(
262-
for_date.year, for_date.month, for_date.day + 1, -timezone
290+
for_date.year, for_date.month, for_date.day + 1, -utc_offset
263291
)
264292

265293
ephemerides = []
@@ -276,7 +304,7 @@ def fun(time: Time) -> bool:
276304

277305
for i, time in enumerate(times):
278306
time_dt = normalize_datetime(
279-
translate_to_timezone(time.utc_datetime(), to_tz=timezone)
307+
translate_to_timezone(time.utc_datetime(), to_tz=utc_offset)
280308
)
281309

282310
if time_dt is not None and time_dt.day != for_date.day:
@@ -291,16 +319,16 @@ def fun(time: Time) -> bool:
291319
culmination_time = normalize_datetime(
292320
translate_to_timezone(
293321
culmination_time.utc_datetime(),
294-
to_tz=timezone,
322+
to_tz=utc_offset,
295323
)
296324
)
297325

298326
ephemerides.append(
299327
AsterEphemerides(rise_time, culmination_time, set_time, aster=aster)
300328
)
301329
except EphemerisRangeError as error:
302-
start = translate_to_timezone(error.start_time.utc_datetime(), timezone)
303-
end = translate_to_timezone(error.end_time.utc_datetime(), timezone)
330+
start = translate_to_timezone(error.start_time.utc_datetime(), utc_offset)
331+
end = translate_to_timezone(error.end_time.utc_datetime(), utc_offset)
304332

305333
start = date(start.year, start.month, start.day + 1)
306334
end = date(end.year, end.month, end.day - 1)

0 commit comments

Comments
 (0)