' QDate and QTime
' Both extend QTimer 
' making them non-display objects
' gives easy to use Date and Time Functions
'
' QDate:
'    Now (format)as string: Current date as MM-DD-YYYY
'    ValidateDateString( date string) as integer: Validates format of date string
'        1=ok - 0=error
'    Month(date string) as long
'        if empty string then returns current month
'        else returns month of date string
'    Day(date string) as long
'        if empty string then returns current day
'        else returns day of date string     
'    Year(date string) as long
'        if empty string then returns current year
'        else returns year of date string
'    DayOfWeek(date string) as integer
'        if empty then returns current day of week
'        else returns day of week of date string
'    SerialDate(date string) as long
'        if empty then returns serial date of current date
'        else serial date of date string
'        Like Excel SerialDate function
'    DaysBetweenDates(date1 string, date2 string) as long
'        returns the number of days between dates
'    MDYtoString(month integer, day integer, year integer) date string    
'        converts Month, Day and year integers to date string
'    SerialToString(Serial date) date string
'        converts a serial date to a string
'
' QTime:
'    Now(format)  as string : Current Time as HH:M:SS
'        with format=12 then formats time as HH:MM:SS am/pm
'    ValidateTimeString(time string)  -1=ok - 0=error 
'    Hours(time string) as integer
'        if time string is empty then return hour sof current time
'        else returns hours of time string
'    Minute(time string) as integer
'        if time string is empty then return minutes of current time
'        else returns minutes of time string
'     Seconds(time string) as integer
'        if time string is empty then return seconds of current time
'        else returns seconds of  time string
'    HMSToString(hours integer, minutes integer, seconds integer, format integer) as string
'        Converts hours, minutes and seconds to string
'        if format=12 then uses am/am format 
'    Serialtime(t string) as double
'        converts time string to serial time
'    SerialToString(serial double) as string
'        Converts serial time to string

type QDate extends QTimer
    yy as integer
    mm as integer
    dd as integer
    
    Function Now as string
       result=Date$
    end function

    function ValidateDateString(d as string) as integer
        dim r as integer, a as integer
        r=0
        if len(d)= 10 then
            r=1
            for a=1 to 10
                select case a
                    case 1,2,4,5,7,8,9,10
                        if mid$(d,a,1)<"0" or mid$(d,a,1)>"9" then
                            r=0
                            a=10
                        end if
                    case 3,6
                        if mid$(d,a,1)<>"-" and mid$(d,a,1)<>"/" then
                            r=0
                            a=10
                        end if
                end select
            next
        end if
        result=r
    end function        
        
    function Month(d as string) as integer
        dim n as string
        n=date$
        if d<>"" then n=d
        if QDate.ValidateDateString(n) then
            result = val(left$(n,2))
        else
            result = -1
        end if
    end function
    
    function Year(d as string) as integer
        dim n as string
        n=date$
        if d<>"" then n=d
        if QDate.ValidateDateString(n) then
            result = val(right$(n,4))
        else
            result = -1
        end if
    end function
    
    function Day(d as string) as integer
        dim n as string
        n=date$
        if d<>"" then n=d
        if QDate.ValidateDateString(n) then
            result = val(mid$(n,4,2))
        else
            result = -1
        end if
    end function

    Function DayOfWeek(d as string) AS INTEGER
        defint c
        defstr n = date$
        defint r=-1

        if d<>"" then n=d
        if QDate.ValidateDateString(n) then
            mm = val(left$(n,2))
            dd = val(mid$(n,4,2))
            yy = val(right$(n,4))
            c=0
            c=c+(yy-1900)*365 + int((yy-1900)/4)
            for i=1 to mm-1
                select case i
                    case 1,3,5,7,8,10
                        c=c+31
                    case 2
                        c=c+28 
                        if ((yy-1900) mod 4) = 0 then c=c+1
                    case 4,6,9,11
                        c=c+30
               end select    
            next
            c=c+dd
            result = (c mod 7)
        else
            result = -1
        end if
    END FUNCTION
    
    function SerialDate(d as string) as long
        dim c as long
        dim i as integer
        
        c=-1
        if QDate.ValidateDateString(d) then
            n=d
            mm = val(left$(d,2))
            dd = val(mid$(d,4,2))
            yy = val(right$(d,4))
            c=0
            c=c+(yy-1900)*365+int((yy-1900)/4)
            for i=1 to mm-1
                select case i
                    case 1,3,5,7,8,10
                        c=c+31
                    case 2
                        c=c+28 
                        if ((yy-1900) mod 4) = 0 then c=c+1
                    case 4,6,9,11
                        c=c+30
               end select    
            next
            c=c+dd
        end if
        result=c
    end function
    
    function DaysBetweenDates(d1 as string, d2 as string) as long
        dim n1 as long, n2 as long, r as long
        r=-1
        if QDate.ValidateDateString(d1) and QDate.ValidateDateString(d2) then
            n1=Qdate.SerialDate(d1)
            n2=Qdate.SerialDate(d2)
            r = abs(n1-n2)-1
         end if
         result=r
    end Function
    
    function MDYtoString(mm as integer, dd as integer, yy as integer) as string
        dim s as string
        s=""
        if mm>0 and dd>0 and yy>0 then
            if (yy<100 or yy>1899) then
                if mm<10 then s=s+"0"
                s=s+str$(mm)+"-"
                if dd<10 then s=s+"0"
                s=s+str$(dd)+"-"
                if yy<100 then yy=yy+1900
                s=s+str$(yy)
            end if
        end if
        result=s
    end function
    
    Function SerialToString(d as long) as string
        dim mm as integer
        dim dd as integer
        dim yy as integer
        
        dim n as integer, n1 as integer
        dim s as string
        
        dim A1 as integer
        dim B1 as integer
        DIM C1 as integer
        Dim D1 as integer
        Dim E1 AS integer
        
        a1=d
        b1=int(a1/365)
        c1=round(b1/4)
        d1=a1-(b1*365+c1)
        E1=a1-d1
        yy=b1+1900
        mm=0
        for n=1 to 11
            select case n
                case 1,3,5,7,8,10
                    if(d1-31)>=0 then
                        d1=d1-31
                    else
                        mm=n
                        n=12
                    end if  
                case 2
                    n1=28
                    if (b1 mod 4)=0 then n=29
                    if (d1-n1)>=0 then
                        d1=d1-n1
                    else
                        mm=n
                        n=12
                    end if
                case 4,6,9,11
                    if(d1-30)>=0 then
                        d1=d1-30
                    else
                        mm=n
                        n=12
                    end if
            end select
        next
        dd=d1
        result=QDate.MDYtoString(mm,dd,yy)
    end function
end type



type QTime extends QTimer
    hh as integer
    mm as integer
    ss as integer
    tt as string
       
    function ValidateTimeString(t as string) as integer
        dim r as integer, a as integer
         r=0
        if len(t)=8 then
            r=1
            for a=1 to 8
                select case a
                        case 1,2,4,5,7,8
                            if mid$(t,a,1))<"0" or mid$(t,a,1)>"9" then
                                r=0
                                a=8
                            end if
                        case 3,6
                            if mid$(t,a,1)<>":" then
                                r=0
                                a=8
                            end if
                 end select
        end if    
    end function

    function Hours(t as string) as integer
        if t="" then t=time$
        
        hh=-1
        if QTime.ValidateTimeString(t) then hh=val(left$(t,2))
        
        result = hh
    end function
        
    function Minutes(t as string) as integer
        if t="" then t=time$
        
        mm-1
        if QTime.ValidateTimeString(t) then mm=val(mid$(t,4,2))
        
        result =  mm
    end function
        
    function Seconds(t as string) as integer
        if t="" then t=time$
        
        ss=-1
        if QTime.ValidateTimeString(t) then ss=val(right$(t,2))
        
        result =  ss
    end function

    function HMSToString(h as integer,m as integer, s as integer, format as integer) as string
        dim s1 as string, f as integer, f1 as integer
        s1=""
        f=0
        f1=0
        if format=12 then
            f=1
            if h>12 then 
                h=h-12
                f1=1
            end if
        end if
        
        if h<10 then s1=s1+"0"
        s1=s1+str$(h)+":"
        
        if m<10 then s1=s1+"0"
        s1=s1+str$(m)+":"
        
        if s<10 then s1=s1+"0"
        s1=s1+str$(s)
        
        if f=1 then
            if f1=0 then
                s1=s1+" am"
            else
                s1=s1+" pm"
            end if
        end if 
        result = s1 
    end function
    
    function now(format as integer) as string
        tt=time$
        hh = QTime.Hours(tt)
        mm = QTime.Minutes(tt)
        ss = QTime.Seconds(tt)
        tt=QTime.HMSToString(hh,mm,ss,format)
        result = tt
    end function
    
    function SerialTime(t as string) as double
        dim s as double
        
        if t="" then t=time$
        s=-1
        if QTime.ValidateTimeString(t) then
            hh=QTime.Hours(t)
            mm=QTime.Minutes(t)
            ss=QTime.Seconds(t)
        
            s=hh+(mm/60)+(ss/3600)
            s=s/24
        end if
        result = s
    end function
    
    function SerialToString(s as double, format as integer) as string
        dim t as double
        
        t=s*24
        hh=int(t)
        t=t-hh
        t=t*60
        mm=int(t)
        t=t-mm
        ss=t*60
        result = QTime.HMSToString(hh,mm,ss,format)
    end function
    
end type

'example
'DIM myDate AS QDate
'Showmessage myDate.now
'Showmessage myDate.month(myDate.now)



