Skip to content
Snippets Groups Projects
Commit e1a4000c authored by Bruce Flynn's avatar Bruce Flynn
Browse files

trunc_datetime

parent 8b25b7e9
No related branches found
No related tags found
No related merge requests found
from .time import to_unix_timestamp, hhmm_to_offset
from .time import to_unix_timestamp, hhmm_to_offset, trunc_datetime
from .units import *
from .calc import *
......@@ -68,8 +68,6 @@ https://cvs.ssec.wisc.edu/cgi-bin/cvsweb.cgi/java/tower/SolarPosition.java
"""
__docformat__ = 'restructuredtext en'
__version__ = '$Revision: 1.6 $'
# $Source: /cvsroot/TOOLS/dev/metobs/python/data/metobs/data/solar_position.py,v $
from metobs.data import NaN, util
import math
......
from datetime import timedelta
from datetime import timedelta, datetime
from calendar import timegm
def trunc_datetime(dt, interval):
"""Truncate a datetime to the nearest time on interval with basetime
of epoch.
>>> v = trunc_datetime(datetime(2014, 5, 20, 0, 0, 4), timedelta(seconds=5)
>>> assert v == datetime(2014, 5, 20, 0, 0, 0)
>>> v = trunc_datetime(datetime(2014, 5, 20, 0, 0, 1), 5)
>>> assert v == datetime(2014, 5, 20, 0, 0, 0)
"""
if not isinstance(interval, timedelta):
interval = timedelta(seconds=interval)
utime = to_unix_timestamp(dt)
return datetime.utcfromtimestamp(utime - utime % interval.total_seconds())
def to_unix_timestamp(dtval):
"""Convert a datetime to a unix timestamp.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment