Jose Manuel Albornoz
Jose Manuel Albornoz

Reputation: 128

Can we create custom timestamps in Python for a day with a duration that is not 24 hours?

I am creating time series models using astronomical data from celestial bodies with a period of rotation that is not 24 hours. For example, I have data from a planet with a 9-hour day; in such planet the timestamp should reset to 00:00:00 (HH:MM:SS) every time the clock reaches 08:59:59

Is there a way to create custom timestamps in Python to deal with such a situation?

Upvotes: 2

Views: 113

Answers (1)

Tzane
Tzane

Reputation: 3462

For converting from timestamp in seconds:

def to_planet_time(timestamp):
    # timestamp in seconds
    # configure day cycle
    minute = 60
    hour = 60 * minute
    day = 9 * hour

    # days
    ts_days = int(timestamp / day)
    timestamp -= ts_days * day

    # hours
    ts_hours = int(timestamp / hour)
    timestamp -= ts_hours * hour

    # minutes
    ts_minutes = int(timestamp / minute)
    timestamp -= ts_minutes * minute

    return "{:02d}:{:02d}:{:02d}".format(ts_hours, ts_minutes, int(timestamp))

s = to_planet_timestamp(60.15)
print(s)

s = to_planet_timestamp(9*60*60)
print(s)

s = to_planet_timestamp(9*60*60 - 1)
print(s)

Out:

00:01:00
00:00:00
08:59:59

Upvotes: 1

Related Questions