Skip to content

Commit 502b5ec

Browse files
committed
util: Add epoch argument to time_to_sample
1 parent 128b339 commit 502b5ec

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

python/digital_rf/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# The full license is in the LICENSE file, distributed with this software.
88
# ----------------------------------------------------------------------------
99
"""Utility functions for Digital RF and Digital Metadata."""
10+
1011
from __future__ import absolute_import, division, print_function
1112

1213
import ast
@@ -17,7 +18,6 @@
1718
import dateutil.parser
1819
import numpy as np
1920
import pytz
20-
2121
import six
2222

2323
__all__ = (
@@ -169,7 +169,7 @@ def sample_to_time_floor(nsamples, sample_rate):
169169
return (seconds, picoseconds)
170170

171171

172-
def time_to_sample(time, samples_per_second):
172+
def time_to_sample(time, samples_per_second, epoch=None):
173173
"""Get a sample index from a time using a given sample rate.
174174
175175
Parameters
@@ -182,6 +182,10 @@ def time_to_sample(time, samples_per_second):
182182
samples_per_second : np.longdouble
183183
Sample rate in Hz.
184184
185+
epoch : datetime, optional
186+
Epoch time. If None, the Digital RF default (the Unix epoch,
187+
January 1, 1970) is used.
188+
185189
186190
Returns
187191
-------
@@ -199,6 +203,8 @@ def time_to_sample(time, samples_per_second):
199203
if time.tzinfo is None:
200204
# assume UTC if timezone was not specified
201205
time = pytz.utc.localize(time)
206+
if epoch is None:
207+
epoch = _default_epoch
202208
td = time - epoch
203209
tsec = int(td.total_seconds())
204210
tfrac = 1e-6 * td.microseconds

0 commit comments

Comments
 (0)