Skip to content

Commit

Permalink
Merge pull request #1533 from CBATeam/get-week-day
Browse files Browse the repository at this point in the history
Add CBA_fnc_weekDay
  • Loading branch information
PabstMirror committed Mar 6, 2022
2 parents c4ae71a + 4a45fb3 commit 1d9557d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class CfgFunctions {
PATHTO_FNC(getNearestBuilding);
};

class DateTime {
PATHTO_FNC(weekDay);
};

class Misc {
PATHTO_FNC(addPerFrameHandler);
PATHTO_FNC(removePerFrameHandler);
Expand Down
36 changes: 36 additions & 0 deletions addons/common/fnc_weekDay.sqf
Original file line number Diff line number Diff line change
@@ -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]. <ARRAY>
Returns:
Day of the week (0: Sunday, 6: Saturday, -1: invalid) <NUMBER>
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
2 changes: 1 addition & 1 deletion addons/common/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
33 changes: 33 additions & 0 deletions addons/common/test_common.sqf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1d9557d

Please sign in to comment.