From ad83ef8d9d865902d04da3313e2a1cbe0254804e Mon Sep 17 00:00:00 2001 From: Brian Moe <lars@moe.phys.uwm.edu> Date: Thu, 19 Nov 2009 15:13:52 -0600 Subject: [PATCH] moved time utils out of template tags lib --- gracedb/models.py | 2 +- gracedb/templatetags/timeutil.py | 53 +------------------------------- utils/__init__.py | 53 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 utils/__init__.py diff --git a/gracedb/models.py b/gracedb/models.py index 7a326d2a8..d1fb6a928 100644 --- a/gracedb/models.py +++ b/gracedb/models.py @@ -6,7 +6,7 @@ import os from gracedb.ligolw.models import CoincEvent -from gracedb.gracedb.templatetags.timeutil import posixToGpsTime +from gracedb.utils import posixToGpsTime from django.conf import settings import pytz, time diff --git a/gracedb/templatetags/timeutil.py b/gracedb/templatetags/timeutil.py index 0d7ed89c2..faa1a8ce2 100644 --- a/gracedb/templatetags/timeutil.py +++ b/gracedb/templatetags/timeutil.py @@ -6,6 +6,7 @@ from django.utils import dateformat from django.utils.html import conditional_escape from django.utils.safestring import mark_safe +from gracedb.utils import posixToGpsTime, gpsToUtc import pytz import time @@ -114,55 +115,3 @@ def utc(dt, format=FORMAT): def gpsdate(gpstime, format=FORMAT): return dateformat.format(gpsToUtc(gpstime), format) - -# TAG that generates stuff that changes time display what-nots. - - -# GPS time conversion - -# This is kind of awful in that leapSeconds -# are hard coded and needs to be kept up to date. -# Also, this should be somewhere else. -# Also, there are almost certainly failing edge cases at leap second adjustment times. - -import calendar, datetime - -gpsEpoch = calendar.timegm((1980, 1, 6, 0, 0, 0, 0, 0, 0)) - -leapSeconds = map(calendar.timegm, [ - (1981, 7, 0, 0, 0, 0, 0, 0, 0), - (1982, 7, 0, 0, 0, 0, 0, 0, 0), - (1983, 7, 0, 0, 0, 0, 0, 0, 0), - (1985, 7, 0, 0, 0, 0, 0, 0, 0), - (1988, 1, 0, 0, 0, 0, 0, 0, 0), - (1990, 1, 0, 0, 0, 0, 0, 0, 0), - (1991, 1, 0, 0, 0, 0, 0, 0, 0), - (1992, 7, 0, 0, 0, 0, 0, 0, 0), - (1993, 7, 0, 0, 0, 0, 0, 0, 0), - (1994, 7, 0, 0, 0, 0, 0, 0, 0), - (1996, 1, 0, 0, 0, 0, 0, 0, 0), - (1997, 7, 0, 0, 0, 0, 0, 0, 0), - (1999, 1, 0, 0, 0, 0, 0, 0, 0), - (2006, 1, 0, 0, 0, 0, 0, 0, 0), - (2009, 1, 0, 0, 0, 0, 0, 0, 0), -]) - -def gpsToPosixTime(gpsTime): - t = gpsEpoch + gpsTime - for leap in leapSeconds: - if t >= leap: - t = t - 1 - return t - -def posixToGpsTime(posixTime): - change = 0 - for leap in leapSeconds: - if posixTime > leap: - change += 1 - return posixTime + change - gpsEpoch - -def gpsToUtc(gpsTime): - t = gpsToPosixTime(gpsTime) - return datetime.datetime.fromtimestamp(t, pytz.utc) - - diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 000000000..df4443682 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,53 @@ + +# GPS time conversion + +# This is kind of awful in that leapSeconds +# are hard coded and needs to be kept up to date. +# Also, this probably is/will be/should be in glue. +# Also, there are almost certainly failing edge cases at leap second adjustment times. +# Oh yea, and I don't think this works exactly right for some periods 2006-2008, say. + +import pytz +import datetime + +import calendar + +gpsEpoch = calendar.timegm((1980, 1, 6, 0, 0, 0, 0, 0, 0)) + +leapSeconds = map(calendar.timegm, [ + (1981, 7, 0, 0, 0, 0, 0, 0, 0), + (1982, 7, 0, 0, 0, 0, 0, 0, 0), + (1983, 7, 0, 0, 0, 0, 0, 0, 0), + (1985, 7, 0, 0, 0, 0, 0, 0, 0), + (1988, 1, 0, 0, 0, 0, 0, 0, 0), + (1990, 1, 0, 0, 0, 0, 0, 0, 0), + (1991, 1, 0, 0, 0, 0, 0, 0, 0), + (1992, 7, 0, 0, 0, 0, 0, 0, 0), + (1993, 7, 0, 0, 0, 0, 0, 0, 0), + (1994, 7, 0, 0, 0, 0, 0, 0, 0), + (1996, 1, 0, 0, 0, 0, 0, 0, 0), + (1997, 7, 0, 0, 0, 0, 0, 0, 0), + (1999, 1, 0, 0, 0, 0, 0, 0, 0), + (2006, 1, 0, 0, 0, 0, 0, 0, 0), + (2009, 1, 0, 0, 0, 0, 0, 0, 0), +]) + +def gpsToPosixTime(gpsTime): + t = gpsEpoch + gpsTime + for leap in leapSeconds: + if t >= leap: + t = t - 1 + return t + +def posixToGpsTime(posixTime): + change = 0 + for leap in leapSeconds: + if posixTime > leap: + change += 1 + return posixTime + change - gpsEpoch + +def gpsToUtc(gpsTime): + t = gpsToPosixTime(gpsTime) + return datetime.datetime.fromtimestamp(t, pytz.utc) + + -- GitLab