Skip to content

Commit 7c99511

Browse files
committed
time conversion docstring updates
1 parent 92dcc4e commit 7c99511

2 files changed

Lines changed: 82 additions & 67 deletions

File tree

gnss_lib_py/utils/time_conversions.py

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,30 @@ def get_leap_seconds(gps_time):
4242
Parameters
4343
----------
4444
t_secs : float or datetime.datetime
45-
Time of clock can be float [ms] or datetime.datetime object
45+
Time of clock can be float [ms] or datetime.datetime object.
4646
4747
Returns
4848
-------
4949
out_leapsecs : float
50-
Leap seconds at given time [s]
50+
Leap seconds at given time [s].
5151
5252
"""
5353
if isinstance(gps_time, datetime):
5454
curr_time = gps_time
5555
else:
5656
curr_time = GPS_EPOCH_0 + timedelta(milliseconds=gps_time)
5757
curr_time = curr_time.replace(tzinfo=timezone.utc)
58-
#TODO: Abstract checking for tzinfo into a single private function
5958
curr_time = _check_tzinfo(curr_time)
6059
if curr_time < GPS_EPOCH_0:
61-
raise RuntimeError("Need input time after GPS epoch "+ str(GPS_EPOCH_0))
60+
raise RuntimeError("Need input time after GPS epoch " \
61+
+ str(GPS_EPOCH_0))
6262
for row_num, time_frame in enumerate(LEAPSECONDS_TABLE):
6363
if curr_time >= time_frame:
6464
out_leapsecs = len(LEAPSECONDS_TABLE)-1 - row_num
6565
break
6666
return out_leapsecs
6767

68-
def gps_millis_to_tow(millis, add_leap_secs=True, verbose=False):
69-
#TODO: Should we be adding leap seconds by default? Shouldn't they
70-
# be in the same time frame of reference
68+
def gps_millis_to_tow(millis, add_leap_secs=False, verbose=False):
7169
"""Convert milliseconds since GPS epoch to GPS week number and time.
7270
7371
The initial GPS epoch is defined by the variable GPS_EPOCH_0 at
@@ -85,7 +83,7 @@ def gps_millis_to_tow(millis, add_leap_secs=True, verbose=False):
8583
Returns
8684
-------
8785
gps_week : float
88-
GPS week
86+
GPS week.
8987
tow : float
9088
GPS time of week [s].
9189
@@ -115,14 +113,15 @@ def datetime_to_tow(t_datetime, add_leap_secs=True, verbose=False):
115113
Returns
116114
-------
117115
gps_week : float
118-
GPS week
116+
GPS week.
119117
tow : float
120-
GPS time of week [s]
118+
GPS time of week [s].
121119
122120
"""
123121
t_datetime = _check_tzinfo(t_datetime)
124122
if t_datetime < GPS_EPOCH_0:
125-
raise RuntimeError("Input time must be after GPS epoch "+ str(GPS_EPOCH_0))
123+
raise RuntimeError("Input time must be after GPS epoch " \
124+
+ str(GPS_EPOCH_0))
126125
if add_leap_secs:
127126
out_leapsecs = get_leap_seconds(t_datetime)
128127
t_datetime = t_datetime + timedelta(seconds=out_leapsecs)
@@ -136,21 +135,22 @@ def datetime_to_tow(t_datetime, add_leap_secs=True, verbose=False):
136135

137136

138137
def tow_to_datetime(gps_week, tow, rem_leap_secs=True):
139-
"""Convert GPS week and time of week (seconds) to datetime
138+
"""Convert GPS week and time of week (seconds) to datetime.
140139
141140
Parameters
142141
----------
143142
gps_week : float
144-
GPS week
143+
GPS week.
145144
tow : float
146-
GPS time of week [s]
145+
GPS time of week [s].
147146
rem_leap_secs : bool
148-
Flag on whether to remove leap seconds from given tow
147+
Flag on whether to remove leap seconds from given tow.
149148
150149
Returns
151150
-------
152151
t_datetime: datetime.datetime
153-
Datetime in UTC timezone, with or without leap seconds based on flag
152+
Datetime in UTC timezone, with or without leap seconds based on
153+
flag.
154154
"""
155155
seconds_since_epoch = WEEKSEC * gps_week + tow
156156
t_datetime = GPS_EPOCH_0 + timedelta(seconds=seconds_since_epoch)
@@ -160,22 +160,25 @@ def tow_to_datetime(gps_week, tow, rem_leap_secs=True):
160160
return t_datetime
161161

162162
def tow_to_unix_millis(gps_week, tow):
163-
"""Convert GPS week and time of week (seconds) to milliseconds since UNIX epoch
163+
"""Convert GPS week and time of week (seconds) to UNIX milliseconds.
164164
165+
Convert GPS week and time of week (seconds) to milliseconds since
166+
UNIX epoch.
165167
Leap seconds will always be removed from tow because of offset between
166-
UTC and GPS clocks
168+
UTC and GPS clocks.
167169
168170
Parameters
169171
----------
170172
gps_week : float
171-
GPS week
173+
GPS week.
172174
tow : float
173-
GPS time of week [s]
175+
GPS time of week [s].
174176
175177
Returns
176178
-------
177179
unix_millis: float
178-
Milliseconds since UNIX epoch (midnight 1/1/1970 UTC)
180+
Milliseconds since UNIX epoch (midnight 1/1/1970 UTC).
181+
179182
"""
180183
#NOTE: Don't need to remove leapseconds here because they're
181184
# removed in tow_to_datetime
@@ -186,127 +189,135 @@ def tow_to_unix_millis(gps_week, tow):
186189

187190

188191
def tow_to_gps_millis(gps_week, tow):
189-
"""Convert GPS week and time of week (seconds) to milliseconds since GPS epoch
192+
"""Convert GPS week and time of week (seconds) to GPS milliseconds.
190193
194+
Convert GPS week and time of week (seconds) to milliseconds since
195+
GPS epoch.
191196
No leap seconds adjustments are made because both times are in the
192197
same frame of reference.
193198
194199
Parameters
195200
----------
196201
gps_week : float
197-
GPS week
202+
GPS week.
198203
tow : float
199-
GPS time of week [s]
204+
GPS time of week [s].
200205
201206
Returns
202207
-------
203208
gps_millis: float
204209
Milliseconds since GPS epoch
205-
(midnight 6th January, 1980 UTC with leap seconds)
210+
(midnight 6th January, 1980 UTC with leap seconds).
211+
206212
"""
207213
millis_since_epoch = 1000*(WEEKSEC * gps_week + tow)
208214
return millis_since_epoch
209215

210216

211217
def datetime_to_unix_millis(t_datetime):
212-
"""Convert datetime to milliseconds since UNIX Epoch (1/1/1970 UTC)
218+
"""Convert datetime to milliseconds since UNIX Epoch (1/1/1970 UTC).
213219
214-
If no timezone is specified, assumes UTC as timezone
220+
If no timezone is specified, assumes UTC as timezone.
215221
216222
Parameters
217223
----------
218224
t_datetime : datetime.datetime
219-
UTC time as a datetime object
225+
UTC time as a datetime object.
220226
221227
Returns
222228
-------
223229
unix_millis : float
224-
Milliseconds since UNIX Epoch (1/1/1970 UTC)
230+
Milliseconds since UNIX Epoch (1/1/1970 UTC).
231+
225232
"""
226233
t_datetime = _check_tzinfo(t_datetime)
227234
unix_millis = 1000*(t_datetime - UNIX_EPOCH_0).total_seconds()
228235
return unix_millis
229236

230237
def datetime_to_gps_millis(t_datetime, add_leap_secs=True):
231-
"""Convert datetime to milliseconds since GPS Epoch (6th January 1980)
238+
"""Convert datetime to milliseconds since GPS Epoch.
232239
240+
GPS Epoch starts at the 6th January 1980.
233241
If no timezone is specified, assumes UTC as timezone and returns
234242
milliseconds in GPS time frame of reference by adding leap seconds.
235-
Milliseconds are not added when the flag add_leap_secs is set to False.
243+
Milliseconds are not added when the flag add_leap_secs is False.
236244
237245
Parameters
238246
----------
239247
t_datetime : datetime.datetime
240-
UTC time as a datetime object
248+
UTC time as a datetime object.
241249
add_leap_secs : bool
242250
Flag for whether output is in UTC seconds or GPS seconds.
243251
244252
Returns
245253
-------
246254
gps_millis : float
247-
Milliseconds since GPS Epoch (6th January 1980 GPS)
255+
Milliseconds since GPS Epoch (6th January 1980 GPS).
256+
248257
"""
249258
gps_week, tow = datetime_to_tow(t_datetime, add_leap_secs=add_leap_secs)
250259
gps_millis = tow_to_gps_millis(gps_week, tow)
251260
return gps_millis
252261

253262

254263
def unix_millis_to_datetime(unix_millis):
255-
"""Convert milliseconds since UNIX epoch (1/1/1970) to UTC datetime
264+
"""Convert milliseconds since UNIX epoch (1/1/1970) to UTC datetime.
256265
257266
Parameters
258267
----------
259268
unix_millis : float
260-
Milliseconds that have passed since UTC epoch
269+
Milliseconds that have passed since UTC epoch.
261270
262271
Returns
263272
-------
264273
t_utc : datetime.datetime
265-
UTC time as a datetime object
274+
UTC time as a datetime object.
266275
"""
267276
t_utc = UNIX_EPOCH_0 + timedelta(milliseconds=unix_millis)
268277
t_utc = t_utc.replace(tzinfo=timezone.utc)
269278
return t_utc
270279

271280

272281
def unix_millis_to_tow(unix_millis):
273-
"""Convert milliseconds since UNIX epoch (1/1/1970) to datetime
282+
"""Convert UNIX milliseconds to GPS week and time of week (seconds).
274283
275-
Always adds leap seconds to convert from UTC to GPS time reference
284+
UNIX milliseconds are since UNIX epoch (1/1/1970).
285+
Always adds leap seconds to convert from UTC to GPS time reference.
276286
277287
Parameters
278288
----------
279289
unix_millis : float
280-
Milliseconds that have passed since UTC epoch
290+
Milliseconds that have passed since UTC epoch.
281291
282292
Returns
283293
-------
284294
gps_week : float
285-
GPS week
295+
GPS week.
286296
tow : float
287-
GPS time of week [s]
297+
GPS time of week [s].
288298
"""
289299
t_utc = unix_millis_to_datetime(unix_millis)
290300
gps_week, tow = datetime_to_tow(t_utc, add_leap_secs=True)
291301
return gps_week, tow
292302

293303

294304
def unix_to_gps_millis(unix_millis, add_leap_secs=True):
295-
"""Convert milliseconds since UNIX epoch (1/1/1970) to millis since GPS epoch
305+
"""Convert milliseconds since UNIX epoch (1/1/1970) to GPS millis.
296306
297307
Adds leap seconds by default but time can be kept in UTC frame by
298-
setting to False
308+
setting to False.
309+
299310
Parameters
300311
----------
301312
unix_millis : float
302-
Milliseconds that have passed since UTC epoch
313+
Milliseconds that have passed since UTC epoch.
303314
add_leap_secs : bool
304315
Flag for whether output is in UTC seconds or GPS seconds.
305316
306317
Returns
307318
-------
308319
gps_millis : float
309-
Milliseconds since GPS Epoch (6th January 1980 GPS)
320+
Milliseconds since GPS Epoch (6th January 1980 GPS).
310321
"""
311322
# Add leapseconds should always be true here
312323
t_utc = unix_millis_to_datetime(unix_millis)
@@ -315,8 +326,9 @@ def unix_to_gps_millis(unix_millis, add_leap_secs=True):
315326

316327

317328
def gps_millis_to_datetime(gps_millis, rem_leap_secs=True):
318-
"""Convert milliseconds since the GPS epoch (in GPS reference) to UTC
329+
"""Convert milliseconds since GPS epoch to datetime.
319330
331+
GPS millis is from the start of the GPS in GPS reference.
320332
The initial GPS epoch is defined by the variable GPS_EPOCH_0 at
321333
which the week number is assumed to be 0.
322334
@@ -332,15 +344,15 @@ def gps_millis_to_datetime(gps_millis, rem_leap_secs=True):
332344
t_utc : datetime.datetime
333345
UTC time as a datetime object
334346
"""
335-
# TODO: Should we remove leapseconds here
336347
gps_week, tow = gps_millis_to_tow(gps_millis, add_leap_secs=False)
337348
t_utc = tow_to_datetime(gps_week, tow, rem_leap_secs=rem_leap_secs)
338349
return t_utc
339350

340351

341352
def gps_to_unix_millis(gps_millis, rem_leap_secs=True):
342-
"""Convert milliseconds since the GPS epoch (in GPS reference) to UTC
353+
"""Convert milliseconds since GPS epoch to UNIX millis.
343354
355+
GPS millis is from the start of the GPS in GPS reference.
344356
The initial GPS epoch is defined by the variable GPS_EPOCH_0 at
345357
which the week number is assumed to be 0.
346358
@@ -365,23 +377,25 @@ def gps_to_unix_millis(gps_millis, rem_leap_secs=True):
365377

366378

367379
def _check_tzinfo(t_datetime):
368-
"""Raises warning if given time doesn't have timezone and converts to UTC
380+
"""Raises warning if time doesn't have timezone and converts to UTC.
369381
370382
Parameters
371383
----------
372384
t_datetime : datetime.datetime
373-
Datetime object with no timezone, UTC timezone or local timezone
385+
Datetime object with no timezone, UTC timezone, or local
386+
timezone.
374387
375388
Returns
376389
-------
377390
t_datetime: datetime.datetime
378391
Datetime object in UTC, added if no timezone was given and
379-
converted to UTC if datetime was in local timezone
392+
converted to UTC if datetime was in local timezone.
393+
380394
"""
381395
if not hasattr(t_datetime, 'tzinfo') or not t_datetime.tzinfo:
382396
warnings.warn("No time zone info found in datetime, assuming UTC",\
383397
RuntimeWarning)
384398
t_datetime = t_datetime.replace(tzinfo=timezone.utc)
385399
if t_datetime.tzinfo != timezone.utc:
386400
t_datetime = t_datetime.astimezone(timezone.utc)
387-
return t_datetime
401+
return t_datetime

0 commit comments

Comments
 (0)