Attribute VB_Name = "CalendarUtilities"
Option Explicit
Public Enum MyMonth
    MM_JANUARY = 1
    MM_FEBRUARY = 2
    MM_MARCH = 3
    MM_APRIL = 4
    MM_MAY = 5
    MM_JUNE = 6
    MM_JULY = 7
    MM_AUGUST = 8
    MM_SEPTEMBER = 9
    MM_OCTOBER = 10
    MM_NOVEMBER = 11
    MM_DECEMBER = 12
End Enum

Public Enum MYDAY
    MD_SUNDAY = 1
    MD_MONDAY = 2
    MD_TUESDAY = 3
    MD_WEDNESDAY = 4
    MD_THURSDAY = 5
    MD_FRIDAY = 6
    MD_SATURDAY = 7
End Enum

Public Function cFormatMonth(ByVal MonthNum As MyMonth, Optional ByVal cLanguage As MONIERLANGUAGE = ML_ENGLISH) As String
    Select Case cLanguage
        Case ML_ENGLISH
            Select Case MonthNum
                Case MM_JANUARY
                    cFormatMonth = "January"
                Case MM_FEBRUARY
                    cFormatMonth = "February"
                Case MM_MARCH
                    cFormatMonth = "March"
                Case MM_APRIL
                    cFormatMonth = "April"
                Case MM_MAY
                    cFormatMonth = "May"
                Case MM_JUNE
                    cFormatMonth = "June"
                Case MM_JULY
                    cFormatMonth = "July"
                Case MM_AUGUST
                    cFormatMonth = "August"
                Case MM_SEPTEMBER
                    cFormatMonth = "September"
                Case MM_OCTOBER
                    cFormatMonth = "October"
                Case MM_NOVEMBER
                    cFormatMonth = "November"
                Case MM_DECEMBER
                    cFormatMonth = "December"
            End Select
        
        Case ML_MALAGASY
            Select Case MonthNum
                Case 1
                    cFormatMonth = "Janoary"
                Case 2
                    cFormatMonth = "Febroary"
                Case 3
                    cFormatMonth = "Martsa"
                Case 4
                    cFormatMonth = "Aprily"
                Case 5
                    cFormatMonth = "May"
                Case 6
                    cFormatMonth = "Jiona"
                Case 7
                    cFormatMonth = "Jolay"
                Case 8
                    cFormatMonth = "Aogositra"
                Case 9
                    cFormatMonth = "Septambra"
                Case 10
                    cFormatMonth = "Oktobra"
                Case 11
                    cFormatMonth = "Novambra"
                Case 12
                    cFormatMonth = "Desambra"
            End Select
            
        Case ML_FRANCAIS
            Select Case MonthNum
                Case 1
                    cFormatMonth = "Janvier"
                Case 2
                    cFormatMonth = "Février"
                Case 3
                    cFormatMonth = "Mars"
                Case 4
                    cFormatMonth = "Avril"
                Case 5
                    cFormatMonth = "Mai"
                Case 6
                    cFormatMonth = "Jiun"
                Case 7
                    cFormatMonth = "Juillet"
                Case 8
                    cFormatMonth = "Août"
                Case 9
                    cFormatMonth = "Septembre"
                Case 10
                    cFormatMonth = "Octobre"
                Case 11
                    cFormatMonth = "Novembre"
                Case 12
                    cFormatMonth = "Decembre"
            End Select
    End Select
End Function

Public Function cFormat3CharsMonth(ByVal MonthNum As MyMonth, Optional ByVal cLanguage As MONIERLANGUAGE = ML_ENGLISH) As String
    Select Case cLanguage
        Case ML_ENGLISH
            Select Case MonthNum
                Case MM_JANUARY
                    cFormat3CharsMonth = "Jan"
                Case MM_FEBRUARY
                    cFormat3CharsMonth = "Feb"
                Case MM_MARCH
                    cFormat3CharsMonth = "Mar"
                Case MM_APRIL
                    cFormat3CharsMonth = "Apr"
                Case MM_MAY
                    cFormat3CharsMonth = "May"
                Case MM_JUNE
                    cFormat3CharsMonth = "Jun"
                Case MM_JULY
                    cFormat3CharsMonth = "Jul"
                Case MM_AUGUST
                    cFormat3CharsMonth = "Aug"
                Case MM_SEPTEMBER
                    cFormat3CharsMonth = "Sep"
                Case MM_OCTOBER
                    cFormat3CharsMonth = "Oct"
                Case MM_NOVEMBER
                    cFormat3CharsMonth = "Nov"
                Case MM_DECEMBER
                    cFormat3CharsMonth = "Dec"
            End Select
        
        Case ML_MALAGASY
            Select Case MonthNum
                Case 1
                    cFormat3CharsMonth = "Jan"
                Case 2
                    cFormat3CharsMonth = "Feb"
                Case 3
                    cFormat3CharsMonth = "Mar"
                Case 4
                    cFormat3CharsMonth = "Apr"
                Case 5
                    cFormat3CharsMonth = "May"
                Case 6
                    cFormat3CharsMonth = "Jio"
                Case 7
                    cFormat3CharsMonth = "Jol"
                Case 8
                    cFormat3CharsMonth = "Aog"
                Case 9
                    cFormat3CharsMonth = "Sep"
                Case 10
                    cFormat3CharsMonth = "Okt"
                Case 11
                    cFormat3CharsMonth = "Nov"
                Case 12
                    cFormat3CharsMonth = "Des"
            End Select
            
        Case ML_FRANCAIS
            Select Case MonthNum
                Case 1
                    cFormat3CharsMonth = "Jan"
                Case 2
                    cFormat3CharsMonth = "Fév"
                Case 3
                    cFormat3CharsMonth = "Mar"
                Case 4
                    cFormat3CharsMonth = "Avr"
                Case 5
                    cFormat3CharsMonth = "Mai"
                Case 6
                    cFormat3CharsMonth = "Jiu"
                Case 7
                    cFormat3CharsMonth = "Jul"
                Case 8
                    cFormat3CharsMonth = "Aou"
                Case 9
                    cFormat3CharsMonth = "Sep"
                Case 10
                    cFormat3CharsMonth = "Oct"
                Case 11
                    cFormat3CharsMonth = "Nov"
                Case 12
                    cFormat3CharsMonth = "Dec"
            End Select
    End Select
End Function

Public Function cTranslate3CharsMonth(ByVal sMonth As String, Optional ByVal cLanguage As MONIERLANGUAGE = ML_ENGLISH) As MyMonth
    Select Case cLanguage
        Case ML_ENGLISH
            Select Case UCase(sMonth)
                Case UCase("jan")
                    cTranslate3CharsMonth = MM_JANUARY
                Case UCase("feb")
                    cTranslate3CharsMonth = MM_FEBRUARY
                Case UCase("mar")
                    cTranslate3CharsMonth = MM_MARCH
                Case UCase("apr")
                    cTranslate3CharsMonth = MM_APRIL
                Case UCase("may")
                    cTranslate3CharsMonth = MM_MAY
                Case UCase("jun")
                    cTranslate3CharsMonth = MM_JUNE
                Case UCase("jul")
                    cTranslate3CharsMonth = MM_JULY
                Case UCase("aug")
                    cTranslate3CharsMonth = MM_AUGUST
                Case UCase("sep")
                    cTranslate3CharsMonth = MM_SEPTEMBER
                Case UCase("oct")
                    cTranslate3CharsMonth = MM_OCTOBER
                Case UCase("nov")
                    cTranslate3CharsMonth = MM_NOVEMBER
                Case UCase("dec")
                    cTranslate3CharsMonth = MM_DECEMBER
            End Select
        
        Case ML_MALAGASY
            Select Case UCase(sMonth)
                Case UCase("jan")
                    cTranslate3CharsMonth = MM_JANUARY
                Case UCase("feb")
                    cTranslate3CharsMonth = MM_FEBRUARY
                Case UCase("mar")
                    cTranslate3CharsMonth = MM_MARCH
                Case UCase("apr")
                    cTranslate3CharsMonth = MM_APRIL
                Case UCase("may")
                    cTranslate3CharsMonth = MM_MAY
                Case UCase("jon")
                    cTranslate3CharsMonth = MM_JUNE
                Case UCase("jol")
                    cTranslate3CharsMonth = MM_JULY
                Case UCase("aog")
                    cTranslate3CharsMonth = MM_AUGUST
                Case UCase("sep")
                    cTranslate3CharsMonth = MM_SEPTEMBER
                Case UCase("okt")
                    cTranslate3CharsMonth = MM_OCTOBER
                Case UCase("nov")
                    cTranslate3CharsMonth = MM_NOVEMBER
                Case UCase("dec")
                    cTranslate3CharsMonth = MM_DECEMBER
            End Select
            
        Case ML_FRANCAIS
            Select Case UCase(sMonth)
                Case UCase("jan")
                    cTranslate3CharsMonth = MM_JANUARY
                Case UCase("fév")
                    cTranslate3CharsMonth = MM_FEBRUARY
                Case UCase("mar")
                    cTranslate3CharsMonth = MM_MARCH
                Case UCase("avr")
                    cTranslate3CharsMonth = MM_APRIL
                Case UCase("mai")
                    cTranslate3CharsMonth = MM_MAY
                Case UCase("jui")
                    cTranslate3CharsMonth = MM_JUNE
                Case UCase("jul")
                    cTranslate3CharsMonth = MM_JULY
                Case UCase("aoû")
                    cTranslate3CharsMonth = MM_AUGUST
                Case UCase("sep")
                    cTranslate3CharsMonth = MM_SEPTEMBER
                Case UCase("oct")
                    cTranslate3CharsMonth = MM_OCTOBER
                Case UCase("nov")
                    cTranslate3CharsMonth = MM_NOVEMBER
                Case UCase("dec")
                    cTranslate3CharsMonth = MM_DECEMBER
            End Select
    End Select
End Function

Public Function cFormatDay(ByVal cDayNum As MYDAY, Optional ByVal cLanguage As MONIERLANGUAGE = ML_ENGLISH) As String
    Select Case cLanguage
        Case ML_ENGLISH
            Select Case cDayNum
                Case 1
                    cFormatDay = "Sunday"
                Case 2
                    cFormatDay = "Monday"
                Case 3
                    cFormatDay = "Tuesday"
                Case 4
                    cFormatDay = "Wednesday"
                Case 5
                    cFormatDay = "Thursday"
                Case 6
                    cFormatDay = "Friday"
                Case 7
                    cFormatDay = "Saturday"
            End Select
            
        Case ML_MALAGASY
            Select Case cDayNum
                Case 1
                    cFormatDay = "Alahady"
                Case 2
                    cFormatDay = "Alatsinainy"
                Case 3
                    cFormatDay = "Talata"
                Case 4
                    cFormatDay = "Alarobia"
                Case 5
                    cFormatDay = "Alakamisy"
                Case 6
                    cFormatDay = "Zoma"
                Case 7
                    cFormatDay = "Sabotsy"
            End Select
            
        Case ML_FRANCAIS
            Select Case cDayNum
                Case 1
                    cFormatDay = "Dimanche"
                Case 2
                    cFormatDay = "Lundi"
                Case 3
                    cFormatDay = "Mardi"
                Case 4
                    cFormatDay = "Mercredi"
                Case 5
                    cFormatDay = "Jeudi"
                Case 6
                    cFormatDay = "Vendredi"
                Case 7
                    cFormatDay = "Samedi"
            End Select
    End Select
End Function

Public Function cFormat3CharsDay(ByVal cDayNum As MYDAY, ByVal cLanguage As MONIERLANGUAGE) As String
    Select Case cLanguage
        Case ML_ENGLISH
            Select Case cDayNum
                Case 1
                    cFormat3CharsDay = "SUN"
                Case 2
                    cFormat3CharsDay = "MON"
                Case 3
                    cFormat3CharsDay = "TUE"
                Case 4
                    cFormat3CharsDay = "WED"
                Case 5
                    cFormat3CharsDay = "THU"
                Case 6
                    cFormat3CharsDay = "FRI"
                Case 7
                    cFormat3CharsDay = "SAT"
            End Select
            
        Case ML_MALAGASY
            Select Case cDayNum
                Case 1
                    cFormat3CharsDay = "ALH"
                Case 2
                    cFormat3CharsDay = "ALT"
                Case 3
                    cFormat3CharsDay = "TLT"
                Case 4
                    cFormat3CharsDay = "ALR"
                Case 5
                    cFormat3CharsDay = "ALK"
                Case 6
                    cFormat3CharsDay = "ZOM"
                Case 7
                    cFormat3CharsDay = "SAB"
            End Select
            
        Case ML_FRANCAIS
            Select Case cDayNum
                Case 1
                    cFormat3CharsDay = "DIM"
                Case 2
                    cFormat3CharsDay = "LUN"
                Case 3
                    cFormat3CharsDay = "MAR"
                Case 4
                    cFormat3CharsDay = "MER"
                Case 5
                    cFormat3CharsDay = "JEU"
                Case 6
                    cFormat3CharsDay = "VEN"
                Case 7
                    cFormat3CharsDay = "SAM"
            End Select
    End Select
End Function

Public Function cFindDayCount(ByVal eMonth As MyMonth, ByVal eYear As Integer) As Byte
    Select Case eMonth
        Case MM_JANUARY
            cFindDayCount = 31
        Case MM_FEBRUARY
            If eYear Mod 4 = 0 Then
                cFindDayCount = 29
            Else
                cFindDayCount = 28
            End If
        Case MM_MARCH
            cFindDayCount = 31
        Case MM_APRIL
            cFindDayCount = 30
        Case MM_MAY
            cFindDayCount = 31
        Case MM_JUNE
            cFindDayCount = 30
        Case MM_JULY
            cFindDayCount = 31
        Case MM_AUGUST
            cFindDayCount = 31
        Case MM_SEPTEMBER
            cFindDayCount = 30
        Case MM_OCTOBER
            cFindDayCount = 31
        Case MM_NOVEMBER
            cFindDayCount = 30
        Case MM_DECEMBER
            cFindDayCount = 31
    End Select
End Function

Public Function cFormatDate(ByVal eDate As Date, Optional ByVal cLanguage As MONIERLANGUAGE = ML_ENGLISH) As String
    Select Case cLanguage
        Case ML_ENGLISH
            cFormatDate = cFormatDay(DatePart("w", eDate), ML_ENGLISH) & ", " & cFormatMonth(DatePart("m", eDate), ML_ENGLISH) _
                            & " " & DatePart("d", eDate) & ", " & DatePart("yyyy", eDate)
        Case Else
            cFormatDate = cFormatDay(DatePart("w", eDate), cLanguage) & " " & DatePart("d", eDate) & " " _
                            & cFormatMonth(DatePart("m", eDate), cLanguage) & " " & DatePart("yyyy", eDate)
    End Select
End Function

Public Function CalculateYear(ByVal sDate1 As String, ByVal sDate2 As String) As Long
    If sDate1 <> "" And sDate2 <> "" Then
        If IsDate(sDate1) And IsDate(sDate2) Then
            CalculateYear = Abs(DateDiff("yyyy", sDate1, sDate2))
        End If
    End If
End Function

Public Function cResizeArrays(arrCountainer() As Byte, ByVal eDate As Date)
    Dim iDateCount As Integer
    ReDim arrCountainer(0 To 41)
    Dim iCnt As Integer
    Dim iTmpVal As Integer
    Dim iCnt2 As Integer
    
    iDateCount = cFindDayCount(DatePart("m", eDate), DatePart("yyyy", eDate))
    For iCnt = 0 To 41
        arrCountainer(iCnt) = 0
    Next iCnt
    
    iTmpVal = DatePart("w", DateSerial(DatePart("yyyy", eDate), DatePart("m", eDate), 1))
    
    iCnt2 = 1
    For iCnt = 0 To iDateCount - 1
        arrCountainer(iCnt + iTmpVal - 1) = iCnt2
        iCnt2 = iCnt2 + 1
    Next iCnt
End Function

Public Sub cMatrixArraysInLabels(oLabel As Object, arrCountainer() As Byte)
    ReDim Preserve arrCountainer(0 To 41)
    Dim iCnt As Integer
    For iCnt = 0 To 41
        If arrCountainer(iCnt) > 0 Then
            oLabel(iCnt).Caption = arrCountainer(iCnt)
            oLabel(iCnt).Enabled = True
        Else
            oLabel(iCnt).Caption = ""
            oLabel(iCnt).Enabled = False
        End If
    Next iCnt
End Sub

Public Function cGetArrayEnd(ByVal cIndex As Integer) As Integer
    Do While cIndex > 6
        cIndex = cIndex - 7
    Loop
    cGetArrayEnd = cIndex
End Function

Public Function GetNewDate(Optional ByVal Hwnd As Long, Optional ByVal InitDate As Date = "24/06/1987") As String
    EnableWindow Hwnd, 0
    Dim NForm As New DateForm
    NForm.sDate = InitDate
    NForm.Show
    Do
        DoEvents
    Loop Until NForm.Visible = False
    If NForm.bCancel = True Then
        GetNewDate = InitDate
    Else
        GetNewDate = NForm.sDate
    End If
    EnableWindow Hwnd, 1
End Function

Public Function IsDateLess(ByVal dDate1 As Date, ByVal dDate2 As Date) As Boolean
    Dim lInterval As Long
    lInterval = DateDiff("d", dDate1, dDate2)
    If lInterval > 0 Then
        IsDateLess = True
    End If
End Function

Public Function IsDateLessEqual(ByVal dDate1 As Date, ByVal dDate2 As Date) As Boolean
    Dim lInterval As Long
    lInterval = DateDiff("d", dDate1, dDate2)
    If lInterval >= 0 Then
        IsDateLessEqual = True
    End If
End Function

Public Function IsDateEqual(ByVal dDate1 As Date, ByVal dDate2 As Date) As Boolean
    Dim lInterval As Long
    lInterval = DateDiff("d", dDate1, dDate2)
    If lInterval = 0 Then
        IsDateEqual = True
    End If
End Function

Public Function IsDateGreater(ByVal dDate1 As Date, ByVal dDate2 As Date) As Boolean
    Dim lInterval As Long
    lInterval = DateDiff("d", dDate1, dDate2)
    If lInterval < 0 Then
        IsDateGreater = True
    End If
End Function

Public Function IsDateGreaterEqual(ByVal dDate1 As Date, ByVal dDate2 As Date) As Boolean
    Dim lInterval As Long
    lInterval = DateDiff("d", dDate1, dDate2)
    If lInterval <= 0 Then
        IsDateGreaterEqual = True
    End If
End Function

Public Function IsTimeBetween(ByVal dDate As Date, ByVal dDateLow As Date, ByVal dDateHigh As Date) As Boolean
    Dim lInterval As Double
    lInterval = DateDiff("s", dDateLow, dDate)
    If lInterval >= 0 Then
        lInterval = DateDiff("s", dDate, dDateHigh)
        If lInterval >= 0 Then
            IsTimeBetween = True
        Else
            IsTimeBetween = False
        End If
    Else
        IsTimeBetween = False
    End If
End Function

Public Function IsDateBetween(ByVal dDate As Date, ByVal dDateLow As Date, ByVal dDateHigh As Date) As Boolean
    Dim lInterval As Double
    lInterval = DateDiff("d", dDateLow, dDate)
    If lInterval >= 0 Then
        lInterval = DateDiff("d", dDate, dDateHigh)
        If lInterval >= 0 Then
            IsDateBetween = True
        Else
            IsDateBetween = False
        End If
    Else
        IsDateBetween = False
    End If
End Function

Public Function GetDateBeforeMonth(ByVal dDate As String, ByVal iMonth As Integer) As String
    Dim sTmpVal As String
    Dim iTmpMonth As Integer
    Dim iTmpYear As Integer
    
    iTmpMonth = DatePart("m", CDate(dDate))
    iTmpYear = DatePart("yyyy", CDate(dDate))
    iTmpMonth = iTmpMonth - iMonth
    If iTmpMonth <= 0 Then
        iTmpMonth = iTmpMonth + 12
        iTmpYear = iTmpYear - 1
    End If
    
    GetDateBeforeMonth = DateSerial(iTmpYear, iTmpMonth, DatePart("d", CDate(Date)))
End Function
