Show More
@@ -1,84 +1,82 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Timezone utilities |
|
3 | Timezone utilities | |
4 |
|
4 | |||
5 | Just UTC-awareness right now |
|
5 | Just UTC-awareness right now | |
6 |
|
6 | |||
7 | Deprecated since IPython 8.19.0. |
|
7 | Deprecated since IPython 8.19.0. | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | # ----------------------------------------------------------------------------- |
|
10 | # ----------------------------------------------------------------------------- | |
11 | # Copyright (C) 2013 The IPython Development Team |
|
11 | # Copyright (C) 2013 The IPython Development Team | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | # ----------------------------------------------------------------------------- |
|
15 | # ----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | # ----------------------------------------------------------------------------- |
|
17 | # ----------------------------------------------------------------------------- | |
18 | # Imports |
|
18 | # Imports | |
19 | # ----------------------------------------------------------------------------- |
|
19 | # ----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | import warnings |
|
21 | import warnings | |
22 | from datetime import tzinfo, timedelta, datetime |
|
22 | from datetime import tzinfo, timedelta, datetime | |
23 |
|
23 | |||
24 | # ----------------------------------------------------------------------------- |
|
24 | # ----------------------------------------------------------------------------- | |
25 | # Code |
|
25 | # Code | |
26 | # ----------------------------------------------------------------------------- |
|
26 | # ----------------------------------------------------------------------------- | |
27 | __all__ = ["tzUTC", "utc_aware", "utcfromtimestamp", "utcnow"] |
|
27 | __all__ = ["tzUTC", "utc_aware", "utcfromtimestamp", "utcnow"] | |
28 |
|
28 | |||
29 | # Enable display of DeprecrationWarning for this module explicitly. |
|
|||
30 | warnings.filterwarnings("default", category=DeprecationWarning, module=__name__) |
|
|||
31 |
|
29 | |||
32 | # constant for zero offset |
|
30 | # constant for zero offset | |
33 | ZERO = timedelta(0) |
|
31 | ZERO = timedelta(0) | |
34 |
|
32 | |||
35 |
|
33 | |||
36 | def __getattr__(name): |
|
34 | def __getattr__(name): | |
37 | if name not in __all__: |
|
35 | if name not in __all__: | |
38 | err = f"IPython.utils.tz is deprecated and has no attribute {name}" |
|
36 | err = f"IPython.utils.tz is deprecated and has no attribute {name}" | |
39 | raise AttributeError(err) |
|
37 | raise AttributeError(err) | |
40 |
|
38 | |||
41 | _warn_deprecated() |
|
39 | _warn_deprecated() | |
42 |
|
40 | |||
43 | return getattr(name) |
|
41 | return getattr(name) | |
44 |
|
42 | |||
45 |
|
43 | |||
46 | def _warn_deprecated(): |
|
44 | def _warn_deprecated(): | |
47 | msg = "The module `IPython.utils.tz` is deprecated and will be completely removed." |
|
45 | msg = "The module `IPython.utils.tz` is deprecated and will be completely removed." | |
48 | warnings.warn(msg, category=DeprecationWarning, stacklevel=2) |
|
46 | warnings.warn(msg, category=DeprecationWarning, stacklevel=2) | |
49 |
|
47 | |||
50 |
|
48 | |||
51 | class tzUTC(tzinfo): |
|
49 | class tzUTC(tzinfo): | |
52 | """tzinfo object for UTC (zero offset) |
|
50 | """tzinfo object for UTC (zero offset) | |
53 |
|
51 | |||
54 | Deprecated since IPython 8.19.0. |
|
52 | Deprecated since IPython 8.19.0. | |
55 | """ |
|
53 | """ | |
56 |
|
54 | |||
57 | _warn_deprecated() |
|
55 | _warn_deprecated() | |
58 |
|
56 | |||
59 | def utcoffset(self, d): |
|
57 | def utcoffset(self, d): | |
60 | return ZERO |
|
58 | return ZERO | |
61 |
|
59 | |||
62 | def dst(self, d): |
|
60 | def dst(self, d): | |
63 | return ZERO |
|
61 | return ZERO | |
64 |
|
62 | |||
65 |
|
63 | |||
66 | UTC = tzUTC() # type: ignore[abstract] |
|
64 | UTC = tzUTC() # type: ignore[abstract] | |
67 |
|
65 | |||
68 |
|
66 | |||
69 | def utc_aware(unaware): |
|
67 | def utc_aware(unaware): | |
70 | """decorator for adding UTC tzinfo to datetime's utcfoo methods |
|
68 | """decorator for adding UTC tzinfo to datetime's utcfoo methods | |
71 |
|
69 | |||
72 | Deprecated since IPython 8.19.0. |
|
70 | Deprecated since IPython 8.19.0. | |
73 | """ |
|
71 | """ | |
74 |
|
72 | |||
75 | def utc_method(*args, **kwargs): |
|
73 | def utc_method(*args, **kwargs): | |
76 | _warn_deprecated() |
|
74 | _warn_deprecated() | |
77 | dt = unaware(*args, **kwargs) |
|
75 | dt = unaware(*args, **kwargs) | |
78 | return dt.replace(tzinfo=UTC) |
|
76 | return dt.replace(tzinfo=UTC) | |
79 |
|
77 | |||
80 | return utc_method |
|
78 | return utc_method | |
81 |
|
79 | |||
82 |
|
80 | |||
83 | utcfromtimestamp = utc_aware(datetime.utcfromtimestamp) |
|
81 | utcfromtimestamp = utc_aware(datetime.utcfromtimestamp) | |
84 | utcnow = utc_aware(datetime.utcnow) |
|
82 | utcnow = utc_aware(datetime.utcnow) |
General Comments 0
You need to be logged in to leave comments.
Login now