Skip to content
Snippets Groups Projects
Commit 7334f8c2 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Merge branch 'TMSS-3182' into 'master'

TMSS-3182

Closes TMSS-3182

See merge request !11
parents 1c50b7bc 9a0a716b
Branches
No related tags found
1 merge request!11TMSS-3182
Pipeline #108984 passed with warnings
Pipeline: PyCommon

#108985

    # 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_date, month_step=1): def monthRanges(min_date: datetime, max_date: datetime, 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.utcfromtimestamp(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.utcfromtimestamp(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
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment