Monday, 1 January 2024

Conversation from nano sec to micro sec

from datetime import datetime


# Given data (replace these values with your actual data)

sourcetime = 1731614518958014585  # Source time in nanoseconds

fh_sourcetime = 1731614518        # FH time in Linux seconds (Unix time in seconds since epoch)

fh_sourcetimeNS = 989645647       # Remaining nanoseconds for FH time

pps_sourcetime = 1731614518       # PPS time in Linux seconds (Unix time in seconds since epoch)

pps_sourcetimeNS = 964054168      # Remaining nanoseconds for PPS time


# Get the current time in milliseconds since epoch

current_time_ms = int(datetime.now().timestamp() * 1000)


# 1. Convert `sourcetime` from nanoseconds to milliseconds

# - `sourcetime` is in nanoseconds, so divide by 1,000,000 to convert to milliseconds.

source_time_ms = sourcetime / 1_000_000


# Calculate delta from `sourcetime` to `current_time_ms`

delta_source_to_current = current_time_ms - source_time_ms


# 2. Convert `fh_sourcetime` and `fh_sourcetimeNS` to milliseconds

# - `fh_sourcetime` is in seconds, so multiply by 1,000 to get milliseconds.

# - `fh_sourcetimeNS` is in nanoseconds, so divide by 1,000,000 to convert to milliseconds.

# - Add both results to get the full `fh_time_ms` in milliseconds.

fh_time_ms = (fh_sourcetime * 1_000) + (fh_sourcetimeNS / 1_000_000)


# Calculate delta from `source_time_ms` to `fh_time_ms`

delta_source_to_fh = fh_time_ms - source_time_ms


# 3. Convert `pps_sourcetime` and `pps_sourcetimeNS` to milliseconds

# - `pps_sourcetime` is in seconds, so multiply by 1,000 to get milliseconds.

# - `pps_sourcetimeNS` is in nanoseconds, so divide by 1,000,000 to convert to milliseconds.

# - Add both results to get the full `pps_time_ms` in milliseconds.

pps_time_ms = (pps_sourcetime * 1_000) + (pps_sourcetimeNS / 1_000_000)


# Calculate delta from `fh_time_ms` to `pps_time_ms`

delta_fh_to_pps = pps_time_ms - fh_time_ms


# 4. Calculate delta from `pps_time_ms` to `current_time_ms`

delta_pps_to_current = current_time_ms - pps_time_ms


# Display the results with explanations

print("Delta (Source Time to Current Time):", delta_source_to_current, "ms")

print("Delta (Source Time to FH Time):", delta_source_to_fh, "ms")

print("Delta (FH Time to PPS Time):", delta_fh_to_pps, "ms")

print("Delta (PPS Time to Current Time):", delta_pps_to_current, "ms")


No comments:

Post a Comment