Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
datascout
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
ABP Computing
ABP Computing Sandbox
datascout
Commits
acef391a
Commit
acef391a
authored
3 years ago
by
[ACC] Elena Operation
Browse files
Options
Downloads
Patches
Plain Diff
changed format of documentation
parent
a02ba74c
Branches
add_time_handling
Branches containing commit
Tags
v0.0.4
Tags containing commit
No related merge requests found
Pipeline
#3615207
passed
3 years ago
Stage: test
Stage: deploy
Changes
1
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
datascout/_timescout.py
+36
-25
36 additions, 25 deletions
datascout/_timescout.py
with
36 additions
and
25 deletions
datascout/_timescout.py
+
36
−
25
View file @
acef391a
...
@@ -5,14 +5,16 @@ import pytz
...
@@ -5,14 +5,16 @@ import pytz
import
datetime
import
datetime
def
unixtime_to_datetime
(
unixtime_ns
,
time_zone
=
'
CERN
'
):
def
unixtime_to_datetime
(
unixtime_ns
,
time_zone
=
'
CERN
'
):
'''
'''
Converts a timestamp in ns from epoch (UTC) to a datetime on the given time_zone
Converts a timestamp in ns from epoch (UTC) to a datetime on the given time_zone
Note, a timezone called
"
CERN
"
(default) can be used as timezone of Geneva area.
Note, a timezone called
"
CERN
"
(default) can be used as timezone of Geneva area.
:param unixtime_ns: ns from epoch
Args:
:param time_zone: desired timezone for the output (default=
'
CERN
'
)
unixtime_ns (int): ns from epoch
:return:
time_zone (string): desired timezone for the output (default=
'
CERN
'
)
Returns:
(datetime): datetime object representing the given timestamp
'''
'''
if
isinstance
(
time_zone
,
str
):
if
isinstance
(
time_zone
,
str
):
if
time_zone
.
upper
()
==
'
UTC
'
:
if
time_zone
.
upper
()
==
'
UTC
'
:
...
@@ -30,13 +32,15 @@ def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'):
...
@@ -30,13 +32,15 @@ def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'):
return
datetime
.
datetime
.
fromtimestamp
(
unixtime_ns
/
1e9
,
tz
=
time_zone
)
return
datetime
.
datetime
.
fromtimestamp
(
unixtime_ns
/
1e9
,
tz
=
time_zone
)
def
datetime_to_unixtime
(
date_time
):
def
datetime_to_unixtime
(
date_time
):
'''
'''
Converts a datetime object into a unixtimestamp in ns
Converts a datetime object into a unixtimestamp in ns
Note that datetime objects are precise only down to the microsecond.
Note that datetime objects are precise only down to the microsecond.
:param date_time: datetime object
Args:
:return: ns from epoch UTC
date_time (datetime): datetime object to convert
Returns:
(int): ns from epoch UTC
'''
'''
if
date_time
.
tzinfo
is
None
:
if
date_time
.
tzinfo
is
None
:
epoch
=
datetime
.
datetime
.
fromtimestamp
(
0
,
tz
=
None
)
epoch
=
datetime
.
datetime
.
fromtimestamp
(
0
,
tz
=
None
)
...
@@ -45,36 +49,43 @@ def datetime_to_unixtime(date_time):
...
@@ -45,36 +49,43 @@ def datetime_to_unixtime(date_time):
return
int
((
date_time
-
epoch
).
total_seconds
()
*
1e6
)
*
1000
return
int
((
date_time
-
epoch
).
total_seconds
()
*
1e6
)
*
1000
def
string_to_datetime
(
date_string
):
def
string_to_datetime
(
date_string
):
'''
'''
Uses standard datetime function to parse a string in the ISO format
Uses standard datetime function to parse a string in the ISO format
e
xample:
E
xample
s
:
In:
string_to_datetime(
'
2022-01-21 15:55:34.123555
'
)
>>>
string_to_datetime
(
'
2022-01-21 15:55:34.123555
'
)
Out:
datetime.datetime(2022, 1, 21, 15, 55, 34, 123555, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600),
'
CET
'
))
datetime
.
datetime
(
2022
,
1
,
21
,
15
,
55
,
34
,
123555
,
tzinfo
=
datetime
.
timezone
(
datetime
.
timedelta
(
seconds
=
3600
),
'
CET
'
))
:param date_string:
Args:
:return:
date_string (string): string to parse
Returns:
(datetime): datetime object with timezone representing the input string date and time
'''
'''
return
datetime
.
datetime
.
fromisoformat
(
date_string
).
astimezone
()
return
datetime
.
datetime
.
fromisoformat
(
date_string
).
astimezone
()
def
string_to_unixtime
(
date_string
):
def
string_to_unixtime
(
date_string
):
'''
'''
Simple parse of ISO string and get a unix timestamp
simple parse of ISO string
:param date_string: date+time in ISO string format
Args:
:return: unix timestamp in ns
date_string (string): date+time in ISO string format
Returns:
(int): unix timestamp in ns
'''
'''
return
datetime_to_unixtime
(
string_to_datetime
(
date_string
))
return
datetime_to_unixtime
(
string_to_datetime
(
date_string
))
def
unixtime_to_string
(
unixtime_ns
,
time_zone
=
'
utc
'
,
format
=
'
%Y-%m-%d %H:%M:%S.%f
'
):
def
unixtime_to_string
(
unixtime_ns
,
time_zone
=
'
CERN
'
,
format
=
'
%Y-%m-%d %H:%M:%S.%f
'
):
'''
'''
Converts a unix timestamp in ns into a string on a given timezone and format
Converts a unix timestamp in ns into a string on a given timezone and format
:param unixtime_ns:
Args:
:param time_zone:
unixtime_ns (int): timestamp in ns to convert
:param format:
time_zone (string): timezone to use for the conversion (default=
'
CERN
'
)
:return: string
format (string): (default=
'
%Y-%m-%d %H:%M:%S.%f
'
)
Returns:
(string): string representation of the input timestamp
'''
'''
return
unixtime_to_datetime
(
unixtime_ns
,
time_zone
=
time_zone
).
strftime
(
format
)
return
unixtime_to_datetime
(
unixtime_ns
,
time_zone
=
time_zone
).
strftime
(
format
)
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