Sign in to follow this  
Followers 0
rudi

Check a Date, if it's a "Working Day"

4 posts in this topic

#1 ·  Posted (edited)

Hi.

For a certain task I need to know, if "today" is a working day (Mo-Fr, and not a Holiday either).

Maybe someone else comes across a similar task:

; Autoit v3.3.8.1
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_LegalCopyright=Rudolf Thilo, IT-Beratung Rudolf Thilo
#AutoIt3Wrapper_Res_Language=1031
://////=__=.=
://////=__==forum: "rudi")
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****


#include-once
#include <Date.au3>
#include <array.au3>


; Example Call for the function:
$Result = CheckWorkingDay("2012/04/08") ; that was Easter Sunday this year
If $Result == True Then
    ConsoleWrite("Working Day" & @LF)
Else
    ConsoleWrite($Result & @LF)
EndIf




Func CheckWorkingDay($Date = False)
    ; Function to check, if a Date is a workingday (TRUE), or "Saturday", "Sunday", "Holiday"
    ; Add / Remove holidays by altering the Array $aHDay
    ; German National Holidays (Bavaria, predominantly katholic cities) are implemented, also the Easter dependant holidays are included. (Gauss' Easter Formula)
    ;
    ;Optional Parameter $Date: If no date value "yyyy/mm/dd" is specified, "today" will be processed
    ;                        A validity check for $Date is *NOT* done!
    ;
    ;            Return value: "True", if "working day" is detected; otherwise: "Saturday", "Sunday" or "Holiday"
    ;                                if a Saturday or Sunday is a Holiday as well, "Holiday" is returned.
    ;        important note: As the return value is *NEVER* "False", you have to compare it with "==" instead of "=" !!
    ;                        Example: if CheckHoliday() == True then ...
    ;                        the statement "if CheckHoliday() then..." will *ALWAYS* be True, so it's useless as well.


    If $Date = False Then $Date = _NowCalcDate() ; Funktion within a Function Definition is not allowed: So $Date=False in definition, fill in "today" here...
    Local $Year = StringLeft($Date, 4) ; get the year
    Local $a, $b, $c, $d, $e, $m, $N, $k, $q
    If $Year < 1582 Then
        $Calendar = "julian" ; the retirement of the "Julian Calendar" began 1582.
    Else
        $Calendar = "gregorian" ; The use of the "Gegorian Calendar" started 1582. In 1949 China was the last state to introduce the Gregorian Calendar.
    EndIf
    $a = Mod($Year, 19)
    $b = Mod($Year, 4)
    $c = Mod($Year, 7)
    $k = Int($Year / 100)
    $p = Int((8 * $k + 13) / 25)
    $q = Int($k / 4)
    Switch $Calendar
        Case "gregorian"
            $m = Mod(15 + $k - $p - $q, 30)
            $N = Mod(4 + $k - $q, 7)
        Case "julian"
            $m = 15
            $N = 6
    EndSwitch
    $d = Mod(19 * $a + $m, 30)
    $e = Mod(2 * $b + 4 * $c + 6 * $d + $N, 7)
    $Shift = 22 + $d + $e ; could have used "21" here, saving the "-1" next line. Then it wouldn't match the Gauss Formula found elswhere in the web.
    $Easter = _DateAdd("D", $Shift - 1, $Year & "/03/01") ; "-1", because Gauss' Easter Rule returns the "Day-of-March", so we have to start at "-1"
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Easter = ' & $Easter & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

    Local $aHDay[1]
    _ArrayAdd($aHDay, $Year & "/01/01") ; Neujahr
    _ArrayAdd($aHDay, $Year & "/01/06") ; Hl. 3 König
    #region Easter related holidays, Germany
    _ArrayAdd($aHDay, _DateAdd("D", -2, $Easter)) ; Karfreitag
    _ArrayAdd($aHDay, $Easter) ; Ostersonntag
    _ArrayAdd($aHDay, _DateAdd("D", 1, $Easter)) ; Ostermontag
    _ArrayAdd($aHDay, _DateAdd("D", 39, $Easter)) ; Christi Himmelfahrt
    _ArrayAdd($aHDay, _DateAdd("D", 49, $Easter)) ; Pfingstsonntag
    _ArrayAdd($aHDay, _DateAdd("D", 50, $Easter)) ; Pfingstmontag
    _ArrayAdd($aHDay, _DateAdd("D", 60, $Easter)) ; Fronleichnam
    #endregion Easter related holidays, Germany
    _ArrayAdd($aHDay, $Year & "/08/15") ; Mariä Himmelfahrt
    _ArrayAdd($aHDay, $Year & "/10/03") ; Tag der deutschen Einheit
    _ArrayAdd($aHDay, $Year & "/11/01") ; Allerheiligen
    _ArrayAdd($aHDay, $Year & "/12/25") ; 1. Weihnachtsfeiertag
    _ArrayAdd($aHDay, $Year & "/12/26") ; 2. Weihnachtsfeiertag
    ; add other national / regional holidays here: Reformationstag, Friedensfest, Buß & Bettag...



    Local $WorkingDay = True ; Retun value, in case the lines below don't detect Sat, Sun, Holiday.

    Local $Mon = StringMid($Date, 6, 2)
    Local $Day = StringRight($Date, 2)
    Switch _DateToDayOfWeekISO($Year, $Mon, $Day) ; 1 = Monday, 7 = Sunday
        Case 6
            $WorkingDay = "Saturday"
        Case 7
            $WorkingDay = "Sunday"
    EndSwitch

    For $i = 1 To UBound($aHDay) - 1
        If $Date = $aHDay[$i] Then
            $WorkingDay = "Holiday"
            ExitLoop
        EndIf
    Next

    Return $WorkingDay

    #cs
        Osterabhängige Tage
        
        Rosenmontag: -48 Tage
        Faschingsdienstag: -47 Tage
        Karfreitag: -2 Tage
        Ostermontag: +1 Tag
        Christi Himmelfahrt: +39 Tage
        Pfingstsonntag: +49 Tage
        Pfingstmontag: +50 Tage
        Fronleichnam: +60 Tage
    #ce

EndFunc   ;==>CheckWorkingDay

Regards, Rudi.

Edited by rudi
1 person likes this

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you are complicating

You can achieve it simply by this

#RequireAdmin
#include <Date.au3>

Func GetWorkingDay($nDay, $nMon, $nYear)
Local $aRet = False
;Get the Real Dates
Local $pDay = @MDAY, $pMon = @MON, $pYear = @YEAR
;Set the Date to be Checked
_SetDate($nDay, $nMon, $nYear) ; Set the current date to 20-10-2004
If @error Then Return SetError(@error,@extended,-1)
;Check Working Day
If @WDAY == 1 Or @WDAY == 7 Then $aRet = True
;Set the Real Date
_SetDate($pDay, $pMon, $pYear)
If @error Then Return SetError(@error,@extended,-1)
;Return
Return $aRet
EndFunc ;==>GetWorkingDay


MsgBox ( 64, @ScriptName & '| Phoenix XL', _
GetWorkingDay(07, 04, 2012) _
)
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

PhoenixXL,

Your version doesn't calculate German holidays as rudi's does. But the point for posting was your version can be consolidated to the following >> no need for _SetDate.

#include <Date.au3>

MsgBox(4096, '', 'Is today (' & @MDAY & '/' & @MON & '/' & @YEAR & ') a weekday/workingday? ' & _IsWeekday(@YEAR, @MON, @MDAY))
MsgBox(4096, '', 'Is today (10/11/2012) a weekday/workingday? ' & _IsWeekday(2012, 11, 10)) ; Saturday

Func _IsWeekday($iYear, $iMonth, $iDay) ; YYYY/MM/DD Format.
    Local $iReturn = _DateToDayOfWeek($iYear, $iMonth, $iDay)
    Return SetError(@error, $iReturn, $iReturn > 1 And $iReturn < 7)
EndFunc   ;==>_IsWeekday

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

Indeed :oops:


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0