Skip to content

Commit

Permalink
util: Add epoch argument to time_to_sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanvolz committed Jun 14, 2024
1 parent 128b339 commit 502b5ec
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions python/digital_rf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# The full license is in the LICENSE file, distributed with this software.
# ----------------------------------------------------------------------------
"""Utility functions for Digital RF and Digital Metadata."""

from __future__ import absolute_import, division, print_function

import ast
Expand All @@ -17,7 +18,6 @@
import dateutil.parser
import numpy as np
import pytz

import six

__all__ = (
Expand Down Expand Up @@ -169,7 +169,7 @@ def sample_to_time_floor(nsamples, sample_rate):
return (seconds, picoseconds)


def time_to_sample(time, samples_per_second):
def time_to_sample(time, samples_per_second, epoch=None):
"""Get a sample index from a time using a given sample rate.
Parameters
Expand All @@ -182,6 +182,10 @@ def time_to_sample(time, samples_per_second):
samples_per_second : np.longdouble
Sample rate in Hz.
epoch : datetime, optional
Epoch time. If None, the Digital RF default (the Unix epoch,
January 1, 1970) is used.
Returns
-------
Expand All @@ -199,6 +203,8 @@ def time_to_sample(time, samples_per_second):
if time.tzinfo is None:
# assume UTC if timezone was not specified
time = pytz.utc.localize(time)
if epoch is None:
epoch = _default_epoch
td = time - epoch
tsec = int(td.total_seconds())
tfrac = 1e-6 * td.microseconds
Expand Down

0 comments on commit 502b5ec

Please sign in to comment.