Skip to content

Commit

Permalink
Add time module to ocean framework
Browse files Browse the repository at this point in the history
This computes a time interval from days and/or seconds, including
a decimal fraction.
  • Loading branch information
xylar committed Feb 16, 2024
1 parent 1e1ac50 commit 6a32832
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions compass/ocean/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time

import numpy as np


def get_time_interval_string(days=None, seconds=None):
"""
Convert a time interval in days and/or seconds to a string for use in a
model config option. If both are provided, they will be added
Parameters
----------
days : float, optional
A time interval in days
seconds : float, optional
A time interval in seconds
Returns
-------
time_str : str
The time as a string in the format "DDDD_HH:MM:SS.SS"
"""
sec_per_day = 86400
total = 0.
if seconds is not None:
total += seconds
if days is not None:
total += sec_per_day * days

day_part = int(total / sec_per_day)
sec_part = total - day_part * sec_per_day
sec_decimal = sec_part - np.floor(sec_part)
# https://stackoverflow.com/a/1384565/7728169
seconds_str = time.strftime('%H:%M:%S', time.gmtime(sec_part))
time_str = f'{day_part:04d}_{seconds_str}.{int(sec_decimal * 1e3):03d}'
return time_str

0 comments on commit 6a32832

Please sign in to comment.