Created
November 3, 2021 14:05
-
-
Save Yagich/d78fbe640ec5d4c335b07d0d32d533d6 to your computer and use it in GitHub Desktop.
godot date/time helper class in GDScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name DateHelper | |
static func date_add_days(date: Dictionary, days_to_add: int = 1) -> Dictionary: | |
assert(date.has("month"), "the date Dictionary must have a month") | |
assert(date.has("day"), "the date Dictionary must have a month") | |
assert(date.has("year"), "the date Dictionary must have a year") | |
var to_unix = OS.get_unix_time_from_datetime(date) + (86400 * days_to_add) | |
return OS.get_datetime_from_unix_time(to_unix) | |
static func datetime_add_seconds(datetime: Dictionary, seconds_to_add: int = 1) -> Dictionary: | |
assert(datetime.has("second"), "the date Dictionary must have a second") | |
assert(datetime.has("minute"), "the date Dictionary must have a minute") | |
assert(datetime.has("hour"), "the date Dictionary must have an hour") | |
var to_unix = OS.get_unix_time_from_datetime(datetime) + seconds_to_add | |
return OS.get_datetime_from_unix_time(to_unix) | |
static func is_leap_year(year: int) -> bool: | |
return true if year % 4 == 0 else false | |
static func get_days_in_month(month: int) -> int: | |
var d = OS.get_date(true) | |
var leap = is_leap_year(d.year) | |
match month: | |
1, 3, 5, 7, 8, 10, 12: | |
return 31 | |
2: | |
return 29 if leap else 28 | |
_: | |
return 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment