diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index 794c76817..118b7ed60 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -114,6 +114,10 @@ class CfgFunctions { PATHTO_FNC(getNearestBuilding); }; + class DateTime { + PATHTO_FNC(weekDay); + }; + class Misc { PATHTO_FNC(addPerFrameHandler); PATHTO_FNC(removePerFrameHandler); diff --git a/addons/common/fnc_weekDay.sqf b/addons/common/fnc_weekDay.sqf new file mode 100644 index 000000000..5f7552838 --- /dev/null +++ b/addons/common/fnc_weekDay.sqf @@ -0,0 +1,36 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_weekDay + +Description: + Calculates date's day of the week using a modified Rata Die method with fractional years. + +Parameters: + _date - Date of [year, month, day]. + +Returns: + Day of the week (0: Sunday, 6: Saturday, -1: invalid) + +Examples: + (begin example) + [systemTimeUTC] call CBA_fnc_weekDay; + [date] call CBA_fnc_weekDay; + [[2022, 2, 16]] call CBA_fnc_weekDay; + (end) + +Author: + Jonpas +---------------------------------------------------------------------------- */ + +params [["_date", [0, 0, 0], [[]], [3, 4, 5, 6, 7]]]; + +// Keep only year, month, day if longer date format is given +// Hours and minutes must be given as 0 for dateToNumber to work correctly for this use-case +_date = [_date select 0, _date select 1, _date select 2, 0, 0]; + +private _yearBefore = ((_date select 0) - 1) max 0; +private _leapYears = floor (_yearBefore / 4); +private _normalYears = _yearBefore - _leapYears; +private _days = _normalYears + (_leapYears * (366 / 365)) + dateToNumber _date; + +(round (_days / (1 / 365))) mod 7 // return diff --git a/addons/common/test.sqf b/addons/common/test.sqf index 63e8537e3..b3dea79de 100644 --- a/addons/common/test.sqf +++ b/addons/common/test.sqf @@ -5,7 +5,7 @@ #define DEBUG_MODE_FULL #include "script_component.hpp" -#define TESTS ["config", "inventory", "weaponComponents", "position", "ret", "macro_is_x"] +#define TESTS ["common", "config", "inventory", "weaponComponents", "position", "ret", "macro_is_x"] SCRIPT(test-common); diff --git a/addons/common/test_common.sqf b/addons/common/test_common.sqf new file mode 100644 index 000000000..8f5235a74 --- /dev/null +++ b/addons/common/test_common.sqf @@ -0,0 +1,33 @@ +#include "script_component.hpp" +SCRIPT(test_common); + +// execVM "\x\cba\addons\common\test_common.sqf"; + +private ["_funcName", "_result"]; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +_funcName = "CBA_fnc_weekDay"; +LOG("Testing " + _funcName); + +TEST_DEFINED("CBA_fnc_weekDay",""); + +_result = [] call CBA_fnc_weekDay; +TEST_TRUE(_result == -1,_funcName); // invalid + +_result = [[0, 0, 0]] call CBA_fnc_weekDay; +TEST_TRUE(_result == -1,_funcName); // invalid + +_result = [[2022, 1, 1]] call CBA_fnc_weekDay; +TEST_TRUE(_result == 6,_funcName); // Saturday + +_result = [[2022, 2, 16]] call CBA_fnc_weekDay; +TEST_TRUE(_result == 3,_funcName); // Wednesday + +// date format [year, month, day, hour, minute] +_result = [[2022, 2, 17, 11, 56]] call CBA_fnc_weekDay; +TEST_TRUE(_result == 4,_funcName); // Thursday + +// systemTime format [year, month, day, hour, minute, second, millisecond] +_result = [[2022, 2, 18, 11, 56, 24, 126]] call CBA_fnc_weekDay; +TEST_TRUE(_result == 5,_funcName); // Friday