简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM