Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PyCommon
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TMSS
Libraries
PyCommon
Commits
7334f8c2
Commit
7334f8c2
authored
5 months ago
by
Jorrit Schaap
Browse files
Options
Downloads
Plain Diff
Merge branch '
TMSS-3182
' into 'master'
TMSS-3182
Closes
TMSS-3182
See merge request
!11
parents
1c50b7bc
9a0a716b
Branches
Branches containing commit
No related tags found
1 merge request
!11
TMSS-3182
Pipeline
#108984
passed with warnings
5 months ago
Stage: prepare
Stage: lint
Stage: test
Stage: package
Stage: images
Stage: integration
Pipeline: PyCommon
#108985
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tmss_pycommon/datetimeutils.py
+17
-15
17 additions, 15 deletions
tmss_pycommon/datetimeutils.py
with
17 additions
and
15 deletions
tmss_pycommon/datetimeutils.py
+
17
−
15
View file @
7334f8c2
# Copyright (C) 2012 ASTRON (Netherlands Institute for Radio Astronomy)
# Copyright (C) 2012 ASTRON (Netherlands Institute for Radio Astronomy)
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-License-Identifier: GPL-3.0-or-later
from
datetime
import
datetime
,
timedelta
from
datetime
import
datetime
,
timedelta
,
timezone
def
monthRanges
(
min_date
,
max_dat
e
,
month_step
=
1
):
def
monthRanges
(
min_date
:
datetime
,
max_date
:
datetim
e
,
month_step
:
int
=
1
):
ranges
=
[]
ranges
=
[]
min_month_start
=
datetime
(
min_date
.
year
,
min_date
.
month
,
1
,
tzinfo
=
min_date
.
tzinfo
)
min_month_start
=
datetime
(
min_date
.
year
,
min_date
.
month
,
1
,
tzinfo
=
min_date
.
tzinfo
)
...
@@ -25,7 +25,7 @@ def monthRanges(min_date, max_date, month_step=1):
...
@@ -25,7 +25,7 @@ def monthRanges(min_date, max_date, month_step=1):
return
ranges
return
ranges
def
totalSeconds
(
td_value
):
def
totalSeconds
(
td_value
)
->
float
:
'''
Return the total number of fractional seconds contained in the duration.
'''
Return the total number of fractional seconds contained in the duration.
For Python < 2.7 compute it, else use total_seconds() method.
For Python < 2.7 compute it, else use total_seconds() method.
'''
'''
...
@@ -34,7 +34,7 @@ def totalSeconds(td_value):
...
@@ -34,7 +34,7 @@ def totalSeconds(td_value):
return
(
td_value
.
microseconds
+
(
td_value
.
seconds
+
td_value
.
days
*
86400
)
*
1000000
)
/
1000000.0
return
(
td_value
.
microseconds
+
(
td_value
.
seconds
+
td_value
.
days
*
86400
)
*
1000000
)
/
1000000.0
def
format_timedelta
(
td
)
:
def
format_timedelta
(
td
:
timedelta
)
->
str
:
'''
Return string representation of timedelta value td, which works even for negative values.
'''
Return string representation of timedelta value td, which works even for negative values.
Normal python is weird: str(timedelta(hours=-1)) becomes
'
-1 day, 23:00:00
'
Normal python is weird: str(timedelta(hours=-1)) becomes
'
-1 day, 23:00:00
'
With this function: format_timedelta(timedelta(hours=-1)) becomes
'
-1:00:00
'
which makes much more sense!
With this function: format_timedelta(timedelta(hours=-1)) becomes
'
-1:00:00
'
which makes much more sense!
...
@@ -45,7 +45,9 @@ def format_timedelta(td):
...
@@ -45,7 +45,9 @@ def format_timedelta(td):
def
parseDatetime
(
date_time
:
str
)
->
datetime
:
def
parseDatetime
(
date_time
:
str
)
->
datetime
:
"""
Parse the datetime format used in LOFAR parsets.
"""
"""
Parse the datetime format used in LOFAR parsets.
"""
return
datetime
.
strptime
(
date_time
,
(
'
%Y-%m-%d %H:%M:%S.%f
'
if
'
.
'
in
date_time
else
'
%Y-%m-%d %H:%M:%S
'
))
timestamp
=
datetime
.
strptime
(
date_time
,
(
'
%Y-%m-%d %H:%M:%S.%f
'
if
'
.
'
in
date_time
else
'
%Y-%m-%d %H:%M:%S
'
))
timestamp
.
tzinfo
=
timezone
.
utc
# LOFAR uses UTC
return
timestamp
def
formatDatetime
(
timestamp
:
datetime
)
->
str
:
def
formatDatetime
(
timestamp
:
datetime
)
->
str
:
"""
Format the timestamp as used in LOFAR parsets.
"""
"""
Format the timestamp as used in LOFAR parsets.
"""
...
@@ -56,7 +58,7 @@ def formatDatetime(timestamp: datetime) -> str:
...
@@ -56,7 +58,7 @@ def formatDatetime(timestamp: datetime) -> str:
MDJ_EPOCH
=
datetime
(
1858
,
11
,
17
,
0
,
0
,
0
)
MDJ_EPOCH
=
datetime
(
1858
,
11
,
17
,
0
,
0
,
0
)
def
to_modified_julian_date
(
timestamp
)
:
def
to_modified_julian_date
(
timestamp
:
datetime
)
->
float
:
'''
'''
computes the modified_julian_date from a python datetime timestamp
computes the modified_julian_date from a python datetime timestamp
:param timestamp: datetime a python datetime timestamp
:param timestamp: datetime a python datetime timestamp
...
@@ -64,7 +66,7 @@ def to_modified_julian_date(timestamp):
...
@@ -64,7 +66,7 @@ def to_modified_julian_date(timestamp):
'''
'''
return
to_modified_julian_date_in_seconds
(
timestamp
)
/
86400.0
return
to_modified_julian_date_in_seconds
(
timestamp
)
/
86400.0
def
to_modified_julian_date_in_seconds
(
timestamp
)
:
def
to_modified_julian_date_in_seconds
(
timestamp
:
datetime
)
->
float
:
'''
'''
computes the modified_julian_date (in seconds as opposed to the official days) from a python datetime timestamp
computes the modified_julian_date (in seconds as opposed to the official days) from a python datetime timestamp
:param timestamp: datetime a python datetime timestamp
:param timestamp: datetime a python datetime timestamp
...
@@ -72,7 +74,7 @@ def to_modified_julian_date_in_seconds(timestamp):
...
@@ -72,7 +74,7 @@ def to_modified_julian_date_in_seconds(timestamp):
'''
'''
return
totalSeconds
(
timestamp
-
MDJ_EPOCH
)
return
totalSeconds
(
timestamp
-
MDJ_EPOCH
)
def
from_modified_julian_date
(
modified_julian_date
)
:
def
from_modified_julian_date
(
modified_julian_date
:
float
)
->
datetime
:
'''
'''
computes the python datetime timestamp from a modified_julian_date
computes the python datetime timestamp from a modified_julian_date
:param modified_julian_date: double, a timestamp expressed in modified_julian_date format (fractional number of days since MJD_EPOCH)
:param modified_julian_date: double, a timestamp expressed in modified_julian_date format (fractional number of days since MJD_EPOCH)
...
@@ -80,7 +82,7 @@ def from_modified_julian_date(modified_julian_date):
...
@@ -80,7 +82,7 @@ def from_modified_julian_date(modified_julian_date):
'''
'''
return
from_modified_julian_date_in_seconds
(
modified_julian_date
*
86400.0
)
return
from_modified_julian_date_in_seconds
(
modified_julian_date
*
86400.0
)
def
from_modified_julian_date_in_seconds
(
modified_julian_date_secs
)
:
def
from_modified_julian_date_in_seconds
(
modified_julian_date_secs
:
float
)
->
datetime
:
'''
'''
computes the python datetime timestamp from a modified_julian_date (in seconds as opposed to the official days)
computes the python datetime timestamp from a modified_julian_date (in seconds as opposed to the official days)
:param modified_julian_date: double, a timestamp expressed in modified_julian_date format (fractional number of seconds since MJD_EPOCH)
:param modified_julian_date: double, a timestamp expressed in modified_julian_date format (fractional number of seconds since MJD_EPOCH)
...
@@ -88,15 +90,15 @@ def from_modified_julian_date_in_seconds(modified_julian_date_secs):
...
@@ -88,15 +90,15 @@ def from_modified_julian_date_in_seconds(modified_julian_date_secs):
'''
'''
return
MDJ_EPOCH
+
timedelta
(
seconds
=
modified_julian_date_secs
)
return
MDJ_EPOCH
+
timedelta
(
seconds
=
modified_julian_date_secs
)
def
to_seconds_since_unix_epoch
(
timestamp
)
:
def
to_seconds_since_unix_epoch
(
timestamp
:
datetime
)
->
float
:
'''
'''
computes the (fractional) number of seconds since the unix epoch for a python datetime.timestamp
computes the (fractional) number of seconds since the unix epoch for a python datetime.timestamp
:param timestamp: datetime a python datetime timestamp (in UTC)
:param timestamp: datetime a python datetime timestamp (in UTC)
:return: double, the (fractional) number of seconds since the unix epoch
:return: double, the (fractional) number of seconds since the unix epoch
'''
'''
return
totalSeconds
(
timestamp
-
datetime
.
utc
fromtimestamp
(
0
))
return
totalSeconds
(
timestamp
-
datetime
.
fromtimestamp
(
0
,
tz
=
timezone
.
utc
))
def
to_milliseconds_since_unix_epoch
(
timestamp
)
:
def
to_milliseconds_since_unix_epoch
(
timestamp
:
datetime
)
->
float
:
'''
'''
computes the (fractional) number of milliseconds since the unix epoch for a python datetime.timestamp
computes the (fractional) number of milliseconds since the unix epoch for a python datetime.timestamp
:param timestamp: datetime a python datetime timestamp
:param timestamp: datetime a python datetime timestamp
...
@@ -104,15 +106,15 @@ def to_milliseconds_since_unix_epoch(timestamp):
...
@@ -104,15 +106,15 @@ def to_milliseconds_since_unix_epoch(timestamp):
'''
'''
return
1000.0
*
to_seconds_since_unix_epoch
(
timestamp
)
return
1000.0
*
to_seconds_since_unix_epoch
(
timestamp
)
def
from_seconds_since_unix_epoch
(
nr_of_seconds_since_epoch
)
:
def
from_seconds_since_unix_epoch
(
nr_of_seconds_since_epoch
:
float
)
->
datetime
:
'''
'''
computes a python datetime.timestamp given the (fractional) number of seconds since the unix epoch
computes a python datetime.timestamp given the (fractional) number of seconds since the unix epoch
:param double or int, the (fractional) number of seconds since the unix epoch
:param double or int, the (fractional) number of seconds since the unix epoch
:return: timestamp: datetime a python datetime timestamp (in UTC)
:return: timestamp: datetime a python datetime timestamp (in UTC)
'''
'''
return
datetime
.
utc
fromtimestamp
(
nr_of_seconds_since_epoch
)
return
datetime
.
fromtimestamp
(
nr_of_seconds_since_epoch
,
tz
=
timezone
.
utc
)
def
from_milliseconds_since_unix_epoch
(
nr_of_milliseconds_since_epoch
)
:
def
from_milliseconds_since_unix_epoch
(
nr_of_milliseconds_since_epoch
:
float
)
->
datetime
:
'''
'''
computes a python datetime.timestamp given the (fractional) number of milliseconds since the unix epoch
computes a python datetime.timestamp given the (fractional) number of milliseconds since the unix epoch
:param double or int, the (fractional) number of milliseconds since the unix epoch
:param double or int, the (fractional) number of milliseconds since the unix epoch
...
...
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