Time Formatting Parsing in GDScript
GDScript supports time formatting and parsing, although it uses a different approach compared to pattern-based layouts. Here’s how you can work with time in GDScript:
extends Node
func _ready():
var print = funcref(self, "print_line")
# Get the current time
var current_time = OS.get_datetime()
print.call(format_datetime(current_time))
# Parse a time string
var parsed_time = parse_datetime("2012-11-01 22:08:41")
print.call(parsed_time)
# Format time in different ways
print.call(format_time(current_time, "%I:%M %p"))
print.call(format_datetime(current_time))
print.call(format_datetime(current_time, true)) # With microseconds
# Custom time formatting
var custom_format = "%I %M %p"
var custom_time = parse_time("8:41 PM", custom_format)
print.call(custom_time)
# Numeric representation
print.call("%04d-%02d-%02dT%02d:%02d:%02d-00:00" % [
current_time["year"], current_time["month"], current_time["day"],
current_time["hour"], current_time["minute"], current_time["second"]
])
# Error handling for parsing
var result = parse_time("8:41PM", "%H:%M:%S")
if result == null:
print.call("Error parsing time")
func format_datetime(datetime, with_microseconds = false):
if with_microseconds:
return "%04d-%02d-%02dT%02d:%02d:%02d.%06d" % [
datetime["year"], datetime["month"], datetime["day"],
datetime["hour"], datetime["minute"], datetime["second"],
datetime["microsecond"]
]
else:
return "%04d-%02d-%02dT%02d:%02d:%02d" % [
datetime["year"], datetime["month"], datetime["day"],
datetime["hour"], datetime["minute"], datetime["second"]
]
func format_time(datetime, format):
return OS.get_time_string_from_system(format, datetime)
func parse_datetime(datetime_string):
return OS.get_datetime_from_unix_time(OS.get_unix_time_from_datetime_string(datetime_string))
func parse_time(time_string, format):
return OS.get_time_dict_from_system(format, time_string)
In GDScript, we use the OS
class to work with date and time. Here’s a breakdown of the code:
We use
OS.get_datetime()
to get the current time.For parsing time strings, we use
OS.get_unix_time_from_datetime_string()
and then convert it back to a datetime dictionary withOS.get_datetime_from_unix_time()
.Time formatting is done using
OS.get_time_string_from_system()
, which accepts a format string similar to the Cstrftime()
function.For custom time parsing, we use
OS.get_time_dict_from_system()
.We can also manually format time using string formatting with the components of the time dictionary.
Error handling for parsing is done by checking if the result is null.
Note that GDScript doesn’t have built-in constants for standard time formats like RFC3339. You would need to define these formats yourself if needed.
When running this script, you would see output similar to:
2023-06-10T15:30:45
2012-11-01T22:08:41
3:30 PM
2023-06-10T15:30:45
2023-06-10T15:30:45.123456
20:41:00
2023-06-10T15:30:45-00:00
Error parsing time
Remember that the exact output will depend on the current time when you run the script.