Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ rustdoc-args = ["--cfg", "docrs", "--generate-link-to-definition"]
serde = { version = "1.0.155", optional = true }
serde_derive = { version = "1.0.155", optional = true }
pyo3 = { version = "0.25.0", features = [
"extension-module",
"multiple-pymethods",
], optional = true }
num-traits = { version = "0.2.15", default-features = false, features = [
Expand All @@ -46,7 +45,7 @@ ureq = { version = "3.0.10", default-features = false, optional = true, features
[features]
default = ["std"]
std = ["serde", "serde_derive", "web-time", "snafu/std", "snafu/backtrace"]
python = ["std", "pyo3", "ut1"]
python = ["std", "pyo3/extension-module", "ut1"]
ut1 = ["std", "ureq", "tabled", "openssl"]

[dev-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions hifitime.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ starting on August 21st 1999 Midnight UT,
def from_mjd_utc(days: float) -> Epoch:
"""Initialize an Epoch from given MJD in UTC time scale"""

@staticmethod
def from_ptp_duration(duration: Duration) -> Epoch:
"""Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) duration since TAI midnight 1970 January 01.
PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems."""

@staticmethod
def from_ptp_nanoseconds(nanoseconds: int) -> Epoch:
"""Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) nanoseconds timestamp since TAI midnight 1970 January 01.
PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems."""

@staticmethod
def from_ptp_seconds(seconds: float) -> Epoch:
"""Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) second timestamp since TAI midnight 1970 January 01.
PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems."""

@staticmethod
def from_qzsst_days(days: float) -> Epoch:
"""Initialize an Epoch from the number of days since the QZSS Time Epoch,
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[tool.maturin]
profile = "release"
features = ["python"]
features = ["python", "pyo3/extension-module"]

[project]
name = "hifitime"
Expand All @@ -15,6 +15,7 @@ classifiers = [
"Topic :: Scientific/Engineering :: Physics",
"Intended Audience :: Science/Research",
]
license = "MPL-2.0"

[tool.yapf]
based_on_style = "google"
Expand Down
32 changes: 30 additions & 2 deletions src/epoch/gregorian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl Epoch {

#[must_use]
/// Converts the Epoch to the Gregorian UTC equivalent as (year, month, day, hour, minute, second).
/// WARNING: Nanoseconds are lost in this conversion!
///
/// # Example
/// ```
Expand Down Expand Up @@ -196,7 +195,6 @@ impl Epoch {

#[must_use]
/// Converts the Epoch to the Gregorian TAI equivalent as (year, month, day, hour, minute, second).
/// WARNING: Nanoseconds are lost in this conversion!
///
/// # Example
/// ```
Expand All @@ -215,6 +213,36 @@ impl Epoch {
Self::compute_gregorian(self.to_duration_in_time_scale(ts), ts)
}

#[must_use]
/// Converts the Epoch to the Gregorian in the provided time scale as (year, month, day, hour, minute, second).
///
/// # Example
/// ```
/// use hifitime::{Epoch, TimeScale};
/// let dt = Epoch::from_gregorian_tai_at_midnight(1972, 1, 1);
/// let (y, m, d, h, min, s, n) = dt.to_gregorian(TimeScale::TAI);
/// assert_eq!(y, 1972);
/// assert_eq!(m, 1);
/// assert_eq!(d, 1);
/// assert_eq!(h, 0);
/// assert_eq!(min, 0);
/// assert_eq!(s, 0);
/// assert_eq!(n, 0);
///
/// // The epoch will be converted to UTC prior to returning the Gregorian parts.
/// let (y, m, d, h, min, s, n) = dt.to_gregorian(TimeScale::UTC);
/// assert_eq!(y, 1971);
/// assert_eq!(m, 12);
/// assert_eq!(d, 31);
/// assert_eq!(h, 23);
/// assert_eq!(min, 59);
/// assert_eq!(s, 50);
/// assert_eq!(n, 0);
/// ```
pub fn to_gregorian(&self, time_scale: TimeScale) -> (i32, u8, u8, u8, u8, u8, u32) {
Self::compute_gregorian(self.to_duration_in_time_scale(time_scale), time_scale)
}

/// Attempts to build an Epoch from the provided Gregorian date and time in TAI.
pub fn maybe_from_gregorian_tai(
year: i32,
Expand Down
3 changes: 2 additions & 1 deletion src/epoch/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ impl Epoch {
}

#[must_use]
/// Initialize an Epoch from the provided duration since UTC midnight 1970 January 01.
/// Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) duration since TAI midnight 1970 January 01.
/// PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems.
pub fn from_ptp_duration(duration: Duration) -> Self {
Self::from_duration(UNIX_REF_EPOCH.to_utc_duration() + duration, TimeScale::TAI)
}
Expand Down
33 changes: 33 additions & 0 deletions src/epoch/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,39 @@ impl Epoch {
Self::maybe_from_gregorian_utc(year, month, day, hour, minute, second, nanos)
}

/// Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) duration since TAI midnight 1970 January 01.
/// PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems.
///
/// :type duration: Duration
/// :rtype: Epoch
#[classmethod]
#[pyo3(name = "from_ptp_duration")]
fn py_from_ptp_duration(_cls: &Bound<'_, PyType>, duration: Duration) -> Self {
Self::from_ptp_duration(duration)
}

/// Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) second timestamp since TAI midnight 1970 January 01.
/// PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems.
///
/// :type seconds: float
/// :rtype: Epoch
#[classmethod]
#[pyo3(name = "from_ptp_seconds")]
fn py_from_ptp_seconds(_cls: &Bound<'_, PyType>, seconds: f64) -> Self {
Self::from_ptp_seconds(seconds)
}

/// Initialize an Epoch from the provided IEEE 1588-2008 (PTPv2) nanoseconds timestamp since TAI midnight 1970 January 01.
/// PTP uses the TAI timescale but with the Unix Epoch for compatibility with unix systems.
///
/// :type nanoseconds: int
/// :rtype: Epoch
#[classmethod]
#[pyo3(name = "from_ptp_nanoseconds")]
fn py_from_ptp_nanoseconds(_cls: &Bound<'_, PyType>, nanoseconds: u64) -> Self {
Self::from_ptp_nanoseconds(nanoseconds)
}

#[classmethod]
/// WARNING: Deprecated since 4.1.1; Use `from_gregorian_utc` instead
/// Builds an Epoch from the provided Gregorian date and time in TAI. If invalid date is provided, this function will panic.
Expand Down