Jump to content
Sign in to follow this  
ozdeadmeat

Time Duration Script

Recommended Posts

Hi guys,

I am looking for some help with regards a Time Duration Function that returns either days, weeks, months, seconds etc from 2 arrays given in the date format

So for example:

Feb 21th 2016 --> March 6th is 14 days.

I remember seeing a function similar to this years ago but for the life of me I cant find it.

Any help would be greatly appreciated. If I happen to get my crappy attempt working or find the script I will post it here.

Share this post


Link to post
Share on other sites

Thanks galzohar, already aware of those commands.

Not really what I am looking for however.

For example: If I have a date Lets say [2016,2,21,6,45] as a start date (note, has nothing to do with the current mission time, date) and I have another date, [2028,3,6,18,45] and I want to calculate the amount of days between these two dates. Both those commands will not help with this calculation.

Here is what I have so far in the function I am writing.

CountTime = {
//Usage [[date1],[date2],"RETURNTYPE"] call CountTime
_originalDate = _this select 0;
_CurrentDate = _this select 1;
_return = _this select 2; //"Seconds", "Minutes", "Days", "Weeks", "Months", "Years"

_LeapMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31];
_monthDays = [31,28,31,30,31,30,31,31,30,31,30,31];

_Oyear = _originalDate select 0;
_Omonth = _originalDate select 1;
_Oday = _originalDate select 2;
_Ohour = _originalDate select 3;
_Ominute = _originalDate select 4;

_Cyear = _CurrentDate select 0;
_Cmonth = _CurrentDate select 1;
_Cday = _CurrentDate select 2;
_Chour = _CurrentDate select 3;
_Cminute = _CurrentDate select 4;

//This is all the years between _Oyear and _Cyear
_Difference = _Cyear - _Oyear;

_yearArray = [];
_LyearArray = [];
for "_i" from (0 to _Difference) do {
_newYear = _Oyear + _i;
_yearArray set [_i,_newYear];
_IsLeap = (_newYear / 4) - (floor(_newYear / 4));
if(_IsLeap == 0) then {_LyearArray set [_i,1]} Else {_LyearArray set [_i,0]};
};

Share this post


Link to post
Share on other sites

BIS_fnc_monthDays will return the correct days in the month depending on arguments used ... Return two and then just a few lines to find the difference in days.

Share this post


Link to post
Share on other sites

IMPORTANT NOTE:

Shit, I didn't see OzDeaDMeaT already posting a solution ... I now feel like having stolen his idea. My apologies! :butbut:

As I couldn't find a BIS function doing this, I figured it might be suitable to write it myself:

[color=#FF8040][color=#191970][b]scriptName[/b][/color] [color=#7A7A7A]"fn_dateDifference"[/color][color=#8B3E2F][b];[/b][/color]
[color=#006400][i]/*
   File:            fn_dateDifference.sqf
   Author:            Heeeere's Johnny!, adapted from an idea by OzDeaDMeaT    <<< Please do not edit or remove this line. Thanks. >>>
   Description:    Calculates the difference in days between two given dates, taking in account leap years.
   Must spawn:        No

   Parameters:
       0 ARRAY        dateStart    start date in format [year, month, day]
       1 ARRAY        dateEnd        end date in format [year, month, day]

   Return:
       INTEGER        daysTotal    difference in days between the given dates
*/[/i][/color]

[color=#1874CD]_dateStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#000000]_this[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]    [color=#006400][i]//[2016,2,21,6,45];[/i][/color]
[color=#1874CD]_dateEnd[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#000000]_this[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b];[/b][/color]    [color=#006400][i]//[2028,3,6,18,45];[/i][/color]

[color=#1874CD]_yearStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateStart[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_monthStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateStart[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_dayStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateStart[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]2[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_hourStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateStart[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]3[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_minStart[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateStart[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]4[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_yearEnd[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateEnd[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_monthEnd[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateEnd[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_dayEnd[/color]        [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateEnd[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]2[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_hourEnd[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateEnd[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]3[/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_minEnd[/color]        [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_dateEnd[/color] [color=#191970][b]select[/b][/color] [color=#FF0000]4[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_daysTotal[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]

[color=#191970][b]for[/b][/color] [color=#7A7A7A]"_i"[/color] [color=#191970][b]from[/b][/color] [color=#1874CD]_yearStart[/color] [color=#191970][b]to[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#1874CD]_yearEnd[/color] [color=#8B3E2F][b]-[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]do[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#006400][i]/*
   Definition of a leap year:
   - if year is dividable by 4, it is a leap year
   - if year is dividable by 100, it is NOT a leap year
   - if year is dividable by 400, it is a leap year
   - else, it is NOT a leap year
   */[/i][/color]
   [color=#1874CD]_isLeapYear[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_i[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]4[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#1874CD]_i[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]100[/color] [color=#8B3E2F][b]![/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]|[/b][/color][color=#8B3E2F][b]|[/b][/color] [color=#1874CD]_i[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]400[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]true[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]false[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
   [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_isLeapYear[/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color]
   [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]366[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color]
   [color=#191970][b]else[/b][/color]
   [color=#8B3E2F][b]{[/b][/color]
       [color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]365[/color][color=#8B3E2F][b];[/b][/color]
   [color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_isLeapYear[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_yearStart[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]4[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#1874CD]_yearStart[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]100[/color] [color=#8B3E2F][b]![/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]|[/b][/color][color=#8B3E2F][b]|[/b][/color] [color=#1874CD]_yearStart[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]400[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]true[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]false[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_daysSum[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_daysOfMonths[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_isLeapYear[/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#FF0000]29[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#FF0000]28[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_monthStart[/color] [color=#191970][b]isEqualTo[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#000000]_forEachIndex[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b])[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]exitWith[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
   [color=#1874CD]_daysSum[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_daysSum[/color] [color=#8B3E2F][b]-[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color][color=#8B3E2F][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#1874CD]_daysOfMonths[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_isLeapYear[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_yearEnd[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]4[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]&[/b][/color][color=#8B3E2F][b]&[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#1874CD]_yearEnd[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]100[/color] [color=#8B3E2F][b]![/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color] [color=#8B3E2F][b]|[/b][/color][color=#8B3E2F][b]|[/b][/color] [color=#1874CD]_yearEnd[/color] [color=#191970][b]mod[/b][/color] [color=#FF0000]400[/color] [color=#8B3E2F][b]=[/b][/color][color=#8B3E2F][b]=[/b][/color] [color=#FF0000]0[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]true[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#000000]false[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#1874CD]_daysOfMonths[/color]    [color=#8B3E2F][b]=[/b][/color] [color=#8B3E2F][b][[/b][/color][color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_isLeapYear[/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]then[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#FF0000]29[/color][color=#8B3E2F][b]}[/b][/color] [color=#191970][b]else[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#FF0000]28[/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]30[/color][color=#8B3E2F][b],[/b][/color] [color=#FF0000]31[/color][color=#8B3E2F][b]][/b][/color][color=#8B3E2F][b];[/b][/color]
[color=#8B3E2F][b]{[/b][/color]
   [color=#191970][b]if[/b][/color][color=#8B3E2F][b]([/b][/color][color=#1874CD]_monthEnd[/color] [color=#191970][b]isEqualTo[/b][/color] [color=#8B3E2F][b]([/b][/color][color=#000000]_forEachIndex[/color] [color=#8B3E2F][b]+[/b][/color] [color=#FF0000]1[/color][color=#8B3E2F][b])[/b][/color][color=#8B3E2F][b])[/b][/color] [color=#191970][b]exitWith[/b][/color] [color=#8B3E2F][b]{[/b][/color][color=#8B3E2F][b]}[/b][/color][color=#8B3E2F][b];[/b][/color]
   [color=#1874CD]_daysSum[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_daysSum[/color] [color=#8B3E2F][b]+[/b][/color] [color=#000000]_x[/color][color=#8B3E2F][b];[/b][/color][color=#8B3E2F][/color]
[color=#8B3E2F][b]}[/b][/color] [color=#191970][b]forEach[/b][/color] [color=#1874CD]_daysOfMonths[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]=[/b][/color] [color=#1874CD]_daysTotal[/color] [color=#8B3E2F][b]-[/b][/color] [color=#1874CD]_dayStart[/color] [color=#8B3E2F][b]+[/b][/color] [color=#1874CD]_dayEnd[/color] [color=#8B3E2F][b]+[/b][/color] [color=#1874CD]_daysSum[/color][color=#8B3E2F][b];[/b][/color]

[color=#1874CD]_daysTotal[/color]
[/color]

Kudos to Killzone_Kid for his SQF to BBCode Converter.

Edited by Heeeere's Johnny!
apology

Share this post


Link to post
Share on other sites

That is ok. This appears much more eligant than my shitty bit of code that I was posting more as an example than as a working function. I am gonna try this out as soon as i get home. You sir are awesomeballs!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×