19 """Utilties for calculating and modifying GPS times using the LAL date
41 import datetime
as _datetime
42 from decimal
import Decimal
43 from numbers
import Number
45 from dateutil.parser
import parse
as str_to_utc
49 GPSTimeNow
as _gps_time_now,
50 GPSToUTC
as _gps_to_utc,
51 UTCToGPS
as _utc_to_gps,
53 from .
import git_version
55 __author__ =
"Duncan Macleod <duncan.macleod@ligo.org>"
56 __version__ = git_version.verbose_msg
57 __date__ = git_version.date
72 GPS_EPOCH = _datetime.datetime(1980, 1, 6, 0, 0, 0)
73 LAL_GPS_MAX = _datetime.datetime(2048, 1, 24, 3, 13, 55)
76 def _check_utc(utc_time):
77 """Check whether this UTC datetime will convert to a valid LIGOTimeGPS
79 if utc_time < GPS_EPOCH:
80 raise ValueError(
"Given UTC time is before the start of the GPS era.")
81 if utc_time > LAL_GPS_MAX:
82 raise ValueError(
"Given UTC time is too far in the future, "
83 "LAL will SegmentationFault.")
88 """Get the current time in GPS seconds
90 @returns a LIGOTimeGPS
92 return _gps_time_now()
96 """Convert the given `datetime.datetime` into a GPS time
98 @returns a LIGOTimeGPS
100 if not isinstance(utc_time, _datetime.datetime):
101 utc_time = _datetime.datetime.combine(utc_time, _datetime.time())
103 return LIGOTimeGPS(_utc_to_gps(utc_time.utctimetuple()))
107 """Convert a GPS time into a `datetime.datetime`
109 @returns a Python `datetime.datetime` object in UTC
111 if isinstance(gps, (Decimal, Number)):
113 gps = LIGOTimeGPS(gps)
114 dt = _datetime.datetime(
115 *_gps_to_utc(gps.gpsSeconds)[:7]
117 if gps.gpsNanoSeconds:
119 microsecond=int(round(gps.gpsNanoSeconds * 1e-3)),
125 """Get the current date and time in UTC
127 @returns a Python `datetime.datetime` object in UTC
133 r"""Converts a date/time string into a GPS time.
135 The following special words are permitted:
143 >>> gpstime.str_to_gps("September 14 2011, 01:46:25")
147 @returns a LIGOTimeGPS
149 if not time_string
or time_string.lower() ==
"now":
150 return _gps_time_now()
151 elif time_string ==
"today":
152 date = _datetime.date.today()
153 return utc_to_gps(_datetime.datetime.combine(date, _datetime.time()))
154 elif time_string ==
"tomorrow":
155 today = _datetime.datetime.combine(_datetime.date.today(),
157 tomorrow = today + _datetime.timedelta(days=1)
159 elif time_string ==
"yesterday":
160 today = _datetime.datetime.combine(_datetime.date.today(),
162 yesterday = today - _datetime.timedelta(days=1)
165 utc = str_to_utc(time_string, tzinfos=TIME_ZONES)
166 micro = utc.microsecond
168 return gps + micro / 1000000.0
173 Convert a LIGOTimeGPS time object into a string.
174 The output format can be given explicitly, but will default
175 as shown in the example.
180 >>> gps_to_str(1000000000)
181 'September 14 2011, 01:46:25 UTC'
184 @returns a string with the given format.
186 gps = LIGOTimeGPS(gps)
188 if gps.gpsNanoSeconds
and not form:
189 form =
"%B %d %Y, %H:%M:%S.%f UTC"
191 form =
"%B %d %Y, %H:%M:%S UTC"
192 return utc.strftime(form)
196 r"""Convert date/time strings to and from GPS times.
197 If no argument is given, the current GPS time is returned.
199 The following special words are permitted:
210 >>> tconvert("January 6 1980 00:00:00")
212 >>> tconvert(1000000000)
213 'September 14 2011, 01:46:25 UTC'
216 @returns the LIGOTimeGPS of the given time string, OR, string
217 representing the given GPS time
230 if __name__ ==
"__main__":
233 print(
"The date/time now is %s (%d)" % (now_utc, now))
static int replace(int c, void *param)
def str_to_gps(time_string=None)
Converts a date/time string into a GPS time.
def tconvert(arg=None, form=None)
Convert date/time strings to and from GPS times.
def gps_time_now()
Get the current time in GPS seconds.
def gps_to_str(gps, form=None)
onvert a LIGOTimeGPS time object into a string.
def utc_time_now()
Get the current date and time in UTC.
def utc_to_gps(utc_time)
Convert the given datetime.datetime into a GPS time.
def gps_to_utc(gps)
Convert a GPS time into a datetime.datetime