SPPAS 4.22

https://sppas.org/

Module sppas.src.utils

Class sppasTime

Description

Utility class to represent date time with a string.

How SPPAS works with the date...

Constructor

Create a sppasTime() instance.

Given time must be formatted exactly like: '%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}'

Parameters
  • now: (str) String representing the current time
Example
Example
 >>> p = sppasTime('2018-04-09T15:00:37+02:00')
 >>> p.now
 >>> '2018-04-09T15:00:37+02:00'
 >>> p.gmt
 >>> '+02:00'
View Source
def __init__(self, now=None):
    """Create a sppasTime() instance.

    Given time must be formatted exactly like:
    '%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}'

    :param now: (str) String representing the current time

    :Example:

    >>> p = sppasTime('2018-04-09T15:00:37+02:00')
    >>> p.now
    >>> '2018-04-09T15:00:37+02:00'
    >>> p.gmt
    >>> '+02:00'

    """
    if now is None:
        ctz = -time.altzone if time.localtime(time.time()).tm_isdst and time.daylight else -time.timezone
        self.now = time.strftime('%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}').format(ctz // 3600, ctz % 3600)
    else:
        self.now = now
    if 'T' not in self.now or '-' not in self.now or ':' not in self.now:
        raise UtilsDataTypeError('sppasTime(now)', '%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}', now)
    try:
        self.year = self.now.split('T')[0].split('-')[0]
        self.month = self.now.split('T')[0].split('-')[1]
        self.day = self.now.split('T')[0].split('-')[2]
        self.hours = self.now.split('T')[1].split(':')[0]
        self.min = self.now.split('T')[1].split(':')[1]
        self.sec = self.now[-8:-6]
        self.gmt = self.now[-6:]
    except IndexError:
        raise UtilsDataTypeError('sppasTime(now)', '%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}', now)