Seite zurück / Previous page
links
Seite vor / Next page
rechts
Kapitel zurück / Previous Chapter
zurück
Kapitel vor / Next Chapter
vor
Seitenanfang / Top of page
oben
Seitenende / End of page
unten
DD380800.HTM
DD-Software.DATETIME
Add this page to your favorites
Save this document

Date and Time Functions


DATETIME.WBT   Version 1.19  2003:10:02

User Defined Functions Library for WinBatch.

Includes calculations for German Holidays.
Enthält Berechnungen für deutsche Feiertage.


If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com


;==========================================================================================================================================
; udflib.DateTime.wbt
;==========================================================================================================================================
; Date and Time Functions v1.19
; User Defined Functions for WinBatch.
;
; Detlev Dalitz.20010325.20020201.20020709.20020719.20020722.20020723.20020725.20020804.20021112.20021202...
; ...20030213.20030604.20030620.20030711.20030819.20030820.20030920.20030924.20030925.20031002.
;==========================================================================================================================================

;==========================================================================================================================================
; Overview
;------------------------------------------------------------------------------------------------------------------------------------------
; udfSayNoYes (bool)                  ; Returns string "No" or "Yes"
;------------------------------------------------------------------------------------------------------------------------------------------
; udfTimeFormatStr (YmdHms,TaggedStr) ; Returns string formatted as defined in TaggedStr
;------------------------------------------------------------------------------------------------------------------------------------------
; udfIsValidDate (YmdHms)             ; Returns boolean, false or true (0..1)
; udfIsValidDateTime (YmdHms)         ; Returns boolean, false or true (0..1)
;------------------------------------------------------------------------------------------------------------------------------------------
; udfIsLeapYear (YmdHms)              ; Returns boolean, false or true (0..1)
; udfIsJulianLeapYear (YmdHms)        ; Returns boolean, false or true (0..1)
;------------------------------------------------------------------------------------------------------------------------------------------
; udfJulianDayOfWeek (YmdHms)         ; Returns number, range=0..6 = Sunday..Saturday
; udfDayOfWeek (YmdHms)               ; Returns number, range=1..7 = Monday..Sunday
; udfDayOfYear (YmdHms)               ; Returns number, range=1..366
; udfWeekOfYear (YmdHms)              ; Returns number, range=0..53
; udfWeekOfMonth (sYmdHms, iMode)     ; Returns number, range=1..6
;------------------------------------------------------------------------------------------------------------------------------------------
; udfNameOfDay (sYmdHms, iLength)     ; Returns string of at least iLength number of characters. Range iLength=0..n
; udfNameOfMonth (sYmdHms, iLength)   ; Returns string of at least iLength number of characters. Range iLength=0..n
;------------------------------------------------------------------------------------------------------------------------------------------
; udfTimeDMY (YmdHms)                 ; Returns string formatted as "dd.mm.yyyy"
; udfTimeDMYShort (YmdHms)            ; Returns string formatted as "ddd dd.mm.yyyy"
; udfTimeDMYLong (YmdHms)             ; Returns string formatted as "dddd dd.mm.yyyy"
;------------------------------------------------------------------------------------------------------------------------------------------
; udfDaysInYear (YmdHms)              ; Returns number, range=365..366
; udfDaysInMonth (YmdHms)             ; Returns number, range=28..31
; udfGetLastDayOfMonth (sYmdHms)      ; Returns datetime string  ; The day item is set to the last day of given month.
; udfGetLastDayOfMonth_2 (sYmdHms)    ; Returns datetime string  ; The day item is set to the last day of given month.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfIsLastDayInMonth (YmdHms)        ; Returns boolean, false or true (0..1)
; udfIsFirstDayOfMonth (YmdHms)       ; Returns boolean, false or true (0..1)
; udfIsLastDayOfMonth (YmdHms)        ; Returns boolean, false or true (0..1)
; udfIsNthDayOfMonth (n, YmdHms)      ; Returns boolean, false or true (0..1)
; udfIsWeekDay (YmdHms)               ; Returns boolean, false or true (0..1)  ; True if day is Monday,Tuesday,Wednesday,Thursday,Friday.
; udfIsWeekendDay (YmdHms)            ; Returns boolean, false or true (0..1)  ; True if day is Saturday,Sunday.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetCountDowInMonth (sYmdHms, iDayOfWeek)              ; Returns number, range=1..5  ; uses iDayOfWeek = 0..6 = Sunday..Saturday
; udfGetNthDowInMonth (sYmdHms, iOccurrence, iDayOfWeek)   ; Returns datetime string     ; uses iDayOfWeek = 0..6 = Sunday..Saturday
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetWeekDayNext (YmdHms)          ; Returns datetime string
; udfGetWeekDayPrev (YmdHms)          ; Returns datetime string
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetSundayOfWeek (YmdHms)         ; Returns datetime string  ; leaves Hms as is
; udfGetMondayOfWeek (YmdHms)         ; Returns datetime string  ; leaves Hms as is
; udfGetTuesdayOfWeek (YmdHms)        ; Returns datetime string  ; leaves Hms as is
; udfGetWednesdayOfWeek (YmdHms)      ; Returns datetime string  ; leaves Hms as is
; udfGetThursdayOfWeek (YmdHms)       ; Returns datetime string  ; leaves Hms as is
; udfGetFridayOfWeek (YmdHms)         ; Returns datetime string  ; leaves Hms as is
; udfGetSaturdayOfWeek (YmdHms)       ; Returns datetime string  ; leaves Hms as is
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetYmd (sYmdHms)                 ; Returns the Ymd date part of the specified sYmdhms.
; udfGetHms (sYmdHms)                 ; Returns the Hms time part of the specified sYmdhms.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfSetYmd (sYmdHms, sYmd)           ; Sets the Ymd date part of the sYmdHms to the specified sYmd date value.
; udfSetHms (sYmdHms, sHms)           ; Sets the Hms time part of the sYmdHms to the specified sHms time value.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetYear (sYmdHms)                ; Returns the number of the year of the specified date.
; udfGetMonth (sYmdHms)               ; Returns the number of the month of the specified date.
; udfGetDay (sYmdHms)                 ; Returns the number of the day of the specified date.
; udfGetHour (sYmdHms)                ; Returns the number of the hour of the specified date.
; udfGetMinute (sYmdHms)              ; Returns the number of the minute of the specified date.
; udfGetSecond (sYmdHms)              ; Returns the number of the second of the specified date.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfSetYear (sYmdHms, iYear)         ; Sets the year to the specified value.
; udfSetMonth (sYmdHms, iMonth)       ; Sets the month to the specified value.
; udfSetDay (sYmdHms, iDay)           ; Sets the day to the specified value.
; udfSetHour (sYmdHms, iHour)         ; Sets the hour to the specified value.
; udfSetMinute (sYmdHms, iMinute)     ; Sets the minute to the specified value.
; udfSetSecond (sYmdHms, iSecond)     ; Sets the second to the specified value.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfSecondsToDhms (iSeconds, iMode)  ; Returns numbers for days, hours, minutes, seconds for a given number of seconds.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfYmdHmsToHTTPStamp (sYmdHms, iMode) ; Returns string formatted as (1) "Sun, 06 Nov 1994 08:49:37 GMT"  ; RFC 822, updated by RFC 1123
;                                       ; Returns string formatted as (2) "Sunday, 06-Nov-94 08:49:37 GMT" ; RFC 850, obsoleted by RFC 1036
;                                       ; Returns string formatted as (3) "Sun Nov  6 08:49:37 1994"       ; ANSI C's asctime() format
;                                       ; Returns string formatted as (4) "1994-11-06T08:49:37"            ; ISO 8601 format
;                                       ; Returns string formatted as (5) "19941106T084937"                ; ISO 8601 compacted format
;------------------------------------------------------------------------------------------------------------------------------------------
; udfYmdHmsToYwd (sYmdHms, iMode)     ; Returns ISO 8601 notation for the week in a formatted string like "yyyyWwwd".
; udfYwdtoYmdHms (sYwd)               ; Returns the YmdHms datetime string available from ISO 8601 week notation string.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfRoundToMinute (sYmdHms, iMode)   ; Returns datetime string.
; udfRoundToHour (sYmdHms, iMode)     ; Returns datetime string.
; udfRoundToDay (sYmdHms, iMode)      ; Returns datetime string.
; udfRoundToMonth (sYmdHms, iMode)    ; Returns datetime string.
; udfRoundToYear (sYmdHms, iMode)     ; Returns datetime string.
; udfRoundToQuarter (sYmdHms, iMode)  ; Returns datetime string.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfAgeDifference (sYmdHms1, sYmdHms2, iMode) ; Returns integer number.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfYmdHmsToUnixSeconds (YmdHms)     ; Returns number UnixTimeStamp 0..999999999..?
; udfUnixSecondsToYmdHms (Seconds)    ; Returns datetime string
;------------------------------------------------------------------------------------------------------------------------------------------
; udfHmsToDecimalHour (Hms)           ; Returns decimal number
; udfDecimalHourToYmdHms (fDecHour)   ; Returns datetime string
;------------------------------------------------------------------------------------------------------------------------------------------
; udfYmdHmsToFloat (sYmdHms)          ; Returns floating point number that represents the DateTime.
; udfFloatToYmdHms (fDateTime)        ; Returns datetime string on a given floating point number that represents the DateTime.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfStrDateTo3Char (sYmdHms)         ; Returns a three-character string suitable for use as the prefix for a file name.
; udfStrDateTo4Char (sYmdHms)         ; Returns a four-character string suitable for use as the prefix for a file name.
; udfStrDateTo8Char (sYmdHms)         ; Returns a eight-character string suitable for use as a dos file name.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfCalendar (YmdHms)                ; Returns a small calendar table.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetSeason (sYmdHms)              ; Returns a string containing the name of the season for the given date.
; udfGetZodiac (sYmdHms)              ; Returns a string containing the name of the zodiac sign for the given date.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfEasterSunday_Gauss (sYmdHms)            ; Returns datetime string.
; udfEasterSunday_Schlyter (sYmdHms)         ; Returns datetime string.
; udfEasterSunday_Lilius_Clavius (sYmdHms)   ; Returns datetime string.
; udfEasterSunday_Passah (sYmdHms)           ; Returns datetime string.
; udfEasterSunday (sYmdHms)                  ; Returns datetime string.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGetTimezoneInfo()                ; Returns a tab delimited list about Daylight saving and Standard time settings.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfGD_AscherMittwoch (YmdHms)       ; Returns datetime string
; udfGD_PalmSonntag (YmdHms)          ; Returns datetime string
; udfGD_KarFreitag (YmdHms)           ; Returns datetime string
; udfGD_OsterSonntag (YmdHms)         ; Returns datetime string
; udfGD_OsterMontag (YmdHms)          ; Returns datetime string
; udfGD_WeisserSonntag (YmdHms)       ; Returns datetime string
; udfGD_ChristiHimmelfahrt (YmdHms)   ; Returns datetime string
; udfGD_PfingstSonntag (YmdHms)       ; Returns datetime string
; udfGD_PfingstMontag (YmdHms)        ; Returns datetime string
; udfGD_Fronleichnam (YmdHms)         ; Returns datetime string
; udfGD_MariaeHimmelfahrt (YmdHms)    ; Returns datetime string
; udfGD_TagDerEinheit (YmdHms)        ; Returns datetime string
; udfGD_Reformationstag (YmdHms)      ; Returns datetime string
; udfGD_HeiligAbend (YmdHms)          ; Returns datetime string
; udfGD_ErsterWeihnachten (YmdHms)    ; Returns datetime string
; udfGD_ZweiterWeihnachten (YmdHms)   ; Returns datetime string
; udfGD_Silvester (YmdHms)            ; Returns datetime string
; udfGD_Neujahr (YmdHms)              ; Returns datetime string
; udfGD_ErsterAdvent (YmdHms)         ; Returns datetime string
; udfGD_ZweiterAdvent (YmdHms)        ; Returns datetime string
; udfGD_DritterAdvent (YmdHms)        ; Returns datetime string
; udfGD_VierterAdvent (YmdHms)        ; Returns datetime string
; udfGD_BussUndBettag (YmdHms)        ; Returns datetime string
; udfGD_HeiligeDreiKoenige (YmdHms)   ; Returns datetime string
; udfGD_Allerheiligen (YmdHms)        ; Returns datetime string
; udfGD_TagDerArbeit (YmdHms)         ; Returns datetime string
;------------------------------------------------------------------------------------------------------------------------------------------
; udfNameOfGD (YmdHms)                ; Returns string (fast speed)
; udfNameOfGD2 (YmdHms)               ; Returns string (slow speed)
; udfGDHashTable (YmdHms, Mode)       ; Returns string (middle speed)
;                                   ... Returns string Mode=0 => creates hash table
;                                   ... Returns string Mode=1 => Name of German Holiday
;                                   ... Returns string Mode=2 => List of German Holidays
;==========================================================================================================================================



;==========================================================================================================================================
; User Defined Functions for Date and Time Calculations.
;==========================================================================================================================================

;==========================================================================================================================================
#DefineFunction udfSayNoYes (bool) ; returns string "No" or "Yes" ; for test purposes
NoYesArray = Arrayize("Nein,Ja",",")
;NoYesArray = Arrayize("No,Yes",",")
Return (NoYesArray[bool])
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfIsValidDate (sYmdHms) ; returns boolean, false or true (0..1)
ciYmdLen = 10 ; StrLen("0000:00:00")
sYmd = StrSub(sYmdHms,1,ciYmdLen)
If (StrLen(sYmd)!=ciYmdLen) Then Return (@FALSE)
iLastErrorMode = ErrorMode(@OFF)
LastError()
TimeDiffDays(sYmd,sYmd)
ErrorMode(iLastErrorMode)
Return (LastError()==0)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsValidDateTime (sYmdHms) ; returns boolean, false or true (0..1)
ciYmdHmsLen = 19 ; StrLen("0000:00:00:00:00:00")
If (StrLen(sYmdHms)!=ciYmdHmsLen) Then Return (@FALSE)
iLastErrorMode = ErrorMode(@OFF)
LastError()
TimeDiff(sYmdHms,sYmdHms)
ErrorMode(iLastErrorMode)
Return (LastError()==0)
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfIsLeapYear (YmdHms) ; returns boolean, false or true (0..1)
Year = ItemExtract(1,YmdHms,":")
Return ((0==(year mod 4))&&(0!=(year mod 4000))&&((0!=(year mod 100))||(0==(year mod 400))))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsJulianLeapYear (sYmdHms) ; returns boolean, false or true (0..1)
iYear = ItemExtract(1,sYmdHms,":")
Return (TimeJulianDay(StrCat(iYear,":03:01"))-TimeJulianDay(StrCat(iYear,":02:28"))>1)
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfJulianDayOfWeek (sYmdHms) ; returns number, range 0..6
Return ((TimeJulianDay(sYmdHms)+5) mod 7) ; Range=0..6 = Sunday..Saturday
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDayOfWeek (sYmdHms) ; returns number, range 1..7
iJulianDow = (TimeJulianDay(sYmdHms)+5) mod 7
Return (iJulianDow+(7*!iJulianDow)) ; Range=1..7 = Monday..Sunday
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDayOfYear (sYmdHms) ; returns number, range=1..366
Return (1+TimeDiffDays(sYmdHms,StrCat(ItemExtract(1,sYmdHms,":"),":01:01:00:00:00")))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfWeekOfYear (sYmdHms) ; returns number, range 0..53
iJulianNow     = TimeJulianDay(sYmdHms)
iJulianJan4    = TimeJulianDay(StrCat(ItemExtract(1,sYmdHms,":"),":01:04:00:00:00"))
iJulianDowJan4 = (iJulianJan4+5) mod 7 ; Range=0..6 = Sunday..Saturday
iJulianMonday  = 1+iJulianJan4-iJulianDowJan4-(7*!iJulianDowJan4)
iWeek          = (iJulianNow-iJulianMonday)/7
iWeek          = iWeek+(iJulianNow>=iJulianMonday)
Return (iWeek)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
;   #DefineFunction udfWeekOfMonth (sYmdHms) ; Returns number, range 1..6.
;   Return (udfWeekOfYear(sYmdHms) - udfWeekOfYear(udfSetDay(sYmdHms,1)) + 1)
;   #EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfWeekOfMonth (sYmdHms, iMode)
iDay = ItemExtract(3,sYmdHms,":")
iDow = (7 + TimeJulianDay(sYmdHms) - iDay - iMode) mod 7
If !iDow Then iDow = 7
iDay = iDay + iDow - 2
Return (1 + (iDay / 7))
;..........................................................................................................................................
; This function udfWeekOfMonth returns the number of the week within the month
; that is given by the first parameter sYmdHms datetime string.
; This function returns a number in range 1..6.
;
; The second parameter iMode controls whether a week begins with Sunday or Monday.
; iMode = 0 ... Week begins with Sunday.
; iMode = 1 ... Week begins with Monday.
;
; Detlev Dalitz.20030819
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfNameOfDay (sYmdHms, iLength)   ; Returns string.
sOut = ItemExtract(1+((TimeJulianDay(sYmdHms)+5) mod 7),"Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag",",")
; sOut = ItemExtract(1+((TimeJulianDay(sYmdHms)+5) mod 7),"Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday",",")
iLength = Max(0,iLength)
If !!iLength Then sOut = StrFix(sOut," ",iLength)
Return (sOut)
; Returns a string with at least iLength number of characters.
; Use iLength=0 for returning the trimmed length of day name as is.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfNameOfMonth (sYmdHms, iLength) ; Returns string.
sOut = ItemExtract(ItemExtract(2,sYmdHms,":"),"Januar,Februar,März,April,Mai,Juni,Juli,August,September,Oktober,November,Dezember",",")
; sOut = ItemExtract(ItemExtract(2,sYmdHms,":"),"January,February,March,April,May,June,July,August,September,October,November,December",",")
iLength = Max(0,iLength)
If !!iLength Then sOut = StrFix(sOut," ",iLength)
Return (sOut)
; Returns a string with at least iLength number of characters.
; Use iLength=0 for returning the trimmed length of month name as is.
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfTimeDMY (YmdHms) ; returns string formatted as "dd.mm.yyyy"
DTArray = Arrayize(YmdHms,":")
Return (StrCat(DTArray[2],".",DTArray[1],".",DTArray[0]))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfTimeDMYShort (YmdHms) ; returns string formatted as "ddd dd.mm.yyyy"
DTArray = Arrayize(YmdHms,":")
Return (StrCat(udfNameOfDay(YmdHms,3)," ",DTArray[2],".",DTArray[1],".",DTArray[0]))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfTimeDMYLong (YmdHms) ; returns string formatted as "dddd dd.mm.yyyy"
DTArray = Arrayize(YmdHms,":")
Return (StrCat(udfNameOfDay(YmdHms,0)," ",DTArray[2],".",DTArray[1],".",DTArray[0]))
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfDaysInYear (YmdHms)
Return (udfIsLeapYear(YmdHms) + 365)
; Returns number, range 365..366.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDaysInMonth (sYmdHms)
iDaysInMonth = ItemExtract(ItemExtract(2,sYmdHms,":"),"31,28,31,30,31,30,31,31,30,31,30,31",",")
If (iDaysInMonth==28) Then iDaysInMonth = iDaysInMonth+udfIsLeapYear(sYmdHms)
Return (iDaysInMonth)
; Returns number, range=28..31.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetLastDayOfMonth (sYmdHms)
Return (ItemReplace(udfDaysInMonth(sYmdHms),3,sYmdHms,":"))
; Returns YmdHms datetime string.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetLastDayOfMonth_2 (sYmdHms)
Return (TimeSubtract(ItemReplace(1,3,TimeAdd(sYmdHms,"0:1:0"),":"),"0:0:1"))
; Returns YmdHms datetime string, leaves Hms as is.
; I prefer this version of udfGetLastDayOfMonth.
; LastDayofNextMonth = TimeSubtract(ItemReplace(1,3,TimeAdd(sYmdHms,"0:2:0"),":"),"0:0:1")
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfIsLastDayInMonth (YmdHms) ; returns boolean, false or true (0..1)
YmdHmsArray = Arrayize(YmdHms,":")
Return (YmdHmsArray[2]==udfDaysInMonth(YmdHms))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfisfirstdayofmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfisfirstdayofmonth

#DefineFunction udfIsFirstDayOfMonth (sYmdHms)
Return (1==(0+ItemExtract(3,sYmdHms,":")))
#EndFunction

:skip_udfisfirstdayofmonth
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfislastdayofmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfislastdayofmonth

#DefineFunction udfIsLastDayOfMonth (sYmdHms)
Return ((0+ItemExtract(2,sYmdHms,":"))!=(0+ItemExtract(2,TimeAdd(sYmdHms,"0:0:1:0:0:0"),":")))
#EndFunction

:skip_udfislastdayofmonth
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfisnthdayofmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfisnthdayofmonth

#DefineFunction udfIsNthDayOfMonth (n, sYmdHms)
If ((n<1)||(n>31))
   Return ((0+ItemExtract(2,sYmdHms,":"))!=(0+ItemExtract(2,TimeAdd(sYmdHms,"0:0:1:0:0:0"),":")))
Else
   Return (n==(0+ItemExtract(3,sYmdHms,":")))
EndIf
; to refer to the "last day" of month use any n out of range 1..31, e.g. n=0
#EndFunction

:skip_udfisnthdayofmonth
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfgetcountdowinmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetcountdowinmonth

#DefineFunction udfGetCountDOWInMonth (sYmdHms, iDayOfWeek)                 ; With iDayOfWeek = 0..6 = Sunday..Saturday.
iDayOfWeek = Min(6,Max(-1,iDayOfWeek))                 ; Limit iDayOfWeek to -1..0..6 days. -1 means current DayofWeek of sYmdHms.
If (iDayOfWeek<0) Then iDayOfWeek = ((TimeJulianDay(sYmdHms)+5) mod 7)      ; 0..6 = Sunday..Saturday.
iMonth = ItemExtract(2,sYmdHms,":")                                         ; Store iMonth for later check.
sYmdHms = ItemReplace("01",3,sYmdHms,":")                                   ; Set the 01.mm.yyyy of month.
iFirstDay = ((TimeJulianDay(sYmdHms)+5) mod 7)                              ; Sunday=0
sYmdHms = TimeAdd(sYmdHms,StrCat("0:0:",(7+iDayOfWeek-iFirstDay) mod 7))    ; Add diff. days to the first occurrence.
iCount = 0
While (iMonth==ItemExtract(2,sYmdHms,":"))
   iCount = iCount+1
   sYmdHms = TimeAdd(sYmdHms,StrCat("0:0:",7))                              ; Add weeks.
EndWhile
Return (iCount)
;..........................................................................................................................................
; This Function "udfGetCountDowInMonth" returns the number of times
; the specified day of the week occurs in the month containing the date.
; If you pass a iDayOfWeek value of -1, then the function counts the number of times the day
; indicated by the date parameter occurs within its own month.
;
; Detlev Dalitz.20020720
;..........................................................................................................................................
#EndFunction

:skip_udfgetcountdowinmonth
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetnthdowinmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetnthdowinmonth

#DefineFunction udfGetNthDOWInMonth (sYmdHms, iOccurrence, iDayOfWeek)
; With iDayOfWeek = 0..6 ; Sunday..Saturday.
@YM0 = "0:0:0"
@YM7 = "0:0:7"
@YMD = "0:0:D"
@D   = "D"
@58  = ":"
iOccurrence = Min(5,Max(1,iOccurrence))                                             ; Limit to 1..5 weeks.
iDayOfWeek  = Min(6,Max(0,iDayOfWeek))                                              ; Limit to 0..6 days.
iMonth  = ItemExtract(2,sYmdHms,@58)                                                ; Save for later check.
sYmdHms = ItemReplace(1,3,sYmdHms,@58)                                              ; Set the 01.mm.yyyy of month.
iFirstDay = ((TimeJulianDay(sYmdHms)+5) mod 7)                                      ; Sunday=0
If ((iOccurrence==1)&&(iDayOfWeek==iFirstDay)) Then Return (TimeAdd(sYmdHms,@YM0))  ; Return if first day of month hits the rule.
sYmdHms = TimeAdd(sYmdHms,StrReplace(@YMD,@D,(7+iDayOfWeek-iFirstDay) mod 7))       ; Add diff. days to the first occurrence.
If (iOccurrence==1) Then Return (sYmdHms)                                           ; Return if this day hits the rule.
sYmdHms = TimeAdd(sYmdHms,StrReplace(@YMD,@D,(7*(iOccurrence-1))))                  ; Add diff. weeks to hit the rule.
If (iMonth!=ItemExtract(2,sYmdHms,@58)) Then sYmdHms = TimeSubtract(sYmdHms,@YM7)   ; Fallback if necessary and subtract 7 days.
Return (sYmdHms)
;..........................................................................................................................................
; This Function "udfGetNthDowInMonth" returns the Nth occurence of the day of the week
; (for example, the second Tuesday) in the specified month.
; If the Nth day cannot be resolved in this month, then the last occurence of day will be returned.
; (for example : the fifth Thursday in July 2002 does not exist (actually it will be calculated to the 01.08.2002),
; so this function returns the last 'good' Thursday 25.07.2002.
;..........................................................................................................................................
#EndFunction

:skip_udfgetnthdowinmonth
;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfgetweekdaynext",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetweekdaynext

#DefineFunction udfGetWeekDayNext (sYmdHms)
iJulianNow = TimeJulianDay(sYmdHms)
While 1
   iJulianNow = iJulianNow+1
   iJulianDowNow = (iJulianNow+5) mod 7
   If (6!=iJulianDowNow) Then If (0!=iJulianDowNow) Then Break ; if not a weekend day then break
EndWhile
Return (TimeJulToYmd(iJulianNow))
;..........................................................................................................................................
; This Function "udfGetWeekDayNext" returns a DateTime string containing the next WeekDay.
; The date whose following weekday (Monday through Friday) is returned.
; Skips over the Weekend if necessary. No check about holidays which are reside on weekdays.
;
; Detlev Dalitz.20020723
;..........................................................................................................................................
#EndFunction

:skip_udfgetweekdaynext
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetweekdayprev",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetweekdayprev

#DefineFunction udfGetWeekDayPrev (sYmdHms)
iJulianNow = TimeJulianDay(sYmdHms)
While 1
   iJulianNow = iJulianNow-1
   iJulianDowNow = (iJulianNow+5) mod 7
   If (6!=iJulianDowNow) Then If (0!=iJulianDowNow) Then Break ; if not a weekend day then break
EndWhile
Return (TimeJulToYmd(iJulianNow))
;..........................................................................................................................................
; This Function "udfGetWeekDayPrev" returns a DateTime string containing the previous WeekDay.
; The date whose previous weekday (Monday through Friday) is returned.
; Skips over the Weekend if necessary. No check about holidays which are reside on weekdays.
;
; Detlev Dalitz.20020723
;..........................................................................................................................................
#EndFunction

:skip_udfgetweekdayprev
;==========================================================================================================================================



;==========================================================================================================================================
#DefineFunction udfGetSundayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetMondayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:1"))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetTuesdayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:2"))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetWednesdayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:3"))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetThursdayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:4"))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetFridayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:5"))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetSaturdayOfWeek (YmdHms) ; returns datetime string ; leaves Hms untouched
Return (TimeAdd(TimeSubtract(YmdHms,StrCat("0:0:",(TimeJulianDay(YmdHms)+5) mod 7)),"0:0:6"))
#EndFunction
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfisweekday",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfisweekday

#DefineFunction udfIsWeekDay (YmdHms)
iJulianDow = ((TimeJulianDay(YmdHms)+5) mod 7)
If (6==iJulianDow) Then Return (@FALSE)
If (0==iJulianDow) Then Return (@FALSE)
Return (@TRUE)
; "Is Date A Weekday"
; Returns @True if a date is a weekday such as Monday, Tuesday, Wednesday, Thursday, or Friday.
#EndFunction

:skip_udfisweekday
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfisweekendday",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfisweekendday

#DefineFunction udfIsWeekendDay (YmdHms)
iJulianDow = ((TimeJulianDay(YmdHms)+5) mod 7)
If (6==iJulianDow) Then Return (@TRUE)
If (0==iJulianDow) Then Return (@TRUE)
Return (@FALSE)
; "Is Date A Weekend Day"
; Returns @True if a date is a weekend day such as Saturday or Sunday.
#EndFunction

:skip_udfisweekendday
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfgetymd",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetymd

#DefineFunction udfGetYmd (sYmdHms)
Return (StrSub(sYmdHms,1,10))
; This Function "udfGetYmd" returns the Ymd date part of the specified sYmdhms.
#EndFunction

:skip_udfgetymd
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgethms",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgethms

#DefineFunction udfGetHms (sYmdHms)
Return (StrSub(sYmdHms,12,-1))
; This Function "udfGetHms" returns the Hms time part of the specified sYmdhms.
#EndFunction
If ItemLocate("udfsetymd",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsetymd
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfsetymd",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsetymd

#DefineFunction udfSetYmd (sYmdHms, sYmd)
sYmdHms = ItemReplace(ItemExtract(1,sYmd,":"),1,sYmdHms,":")
sYmdHms = ItemReplace(ItemExtract(2,sYmd,":")-1,2,sYmdHms,":")
sYmdHms = ItemReplace(ItemExtract(3,sYmd,":")-1,3,sYmdHms,":")
Return (TimeAdd("0000:01:01:00:00:00",sYmdHms)) ; make valid datetime string
; This Function "udfSetYmd" sets the Ymd date part of the sYmdHms to the specified sYmd date value.
#EndFunction

:skip_udfsetymd
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfsethms",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsethms

#DefineFunction udfSetHms (sYmdHms, sHms)
sYmdHms = ItemReplace("00",4,sYmdHms,":")
sYmdHms = ItemReplace("00",5,sYmdHms,":")
sYmdHms = ItemReplace("00",6,sYmdHms,":")
Return (TimeAdd(sYmdHms,StrCat("0:0:0:",sHms))) ; make valid datetime string
; This Function "udfsetHms" sets the Hms time part of the sYmdHms to the specified sHms time value.
#EndFunction

:skip_udfsethms
;==========================================================================================================================================



;==========================================================================================================================================
If ItemLocate("udfgetyear",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetyear

#DefineFunction udfGetYear (sYmdHms)
Return (ItemExtract(1,sYmdHms,":"))
; This Function "udfGetYear" returns the number of the year of the specified date.
#EndFunction

:skip_udfgetyear
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetmonth",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetmonth

#DefineFunction udfGetMonth (sYmdHms)
Return (ItemExtract(2,sYmdHms,":"))
; This Function "udfGetMonth" returns the number of the month of the specified date.
#EndFunction

:skip_udfgetmonth
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetday",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetday

#DefineFunction udfGetDay (sYmdHms)
Return (ItemExtract(3,sYmdHms,":"))
; This Function "udfGetDay" returns the number of the day of the specified date.
#EndFunction

:skip_udfgetday
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgethour",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgethour

#DefineFunction udfGetHour (sYmdHms)
Return (ItemExtract(4,sYmdHms,":"))
; This Function "udfGetHour" returns the number of the hour of the specified date.
#EndFunction

:skip_udfgethour