Time

spiceminer ships its own time implementation to allow custom features and more consistency than the standart datetime module.

Differences to datetime

It’s a float:

That means it can be used in range functions that support floats like numpy.arange or numpy.linspace.

It also supports all arithmetical operations, allowing much simpler calculations like Time(2015) + 20 to add 20 seconds instead of having to generate a new object explicitly. It also has constants for minute, hour, and day to simpliefy use in range functions. For example numpy.arange(Time(2000), Time(2001), Time.DAY) produces an array with each entry representing a day in the year 2000.

Day of year:
Time has constructor and attribute for handling day of year instead of handling month, day, hour, etc. seperately.
No timezones:
You don’t have to handle conversion to UTC and timezone related weirdness.

API

class spiceminer.Time(year=1970, month=1, day=1, hour=0, minute=0, second=0)

A powerfull POSIX time representation that always references UTC. It is a subclass of numbers.Real, so it acts like a float in mathematical operations.

Comparison operations also support datetime objects.

Addition and subtraction operations yield new Time objects when the left operand is already a Time object. They also support timedelta objects as right operands.

Parameters:

year: int, optional

[1..9999]

month: int, optional

[1..12]

day: int, optional

[1..31]

hour: int, optional

[0..23]

minute: int, optional

[0..59]

second: float, optional

[0..60)

Raises:

ValueError

If an argument is out of bounds.

TypeError

If an argument has the wrong type.

Examples

>>> Time()
Time(year=1970, month=1, day=1, hour=0, minute=0, second=0)
>>> Time(2000, 5, hour=14)
Time(year=2000, month=5, day=1, hour=14, minute=0, second=0)

Attributes

classattribute MINUTE: float One minute in seconds
classattribute HOUR: float One hour in seconds
classattribute DAY: float One day in seconds
classattribute WEEK: float One week (7 days) in seconds
year: int  
month: int  
day: int  
hour: int  
minute: int  
second: float  

Methods

classmethod now()

Get current time.

Returns:

Time

New POSIX timestamp.

classmethod fromposix(timestamp)

Generate a Time instance from a single number.

Allows sub second precision.

Parameters:

timestamp: float

A number representing a POSIX/UNIX timestamp.

Returns:

Time

New POSIX timestamp.

classmethod fromstring(string, format='%Y-%m-%d %H:%M:%S')

Equivalent to time.strptime().

Does not allow sub second precision.

Parameters:

string: str

The string to parse.

format: The format of the string.

Returns:

Time

New POSIX timestamp.

classmethod fromydoy(year, doy)

Generate a Time instance from two numbers representing a year and a day in that year.

Allows sub second precision.

Parameters:

year: int

The year to convert.

doy: float

The day od year to convert. Can be a float to allow for hour, minute, etc. measurement.

Returns:

Time

New POSIX timestamp.

Raises:

ValueError

If 0 <= doy < (365 or 366)

classmethod fromdatetime(dt)

Generate a Time instance from a datetime object.

Allows sub second precision.

Parameters:

dt: datetime

The datetime object to convert.

Returns:

Time

New POSIX timestamp.

day_of_year()

The day of year including hours, minutes, and seconds.

Allows sub second precision.

Returns:

float

Day of the year.

week_of_year()

The week of year including days, hours, minutes, and seconds.

Allows sub second precision.

Returns:

float

Week of the year.

timetuple()

Struct representation as produced by time.gmtime().

Does not allow sub second precision.

Returns:

time_struct

Time representation used by some other time related functions.