77# The full license is in the LICENSE file, distributed with this software.
88# ----------------------------------------------------------------------------
99"""Utility functions for Digital RF and Digital Metadata."""
10+
1011from __future__ import absolute_import , division , print_function
1112
1213import ast
1718import dateutil .parser
1819import numpy as np
1920import pytz
20-
2121import 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