stick-the-quick/util/TimeExt.gd

59 lines
1.8 KiB
GDScript3
Raw Normal View History

class_name TimeExt extends StrictSingleton
## Extension methods for Time class.
## Returns the current number of seconds since the Epoch.
static func get_current_unix_time() -> int:
return Time.get_unix_time_from_datetime_dict(
Time.get_datetime_dict_from_system()
)
## Returns a string representation of the given number of system time seconds.
##
## Format is [minus] [N seconds|N minutes N seconds|N hours N minutes|N days].
## See function body comments for specific details.
static func format_systime_interval(seconds: int) -> String:
var neg: bool = seconds < 0
seconds = abs(seconds)
# If 0 minutes, report only seconds.
if seconds < 60:
return "%s%d seconds" % [
'minus ' if neg else '',
seconds
]
# If 0 hours, report only minutes and seconds.
elif seconds < 3600:
return "%s%d minutes %d seconds" % [
'minus ' if neg else '',
int(seconds/60.0), seconds%60
]
# If 0 days, report only hours and minutes, and regard seconds
# as probably too small in comparison to be of any interest.
elif seconds < 86400:
return "%s%d hours %02d minutes" % [
'minus ' if neg else '',
int(seconds/3600.0),
int(seconds/60.0)%60
]
# If >0 days, report only days, and regard everything else
# as probably too small in comparison to be of any interest.
else:
return "%s%d days" % [
'minus ' if neg else '',
int(seconds/86400.0)
]
## Returns a string representation of the given number of realtime seconds.
##
## Format is [-]HH:MM:SS:TT (where T stands for pars minuta [t]ertia,
## more commonly known as the typical duration of one frame).
static func format_realtime_interval(seconds: float) -> String:
var neg: bool = seconds < 0.0
seconds = abs(seconds)
return "%s%d:%02d:%02d:%02d" % [
'-' if neg else '',
int(seconds/3600.0),
int(seconds/60.0)%60,
int(seconds)%60,
int(seconds*60.0)%60
]