Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pendulum/__init__.py: 68%
142 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-30 06:11 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-30 06:11 +0000
1from __future__ import annotations
3import datetime as _datetime
5from typing import Union
6from typing import cast
7from typing import overload
9from pendulum.__version__ import __version__
10from pendulum.constants import DAYS_PER_WEEK
11from pendulum.constants import HOURS_PER_DAY
12from pendulum.constants import MINUTES_PER_HOUR
13from pendulum.constants import MONTHS_PER_YEAR
14from pendulum.constants import SECONDS_PER_DAY
15from pendulum.constants import SECONDS_PER_HOUR
16from pendulum.constants import SECONDS_PER_MINUTE
17from pendulum.constants import WEEKS_PER_YEAR
18from pendulum.constants import YEARS_PER_CENTURY
19from pendulum.constants import YEARS_PER_DECADE
20from pendulum.date import Date
21from pendulum.datetime import DateTime
22from pendulum.day import WeekDay
23from pendulum.duration import Duration
24from pendulum.formatting import Formatter
25from pendulum.helpers import format_diff
26from pendulum.helpers import get_locale
27from pendulum.helpers import locale
28from pendulum.helpers import set_locale
29from pendulum.helpers import week_ends_at
30from pendulum.helpers import week_starts_at
31from pendulum.interval import Interval
32from pendulum.parser import parse
33from pendulum.testing.traveller import Traveller
34from pendulum.time import Time
35from pendulum.tz import UTC
36from pendulum.tz import fixed_timezone
37from pendulum.tz import local_timezone
38from pendulum.tz import set_local_timezone
39from pendulum.tz import test_local_timezone
40from pendulum.tz import timezones
41from pendulum.tz.timezone import FixedTimezone
42from pendulum.tz.timezone import Timezone
45MONDAY = WeekDay.MONDAY
46TUESDAY = WeekDay.TUESDAY
47WEDNESDAY = WeekDay.WEDNESDAY
48THURSDAY = WeekDay.THURSDAY
49FRIDAY = WeekDay.FRIDAY
50SATURDAY = WeekDay.SATURDAY
51SUNDAY = WeekDay.SUNDAY
54_TEST_NOW: DateTime | None = None
55_LOCALE = "en"
56_WEEK_STARTS_AT: WeekDay = WeekDay.MONDAY
57_WEEK_ENDS_AT: WeekDay = WeekDay.SUNDAY
59_formatter = Formatter()
62@overload
63def timezone(name: int) -> FixedTimezone:
64 ...
67@overload
68def timezone(name: str) -> Timezone:
69 ...
72@overload
73def timezone(name: str | int) -> Timezone | FixedTimezone:
74 ...
77def timezone(name: str | int) -> Timezone | FixedTimezone:
78 """
79 Return a Timezone instance given its name.
80 """
81 if isinstance(name, int):
82 return fixed_timezone(name)
84 if name.lower() == "utc":
85 return UTC
87 return Timezone(name)
90def _safe_timezone(
91 obj: str | float | _datetime.tzinfo | Timezone | FixedTimezone | None,
92 dt: _datetime.datetime | None = None,
93) -> Timezone | FixedTimezone:
94 """
95 Creates a timezone instance
96 from a string, Timezone, TimezoneInfo or integer offset.
97 """
98 if isinstance(obj, (Timezone, FixedTimezone)):
99 return obj
101 if obj is None or obj == "local":
102 return local_timezone()
104 if isinstance(obj, (int, float)):
105 obj = int(obj * 60 * 60)
106 elif isinstance(obj, _datetime.tzinfo):
107 # zoneinfo
108 if hasattr(obj, "key"):
109 obj = obj.key
110 # pytz
111 elif hasattr(obj, "localize"):
112 obj = obj.zone # type: ignore[attr-defined]
113 elif obj.tzname(None) == "UTC":
114 return UTC
115 else:
116 offset = obj.utcoffset(dt)
118 if offset is None:
119 offset = _datetime.timedelta(0)
121 obj = int(offset.total_seconds())
123 obj = cast(Union[str, int], obj)
125 return timezone(obj)
128# Public API
129def datetime(
130 year: int,
131 month: int,
132 day: int,
133 hour: int = 0,
134 minute: int = 0,
135 second: int = 0,
136 microsecond: int = 0,
137 tz: str | float | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
138 fold: int = 1,
139 raise_on_unknown_times: bool = False,
140) -> DateTime:
141 """
142 Creates a new DateTime instance from a specific date and time.
143 """
144 return DateTime.create(
145 year,
146 month,
147 day,
148 hour=hour,
149 minute=minute,
150 second=second,
151 microsecond=microsecond,
152 tz=tz,
153 fold=fold,
154 raise_on_unknown_times=raise_on_unknown_times,
155 )
158def local(
159 year: int,
160 month: int,
161 day: int,
162 hour: int = 0,
163 minute: int = 0,
164 second: int = 0,
165 microsecond: int = 0,
166) -> DateTime:
167 """
168 Return a DateTime in the local timezone.
169 """
170 return datetime(
171 year, month, day, hour, minute, second, microsecond, tz=local_timezone()
172 )
175def naive(
176 year: int,
177 month: int,
178 day: int,
179 hour: int = 0,
180 minute: int = 0,
181 second: int = 0,
182 microsecond: int = 0,
183 fold: int = 1,
184) -> DateTime:
185 """
186 Return a naive DateTime.
187 """
188 return DateTime(year, month, day, hour, minute, second, microsecond, fold=fold)
191def date(year: int, month: int, day: int) -> Date:
192 """
193 Create a new Date instance.
194 """
195 return Date(year, month, day)
198def time(hour: int, minute: int = 0, second: int = 0, microsecond: int = 0) -> Time:
199 """
200 Create a new Time instance.
201 """
202 return Time(hour, minute, second, microsecond)
205@overload
206def instance(
207 obj: _datetime.datetime,
208 tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
209) -> DateTime:
210 ...
213@overload
214def instance(
215 obj: _datetime.date,
216 tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
217) -> Date:
218 ...
221@overload
222def instance(
223 obj: _datetime.time,
224 tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
225) -> Time:
226 ...
229def instance(
230 obj: _datetime.datetime | _datetime.date | _datetime.time,
231 tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
232) -> DateTime | Date | Time:
233 """
234 Create a DateTime/Date/Time instance from a datetime/date/time native one.
235 """
236 if isinstance(obj, (DateTime, Date, Time)):
237 return obj
239 if isinstance(obj, _datetime.date) and not isinstance(obj, _datetime.datetime):
240 return date(obj.year, obj.month, obj.day)
242 if isinstance(obj, _datetime.time):
243 return Time.instance(obj, tz=tz)
245 return DateTime.instance(obj, tz=tz)
248def now(tz: str | Timezone | None = None) -> DateTime:
249 """
250 Get a DateTime instance for the current date and time.
251 """
252 return DateTime.now(tz)
255def today(tz: str | Timezone = "local") -> DateTime:
256 """
257 Create a DateTime instance for today.
258 """
259 return now(tz).start_of("day")
262def tomorrow(tz: str | Timezone = "local") -> DateTime:
263 """
264 Create a DateTime instance for tomorrow.
265 """
266 return today(tz).add(days=1)
269def yesterday(tz: str | Timezone = "local") -> DateTime:
270 """
271 Create a DateTime instance for yesterday.
272 """
273 return today(tz).subtract(days=1)
276def from_format(
277 string: str,
278 fmt: str,
279 tz: str | Timezone = UTC,
280 locale: str | None = None,
281) -> DateTime:
282 """
283 Creates a DateTime instance from a specific format.
284 """
285 parts = _formatter.parse(string, fmt, now(tz=tz), locale=locale)
286 if parts["tz"] is None:
287 parts["tz"] = tz
289 return datetime(**parts)
292def from_timestamp(timestamp: int | float, tz: str | Timezone = UTC) -> DateTime:
293 """
294 Create a DateTime instance from a timestamp.
295 """
296 dt = _datetime.datetime.utcfromtimestamp(timestamp)
298 dt = datetime(
299 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond
300 )
302 if tz is not UTC or tz != "UTC":
303 dt = dt.in_timezone(tz)
305 return dt
308def duration(
309 days: float = 0,
310 seconds: float = 0,
311 microseconds: float = 0,
312 milliseconds: float = 0,
313 minutes: float = 0,
314 hours: float = 0,
315 weeks: float = 0,
316 years: float = 0,
317 months: float = 0,
318) -> Duration:
319 """
320 Create a Duration instance.
321 """
322 return Duration(
323 days=days,
324 seconds=seconds,
325 microseconds=microseconds,
326 milliseconds=milliseconds,
327 minutes=minutes,
328 hours=hours,
329 weeks=weeks,
330 years=years,
331 months=months,
332 )
335def interval(start: DateTime, end: DateTime, absolute: bool = False) -> Interval:
336 """
337 Create an Interval instance.
338 """
339 return Interval(start, end, absolute=absolute)
342# Testing
344_traveller = Traveller(DateTime)
346freeze = _traveller.freeze
347travel = _traveller.travel
348travel_to = _traveller.travel_to
349travel_back = _traveller.travel_back
351__all__ = [
352 "__version__",
353 "DAYS_PER_WEEK",
354 "HOURS_PER_DAY",
355 "MINUTES_PER_HOUR",
356 "MONTHS_PER_YEAR",
357 "SECONDS_PER_DAY",
358 "SECONDS_PER_HOUR",
359 "SECONDS_PER_MINUTE",
360 "WEEKS_PER_YEAR",
361 "YEARS_PER_CENTURY",
362 "YEARS_PER_DECADE",
363 "Date",
364 "DateTime",
365 "Duration",
366 "Formatter",
367 "WeekDay",
368 "date",
369 "datetime",
370 "duration",
371 "format_diff",
372 "freeze",
373 "from_format",
374 "from_timestamp",
375 "get_locale",
376 "instance",
377 "interval",
378 "local",
379 "locale",
380 "naive",
381 "now",
382 "set_locale",
383 "week_ends_at",
384 "week_starts_at",
385 "parse",
386 "Interval",
387 "Time",
388 "UTC",
389 "local_timezone",
390 "set_local_timezone",
391 "test_local_timezone",
392 "time",
393 "timezone",
394 "timezones",
395 "today",
396 "tomorrow",
397 "travel",
398 "travel_back",
399 "travel_to",
400 "FixedTimezone",
401 "Timezone",
402 "yesterday",
403]