Using Godot 3.6 on Windows:
How to calculate the ISO week number of a date given the following parameters: day
, month
, year
?
I could not find a way to calculate the week number from the built-in class Time
I've also looked into the Calendar Button Plugin for Godot Engine 3.2.3 repo by Ivan Skodje but could not find any answer or inspiration.
One way I managed to achieve this is by using PowerShell
by using the following self-defined methods:
# Executes the [ script ] in Windows PowerShell
static func execute(script, open_console: bool = false, freeze_loop: bool = true) -> Array:
if OS.get_name() != "Windows":
return []
var output: Array = []
var arguments: PoolStringArray = PoolStringArray([])
match typeof(script):
TYPE_STRING:
arguments.append(script as String)
TYPE_ARRAY, TYPE_STRING_ARRAY:
for line in script:
arguments.append("\n%s" % line)
_:
return []
OS.execute("powershell.exe", arguments, freeze_loop, output, false, open_console)
return output
Inspired by the following answer: Get the ISO 8601 Week of Year of a given date in Powershell
# Returns the ISO week number of the specified date
static func get_iso_week(day: int, month: int, year: int) -> int:
var script: PoolStringArray = PoolStringArray([
"$date = [DateTime] '%d-%d-%d'" % [year, month, day],
("(Get-Culture).Calendar.GetWeekOfYear(($date).AddDays(-[Int]"
+ "(($date).AddDays(-1)).DayOfWeek+3), 2, 1)")
])
var result: Array = execute(script)
if result.empty():
return 0
return result[0].to_int()
Using the above methods ^ the result is accurate and as expected, but the execution time is quite slow especially when calling this function 6 times (once for each week row in a standard calendar), and I don't know how to program a method in PowerShell
that can return a list of all week numbers by the specified month
and year
(this was, only one PowerShell
execution will be performed instead of 6, but I still find this suboptimal)
ChatGPT also failed to find an answer suitable for Godot 3.6 no matter how many times I need to correct it. (Always suggesting classes and methods that do not exist)