|
30 | 30 | from .date import Date |
31 | 31 | from .exceptions import PendulumException |
32 | 32 | from .helpers import add_duration |
| 33 | +from .helpers import get_test_now |
| 34 | +from .helpers import has_test_now |
33 | 35 | from .period import Period |
34 | 36 | from .time import Time |
35 | 37 | from .tz import UTC |
| 38 | +from .tz import local_timezone |
36 | 39 | from .tz.timezone import FixedTimezone |
37 | 40 | from .tz.timezone import Timezone |
38 | 41 | from .utils._compat import PY38 |
@@ -72,12 +75,79 @@ class DateTime(datetime.datetime, Date): |
72 | 75 | "century", |
73 | 76 | ] |
74 | 77 |
|
| 78 | + @classmethod |
| 79 | + def create( |
| 80 | + cls, |
| 81 | + year: int, |
| 82 | + month: int, |
| 83 | + day: int, |
| 84 | + hour: int = 0, |
| 85 | + minute: int = 0, |
| 86 | + second: int = 0, |
| 87 | + microsecond: int = 0, |
| 88 | + tz: Optional[Union[str, float, Timezone]] = UTC, |
| 89 | + fold: Optional[int] = 1, |
| 90 | + raise_on_unknown_times: bool = False, |
| 91 | + ) -> "DateTime": |
| 92 | + """ |
| 93 | + Creates a new DateTime instance from a specific date and time. |
| 94 | + """ |
| 95 | + if tz is not None: |
| 96 | + tz = pendulum._safe_timezone(tz) |
| 97 | + |
| 98 | + dt = datetime.datetime( |
| 99 | + year, month, day, hour, minute, second, microsecond, fold=fold |
| 100 | + ) |
| 101 | + |
| 102 | + if tz is not None: |
| 103 | + dt = tz.convert(dt, raise_on_unknown_times=raise_on_unknown_times) |
| 104 | + |
| 105 | + return cls( |
| 106 | + dt.year, |
| 107 | + dt.month, |
| 108 | + dt.day, |
| 109 | + dt.hour, |
| 110 | + dt.minute, |
| 111 | + dt.second, |
| 112 | + dt.microsecond, |
| 113 | + tzinfo=dt.tzinfo, |
| 114 | + fold=dt.fold, |
| 115 | + ) |
| 116 | + |
75 | 117 | @classmethod |
76 | 118 | def now(cls, tz: Optional[Union[str, Timezone]] = None) -> "DateTime": |
77 | 119 | """ |
78 | 120 | Get a DateTime instance for the current date and time. |
79 | 121 | """ |
80 | | - return pendulum.now(tz) |
| 122 | + if has_test_now(): |
| 123 | + test_instance = get_test_now() |
| 124 | + _tz = pendulum._safe_timezone(tz) |
| 125 | + |
| 126 | + if tz is not None and _tz != test_instance.timezone: |
| 127 | + test_instance = test_instance.in_tz(_tz) |
| 128 | + |
| 129 | + return test_instance |
| 130 | + |
| 131 | + if tz is None or tz == "local": |
| 132 | + dt = datetime.datetime.now(local_timezone()) |
| 133 | + elif tz is UTC or tz == "UTC": |
| 134 | + dt = datetime.datetime.now(UTC) |
| 135 | + else: |
| 136 | + dt = datetime.datetime.now(UTC) |
| 137 | + tz = pendulum._safe_timezone(tz) |
| 138 | + dt = dt.astimezone(tz) |
| 139 | + |
| 140 | + return cls( |
| 141 | + dt.year, |
| 142 | + dt.month, |
| 143 | + dt.day, |
| 144 | + dt.hour, |
| 145 | + dt.minute, |
| 146 | + dt.second, |
| 147 | + dt.microsecond, |
| 148 | + tzinfo=dt.tzinfo, |
| 149 | + fold=dt.fold, |
| 150 | + ) |
81 | 151 |
|
82 | 152 | @classmethod |
83 | 153 | def utcnow(cls) -> "DateTime": |
@@ -124,7 +194,7 @@ def set( |
124 | 194 | if tz is None: |
125 | 195 | tz = self.tz |
126 | 196 |
|
127 | | - return pendulum.datetime( |
| 197 | + return DateTime.create( |
128 | 198 | year, month, day, hour, minute, second, microsecond, tz=tz |
129 | 199 | ) |
130 | 200 |
|
@@ -428,7 +498,7 @@ def is_long_year(self) -> bool: |
428 | 498 | See link `https://en.wikipedia.org/wiki/ISO_8601#Week_dates`_ |
429 | 499 | """ |
430 | 500 | return ( |
431 | | - pendulum.datetime(self.year, 12, 28, 0, 0, 0, tz=self.tz).isocalendar()[1] |
| 501 | + DateTime.create(self.year, 12, 28, 0, 0, 0, tz=self.tz).isocalendar()[1] |
432 | 502 | == 53 |
433 | 503 | ) |
434 | 504 |
|
@@ -502,7 +572,7 @@ def add( |
502 | 572 | ) |
503 | 573 |
|
504 | 574 | if units_of_variable_length or self.tzinfo is None: |
505 | | - return pendulum.datetime( |
| 575 | + return DateTime.create( |
506 | 576 | dt.year, |
507 | 577 | dt.month, |
508 | 578 | dt.day, |
@@ -1225,7 +1295,7 @@ def replace( |
1225 | 1295 | if fold is None: |
1226 | 1296 | fold = self.fold |
1227 | 1297 |
|
1228 | | - return pendulum.datetime( |
| 1298 | + return DateTime.create( |
1229 | 1299 | year, month, day, hour, minute, second, microsecond, tz=tzinfo, fold=fold |
1230 | 1300 | ) |
1231 | 1301 |
|
|
0 commit comments