gnome.utilities.time_utils

time_utils

assorted utilities for working with time and datetime

Classes

TZOffset

FixedOffset

Fixed offset "timezone" in minutes east from UTC.

UTC

A simple tzinfo class for UTC (i.e. no offset, ever...)

Functions

timezone_offset_seconds()

Calculates the minimum acceptable date, considering timezones east of GMT.

zero_time()

date_to_sec(date_times)

sec_to_date(seconds)

sec_to_datetime(seconds)

sec_to_timestruct(seconds)

FIXME: left over code from previous attempt

round_time([dt, roundTo])

Round a datetime object or numpy array to any time laps in seconds

asdatetime(dt)

makes sure the input is a datetime.datetime object

Module Contents

class gnome.utilities.time_utils.TZOffset
offset: float = None
title: str = ''
as_timedelta()

Returns the offset as a timedelta

as_iso_string()

returns the offset as an isostring:

-8:00

3:30

etc …

class gnome.utilities.time_utils.FixedOffset(offset, name)

Bases: datetime.tzinfo

Fixed offset “timezone” in minutes east from UTC.

utcoffset(dt)

datetime -> timedelta, positive for east of UTC, negative for west of UTC

tzname(dt)

datetime -> string name of time zone.

dst(dt)

datetime -> DST offset as timedelta, positive for east of UTC.

Return 0 if DST not in effect. utcoffset() must include the DST offset.

class gnome.utilities.time_utils.UTC

Bases: FixedOffset

A simple tzinfo class for UTC (i.e. no offset, ever…)

gnome.utilities.time_utils.timezone_offset_seconds()

Calculates the minimum acceptable date, considering timezones east of GMT.

returns the offset value in seconds of the beginning of the unix epoch.

This is mostly for testing purposes.

gnome.utilities.time_utils.zero_time()
gnome.utilities.time_utils.date_to_sec(date_times)
Parameters:

date_time – Either a python datetime object or a numpy array of dtype=datetime or dtype=numpy.datetime64

Returns:

either an array containing the time in seconds or just the date in seconds if only 1 input

For each date, it makes timetuple and forces tm_isdst=0, then calls time.mktime to convert to seconds Consistent with Gnome, it does not account for daylight savings time.

The epoch is as defined in python: Jan 1, 1970

gnome.utilities.time_utils.sec_to_date(seconds)
Parameters:

seconds – Either time in seconds or a numpy array containing time in seconds (integer – ideally uint32)

Takes the time and converts it back to datetime object.

This does NOT use: time_utils.sec_to_timestruct(…), but rather, converts directly then “fixes” DST to be compatible with GNOME

Note: time_utils.sec_to_timestruct(…)

to test that it works in the same way as the lib_gnome C++ cython wrapper

FIXME: this may be broken there!!!!!

gnome.utilities.time_utils.sec_to_datetime(seconds)
gnome.utilities.time_utils.sec_to_timestruct(seconds)
FIXME: left over code from previous attempt

mirrors Cython/C++ code, but breaks for the spring DST transition

Only used for testing the Cython code.

Parameters:

seconds – time in seconds

This doesn’t operate on a numpy array. This was separated as a way to explicitly check that we get the same results as the C++ gnome code. It is unlikely to be called from pyGnome

Takes the time and converts it back using localtime() If tm_dst = 1 (by default), then subtract 1 hour and set this flag to 0 Returns a time.struct_time

gnome.utilities.time_utils.round_time(dt=None, roundTo=60)

Round a datetime object or numpy array to any time laps in seconds

Parameters:
  • dt – datetime.datetime object or numpy array of datetime objects, default now.

  • roundTo – Closest number of seconds to round to, default 1 minute.

Returns:

either an array with rounded values or just a single value if only 1 value was input

Author: Thierry Husson 2012 - Use it as you want but don’t blame me.

found on : http://stackoverflow.com

gnome.utilities.time_utils.asdatetime(dt)

makes sure the input is a datetime.datetime object

if it already is, it will be passed through.

If not it will attempt to parse a string to make a datetime object.

None will also be passed through silently