43 lines
963 B
GDScript
43 lines
963 B
GDScript
# datetime.gd
|
|
class_name DateTime
|
|
extends Resource
|
|
|
|
@export var _unix_time: float = 0.0
|
|
|
|
|
|
static func now() -> DateTime:
|
|
var dt = DateTime.new()
|
|
dt._unix_time = Time.get_unix_time_from_system()
|
|
return dt
|
|
|
|
|
|
static func from_unix(unix_seconds: float) -> DateTime:
|
|
var dt = DateTime.new()
|
|
dt._unix_time = unix_seconds
|
|
return dt
|
|
|
|
|
|
static func from_string(input: String) -> DateTime:
|
|
var s := input.strip_edges()
|
|
if s.ends_with("Z"):
|
|
s = s.substr(0, s.length() - 1)
|
|
var unix := Time.get_unix_time_from_datetime_string(s)
|
|
if s.find("-") == -1 or s.find(":") == -1:
|
|
push_error("Invalid datetime string format: %s" % input)
|
|
return null
|
|
var dt = DateTime.new()
|
|
dt._unix_time = unix
|
|
return dt
|
|
|
|
|
|
func to_unix() -> float:
|
|
return _unix_time
|
|
|
|
|
|
func _to_string() -> String:
|
|
var dict := Time.get_datetime_dict_from_unix_time(_unix_time)
|
|
return "%04d-%02d-%02dT%02d:%02d:%02dZ" % [
|
|
dict.year, dict.month, dict.day,
|
|
dict.hour, dict.minute, dict.second
|
|
]
|