
Public Class ThisWorkbook

    Private Sub ThisWorkbook_Startup() Handles Me.Startup
        Globals.Ribbons.Ribbon1.TodayLbl.Label = Today
        Globals.Ribbons.Ribbon1.TodayShamsiLbl.Label = ShamsiCal.Miladi2Shamsi(Today)
        Dim RTL_char As String = ChrW(8207)
        Globals.Ribbons.Ribbon1.Label1.Label = RTL_char + "شمسی:"
        Globals.Ribbons.Ribbon1.Label2.Label = RTL_char + "میلادی:"
    End Sub

    Private Sub ThisWorkbook_Shutdown() Handles Me.Shutdown

    End Sub

    Public Shared Function calcTotalDays(baseRange As Excel.Range) As String
        Dim SDyear, SDmonth, SDday
        Dim FDyear, FDmonth, FDday

        Dim StartDate, FinishDate
        Dim sdArray As Array, fdArray As Array

        StartDate = baseRange.Value2
        FinishDate = baseRange.Offset(0, 1).Value2

        If StartDate.ToString.Trim = "" Or FinishDate.ToString.Trim = "" Then
            Throw New NullReferenceException
        End If

        sdArray = StartDate.ToString.Split("-")
        fdArray = FinishDate.ToString.Split("-")

        If (sdArray.Length <> 3 Or fdArray.Length <> 3) Then
            Throw New InvalidExpressionException
        End If

        SDday = CInt(sdArray(2))
        SDmonth = CInt(sdArray(1))
        SDyear = CInt(sdArray(0))

        FDday = CInt(fdArray(2))
        FDmonth = CInt(fdArray(1))
        FDyear = CInt(fdArray(0))

        Dim StartDt As Date
        Dim FinDt As Date
        StartDt = Pcalander.ToDateTime(SDyear, SDmonth, SDday, 0, 0, 0, 0)
        FinDt = Pcalander.ToDateTime(FDyear, FDmonth, FDday, 0, 0, 0, 0)

        Dim subt As TimeSpan = FinDt.Subtract(StartDt)

        Dim DaysTotal = subt.Days + 1

        If (DaysTotal < 0) Then
            Throw New InvalidExpressionException
        End If

        Return DaysTotal.ToString

    End Function

    Public Shared Function calcTotalDateString(DaysTotal As Integer) As String
        Dim YearsBase = 365
        Dim MonthsBase = 30
        'Dim DaysBase = 30


        Dim years As UInt32 = Int(DaysTotal / YearsBase)
        Dim months As UInt32 = Int(Int(DaysTotal - 365 * years) / MonthsBase)
        Dim days As UInt32 = DaysTotal - months * MonthsBase - years * YearsBase 'Int(Int(subt.Days - 12 * months) / DaysBase)

        Dim res As String = years.ToString("00") + "-" + String.Format(months.ToString("D2"), "00") + "-" + String.Format(days.ToString("D2"), "00")

        Return res
    End Function


End Class
