Imports System.Globalization
Imports System.String

Module ShamsiCal
    Public Pcalander As PersianCalendar = New PersianCalendar

    ''' <summary>
    ''' تاریخ میلادی را به شمسی تبدیل میکند
    ''' </summary>
    ''' <param name="MiladiDate">تاریخ میلادی موردنظر را وارد کنید</param>
    ''' <returns>YYYY-MM-DD</returns>
    ''' <remarks></remarks>
    Public Function Miladi2Shamsi(MiladiDate As DateTime, Optional Seprator As String = "/") As String
        Dim PrsClnd As PersianCalendar = New PersianCalendar()
        Dim Year As String = PrsClnd.GetYear(MiladiDate).ToString("0000")
        Dim Month As String = PrsClnd.GetMonth(MiladiDate).ToString("00")
        Dim Day As String = PrsClnd.GetDayOfMonth(MiladiDate).ToString("00")
        Return (Year & Seprator & Month & Seprator & Day)
    End Function

    ' ''' <summary>
    ' ''' Gets start date and time and finish date and time then returns per
    ' ''' </summary>
    ' ''' <param name="ShamsiDate">Shamsi date like: YYYY/MM/DD</param>
    ' ''' <param name="time">Time like: 00:00:00</param>
    ' ''' <returns></returns>
    ' ''' <remarks></remarks>
    'Public Function GetPersianCalObjectFrom(ShamsiDate As String, time As String, Optional separator As String = "/") 'StartDate As String, StartTime As String, FinDate As String, FinTime As String) As PersianCalendar
    '    Dim PrsClnd As PersianCalendar = New PersianCalendar()
    '    Dim datearray() As String = ShamsiDate.Split(separator)
    '    If datearray.Length > 2 Then
    '        PrsClnd.AddYears(PrsClnd, Val(datearray(0)))
    '        PrsClnd.
    '    End If
    '    Return PrsClnd
    'End Function

    Public Function Shamsi2Miladi(ShamsiDate As String, Optional time As String = "00:00", Optional separator As String = "/") As DateTime
        Dim FormatString As String = "YYYY" + separator + "MM" + separator + "DD" + " " + "HH:MM:SS"
        Dim PrsClnd As PersianCalendar = New PersianCalendar()
        Dim GregDate As DateTime = New DateTime()


        Dim dt() As String = ShamsiDate.Split(separator)
        Dim YYYY As String = Val(dt(0))
        Dim MM As String = Val(dt(1))
        Dim DD As String = Val(dt(2))

        Dim ti() As String = time.Split(":")
        Dim hours = ti(0)
        Dim mins = ti(1)

        GregDate = PrsClnd.ToDateTime(YYYY, MM, DD, 0, 0, 0, 0)
        If Val(hours) + Val(mins) = 0 Then '+ Val(secs) = 0 Then
            Return GregDate.ToString(Left(FormatString, 10))
        End If
        Return GregDate.ToString(FormatString)
    End Function

    ''' <summary>
    ''' Calculates duration between two shamsi DateTime
    ''' </summary>
    ''' <param name="startTime">Time like: 00:00:00</param>
    ''' <param name="StartDateShamsi">Shamsi date like: YYYY/MM/DD or YYYY-MM-DD (Remember to set separator if you use "-" instead of "/")</param>
    ''' <param name="FinishTime">Time like: 00:00:00</param>
    ''' <param name="FinishDateShamsi">Shamsi date like: YYYY/MM/DD or YYYY-MM-DD (Remember to set separator if you use "-" instead of "/")</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DurationOfShamsiDates(ByVal startTime As String, ByVal StartDateShamsi As String, ByVal FinishTime As String _
                                          , ByVal FinishDateShamsi As String, Optional separator As String = "/") As TimeSpan
        'Dim sd1 As DateTime = Shamsi2Miladi(StartDate, separator)
        'Dim fd2 As DateTime = Shamsi2Miladi(FinishDate, separator)

        'Dim prs As PersianCalendar

        Dim FormatString As String = "YYYY" + separator + "MM" + separator + "DD" + " " + "HH:MM:SS"

        Dim StartDate = Shamsi2Miladi(StartDateShamsi, startTime, separator)
        Dim FinishDate = Shamsi2Miladi(FinishDateShamsi, FinishTime, separator)

        'Dim StartDateString As String = StartDate + " " + startTime
        Dim startdatetime As DateTime = DateTime.ParseExact(StartDate, FormatString, CultureInfo.InvariantCulture)
        Dim Finishdatetime As DateTime = DateTime.ParseExact(FinishDate, FormatString, CultureInfo.InvariantCulture)

        Dim Diff As TimeSpan = Finishdatetime - startdatetime

        Return Diff
    End Function

End Module
