Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
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
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
fc76b4d9
Commit
fc76b4d9
authored
6 years ago
by
juga
Committed by
Matt Traudt
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add functions to convert timestamp formats
parent
14dcf633
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sbws/util/timestamp.py
+55
-0
55 additions, 0 deletions
sbws/util/timestamp.py
with
55 additions
and
0 deletions
sbws/util/timestamp.py
0 → 100644
+
55
−
0
View file @
fc76b4d9
"""
Util functions to convert between timestamp formats
"""
from
datetime
import
datetime
def
dt_obj_to_isodt_str
(
dt
):
"""
Convert datetime object to ISO 8601 string.
:param datetime dt: datetime object in UTC timezone
:returns: ISO 8601 string
"""
assert
isinstance
(
dt
,
datetime
)
# Using naive datetime object without timezone, assumed utc
return
dt
.
replace
(
microsecond
=
0
).
isoformat
()
def
unixts_to_dt_obj
(
unixts
):
"""
Convert unix timestamp to naive datetime object in UTC time zone.
:param float/int/str unixts: unix timestamp
:returns: datetime object in UTC timezone
"""
if
isinstance
(
unixts
,
str
):
try
:
unixts
=
int
(
unixts
)
except
ValueError
as
e
:
unixts
=
float
(
unixts
)
if
isinstance
(
unixts
,
float
):
unixts
=
int
(
unixts
)
assert
isinstance
(
unixts
,
int
)
return
datetime
.
utcfromtimestamp
(
unixts
)
def
unixts_to_isodt_str
(
unixts
):
"""
Convert unix timestamp to ISO 8601 string in UTC time zone.
:param float/int/str unixts: unix timestamp
:returns: ISO 8601 string in UTC time zone
"""
return
dt_obj_to_isodt_str
(
unixts_to_dt_obj
(
unixts
))
def
now_isodt_str
():
"""
Return datetime now as ISO 8601 string in UTC time zone.
"""
return
dt_obj_to_isodt_str
(
datetime
.
utcnow
())
def
unixts_to_str
(
unixts
):
"""
Convert unix timestamp integer or float to string
"""
# even if it is only converting to str, ensure that input is nothing else
# than int or float
assert
isinstance
(
unixts
,
int
)
or
isinstance
(
unixts
,
float
)
return
str
(
unixts
)
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