-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding foundations for a Timestamptz column type
- Loading branch information
1 parent
733b37d
commit 321440e
Showing
7 changed files
with
278 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
import datetime | ||
import typing as t | ||
|
||
from .timestamp import TimestampOffset, TimestampNow, TimestampCustom | ||
|
||
|
||
class TimestamptzOffset(TimestampOffset): | ||
def python(self): | ||
return datetime.datetime.now( | ||
tz=datetime.timezone.utc | ||
) + datetime.timedelta( | ||
days=self.days, | ||
hours=self.hours, | ||
minutes=self.minutes, | ||
seconds=self.seconds, | ||
) | ||
|
||
|
||
class TimestamptzNow(TimestampNow): | ||
def python(self): | ||
return datetime.datetime.now(tz=datetime.timezone.utc) | ||
|
||
|
||
class TimestamptzCustom(TimestampCustom): | ||
@property | ||
def datetime(self): | ||
return datetime.datetime( | ||
year=self.year, | ||
month=self.month, | ||
day=self.day, | ||
hour=self.hour, | ||
second=self.second, | ||
microsecond=self.microsecond, | ||
tzinfo=datetime.timezone.utc, | ||
) | ||
|
||
@classmethod | ||
def from_datetime(cls, instance: datetime.datetime): # type: ignore | ||
if instance.tzinfo is not None: | ||
instance = instance.astimezone(datetime.timezone.utc) | ||
return cls( | ||
year=instance.year, | ||
month=instance.month, | ||
day=instance.month, | ||
hour=instance.hour, | ||
second=instance.second, | ||
microsecond=instance.microsecond, | ||
) | ||
|
||
|
||
TimestamptzArg = t.Union[ | ||
TimestamptzCustom, | ||
TimestamptzNow, | ||
TimestamptzOffset, | ||
None, | ||
datetime.datetime, | ||
] | ||
|
||
|
||
__all__ = [ | ||
"TimestamptzArg", | ||
"TimestamptzCustom", | ||
"TimestamptzNow", | ||
"TimestamptzOffset", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.