XojoScript Date class

Since XojoScript doesn’t have a Date class, I created one. Enjoy.

  Class Date
    Dim Month As Integer
    Dim Day As Integer
    Dim Year As Integer
    Dim Hour As Integer
    Dim Minute As Integer
    Dim Second As Integer
    
    Private Function CurrentSQLDateTime() As String
      dim cmd as string
      dim d as string
      
      //
      // First try it as Windows
      //
      cmd = "DATE /t"
      d = DoShellCommand(cmd).Trim
      if d.Len = 14 then
        d = d.Right(10)
        cmd = "TIME /t"
        dim t as string = DoShellCommand(cmd).Trim
        
        dim parts() as string
        parts.Append t.Left(2)
        parts.Append t.Mid(4, 2)
        parts.Append "00" // Close as we can get, I guess
        
        if t.Right(2) = "PM" then
          parts(0) = str(parts(0).Val + 12)
        end if
        
        d = d.Right(4) + "-" + d.Left(2) + "-" + d.Mid(4, 2) + " " + join(parts, ":")
        
      else
        //
        // Try it on Mac/Linux
        //
        cmd = "date ""+%Y-%m-%d %H:%M:%S"""
        d = DoShellCommand( cmd ).Trim
      end if
      
      return d
    End Function
    
    Sub Constructor()
      dim s as string = CurrentSQLDateTime()
      SQLDateTime = s
    End Sub
    
    Sub SQLDate(Assigns s As String)
      dim parts() as string
      parts = s.Split("-")
      Year = parts(0).Val
      Month = parts(1).Val
      Day = parts(2).Val
    End Sub
    
    Function SQLDate() As String
      dim parts() as string
      parts.Append str(Year)
      parts.Append format(Month, "00")
      parts.Append format(Day, "00")
      
      return join(parts, "-")
    End Function
    
    Sub SQLDateTime (Assigns s As String)
      dim parts() as string
      parts = s.Split(" ")
      SQLDate = parts(0)
      
      parts = parts(1).Split(":")
      Hour = parts(0).Val
      Minute = parts(1).Val
      Second = parts(2).Val
    End Sub
    
    Function SQLDateTime() As String
      dim r as string = SQLDate()
      dim time as string = format(Hour, "00") + ":" + format(Minute, "00") + ":" + format(Second, "00")
      r = r + " " + time
      return r
    End Function
  End Class
1 Like