Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Portal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fachschaft
Portal
Commits
99557b65
Verified
Commit
99557b65
authored
9 months ago
by
Jakob Moser
Browse files
Options
Downloads
Patches
Plain Diff
Add UtcDateTime type
parent
7990beef
No related branches found
No related tags found
1 merge request
!5
Add basic User auth endpoints (non functional)
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
portal/model/UtcDateTime.py
+51
-0
51 additions, 0 deletions
portal/model/UtcDateTime.py
with
51 additions
and
0 deletions
portal/model/UtcDateTime.py
0 → 100644
+
51
−
0
View file @
99557b65
from
datetime
import
datetime
,
timezone
from
zoneinfo
import
ZoneInfo
from
typing
import
Optional
,
Any
from
sqlalchemy.engine
import
Dialect
import
sqlalchemy.types
as
types
class
UtcDateTime
(
types
.
TypeDecorator
):
"""
A ZonedDateTime object (if there were such a thing in Python) with the timezone fixed to UTC.
This is a poor man
'
s attempt at implementing an Instant (which is what I actually want to store,
but for which there is no Python type) while also staying as close as possible to the types
that do exist in Python.
In the database, a UtcDateTime is generally stored as an ISO string (e.g. 2023-11-14T11:00+00:00).
Further reading on ZonedDateTime:
- https://tc39.es/proposal-temporal/docs/#Temporal-ZonedDateTime
- https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/time/ZonedDateTime.html
"""
impl
=
types
.
TypeEngine
cache_ok
=
True
def
load_dialect_impl
(
self
,
dialect
:
Dialect
)
->
types
.
TypeEngine
[
Any
]:
return
types
.
String
(
32
)
def
process_bind_param
(
self
,
value
:
datetime
,
dialect
:
Dialect
)
->
Optional
[
str
|
datetime
]:
if
value
is
None
:
return
None
return
value
.
astimezone
(
timezone
.
utc
).
isoformat
()
def
process_result_value
(
self
,
value
:
str
|
datetime
,
dialect
:
Dialect
)
->
Optional
[
datetime
]:
if
value
is
None
:
return
None
result
=
datetime
.
fromisoformat
(
value
)
assert
(
result
.
tzinfo
==
timezone
.
utc
),
"
tzinfo of returned object is expected to be timezone.utc
"
return
result
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment