11from __future__ import annotations
22
33import re
4+ import warnings
45from datetime import UTC , datetime
56
67from whenever import Date , Instant , Time , ZonedDateTime
@@ -16,17 +17,26 @@ class TimestampFormatError(ValueError): ...
1617
1718
1819class Timestamp :
19- obj : ZonedDateTime
20+ _obj : ZonedDateTime
2021
2122 def __init__ (self , value : str | ZonedDateTime | Timestamp | None = None ):
2223 if value and isinstance (value , ZonedDateTime ):
23- self .obj = value
24+ self ._obj = value
2425 elif value and isinstance (value , self .__class__ ):
25- self .obj = value .obj
26+ self ._obj = value ._obj
2627 elif isinstance (value , str ):
27- self .obj = self ._parse_string (value )
28+ self ._obj = self ._parse_string (value )
2829 else :
29- self .obj = ZonedDateTime .now ("UTC" )
30+ self ._obj = ZonedDateTime .now ("UTC" )
31+
32+ @property
33+ def obj (self ) -> ZonedDateTime :
34+ warnings .warn (
35+ "Direct access to obj property is deprecated. Use to_string(), to_timestamp(), or to_datetime() instead." ,
36+ UserWarning ,
37+ stacklevel = 2 ,
38+ )
39+ return self ._obj
3040
3141 @classmethod
3242 def _parse_string (cls , value : str ) -> ZonedDateTime :
@@ -37,8 +47,8 @@ def _parse_string(cls, value: str) -> ZonedDateTime:
3747 pass
3848
3949 try :
40- instant_date_ = Instant .parse_common_iso (value )
41- return instant_date_ .to_tz ("UTC" )
50+ instant_date = Instant .parse_common_iso (value )
51+ return instant_date .to_tz ("UTC" )
4252 except ValueError :
4353 pass
4454
@@ -65,14 +75,16 @@ def __repr__(self) -> str:
6575
6676 def to_string (self , with_z : bool = True ) -> str :
6777 if with_z :
68- return self .obj .instant ().format_common_iso ()
69- iso8601_string = self .obj .format_common_iso ()
70- if iso8601_string [- 1 ] == "Z" :
71- iso8601_string = iso8601_string [:- 1 ] + "+00:00"
72- return iso8601_string
78+ return self ._obj .instant ().format_common_iso ()
79+ # iso8601_string = self._obj.format_common_iso()
80+
81+ # self.to_datetime().isoformat()
82+ # if iso8601_string[-1] == "Z":
83+ # iso8601_string = iso8601_string[:-1] + "+00:00"
84+ return self .to_datetime ().isoformat ()
7385
7486 def to_timestamp (self ) -> int :
75- return self .obj .timestamp ()
87+ return self ._obj .timestamp ()
7688
7789 def to_datetime (self ) -> datetime :
7890 time_str = self .to_string ()
@@ -81,31 +93,31 @@ def to_datetime(self) -> datetime:
8193 def __eq__ (self , other : object ) -> bool :
8294 if not isinstance (other , Timestamp ):
8395 return NotImplemented
84- return self .obj == other .obj
96+ return self ._obj == other ._obj
8597
8698 def __lt__ (self , other : object ) -> bool :
8799 if not isinstance (other , Timestamp ):
88100 return NotImplemented
89- return self .obj < other .obj
101+ return self ._obj < other ._obj
90102
91103 def __gt__ (self , other : object ) -> bool :
92104 if not isinstance (other , Timestamp ):
93105 return NotImplemented
94- return self .obj > other .obj
106+ return self ._obj > other ._obj
95107
96108 def __le__ (self , other : object ) -> bool :
97109 if not isinstance (other , Timestamp ):
98110 return NotImplemented
99- return self .obj <= other .obj
111+ return self ._obj <= other ._obj
100112
101113 def __ge__ (self , other : object ) -> bool :
102114 if not isinstance (other , Timestamp ):
103115 return NotImplemented
104- return self .obj >= other .obj
116+ return self ._obj >= other ._obj
105117
106118 def __hash__ (self ) -> int :
107119 return hash (self .to_string ())
108120
109121 def add_delta (self , hours : int = 0 , minutes : int = 0 , seconds : int = 0 , microseconds : int = 0 ) -> Timestamp :
110- time = self .obj .add (hours = hours , minutes = minutes , seconds = seconds , microseconds = microseconds )
122+ time = self ._obj .add (hours = hours , minutes = minutes , seconds = seconds , microseconds = microseconds )
111123 return Timestamp (time )
0 commit comments