Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CBA_fnc_weekDay #1533

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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