Current date/time stamp in RFC 822 / 1123 format?

  1. last year

    Tim D

    3 Sep 2017 Pre-Release Testers, Xojo Pro, XDC Speakers Richmond, VA

    Does anyone have or know of an easy way to get the current date/time stamp in RFC 822 / 1123 format?

    For example:
    Sun, 06 Nov 1994 08:49:37 GMT

    Details on the format can be found here (in section "3.3.1 Full Date"):
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

    Thanks,

    Tim

  2. scott b

    3 Sep 2017 Pre-Release Testers, Xojo Pro local coffee shop

    @Tim D Does anyone have or know of an easy way to get the current date/time stamp in RFC 822 / 1123 format?

    For example:
    Sun, 06 Nov 1994 08:49:37 GMT

    Details on the format can be found here (in section "3.3.1 Full Date"):
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

    Thanks,

    Tim

    write a method that is an extension of date or xojo.data.date and returns the value.
    I Don’t know of a way other than that.

  3. Norman P

    3 Sep 2017 Xojo Inc North of 50
    Edited last year

    something really lazy like

    Public Function ToRFC1123(extends dt as Xojo.Core.Date) as Text
      ' Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
      
      ' HTTP-date    = rfc1123-date | rfc850-date | asctime-date
      ' rfc1123-date = wkday "," SP date1 SP time SP "GMT"
      ' rfc850-date  = weekday "," SP date2 SP time SP "GMT"
      ' asctime-date = wkday SP date3 SP time SP 4DIGIT
      ' date1        = 2DIGIT SP month SP 4DIGIT
      ' ; day month year (e.g., 02 Jun 1982)
      ' date2        = 2DIGIT "-" month "-" 2DIGIT
      ' ; day-month-year (e.g., 02-Jun-82)
      ' date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))
      ' ; month day (e.g., Jun  2)
      ' time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT
      ' ; 00:00:00 - 23:59:59
      ' wkday        = "Mon" | "Tue" | "Wed"
      ' | "Thu" | "Fri" | "Sat" | "Sun"
      ' weekday      = "Monday" | "Tuesday" | "Wednesday"
      ' | "Thursday" | "Friday" | "Saturday" | "Sunday"
      ' month        = "Jan" | "Feb" | "Mar" | "Apr"
      ' | "May" | "Jun" | "Jul" | "Aug"
      ' | "Sep" | "Oct" | "Nov" | "Dec"
      
      Dim tmp As Text
      
      // clone the date since we do not want to alter it
      Dim tmpDt As Xojo.Core.Date = New Xojo.Core.Date(dt.SecondsFrom1970, New Xojo.Core.TimeZone(0))
      
      Select Case tmpDt.DayOfWeek
      Case 1
        tmp = tmp + "Sun"
      Case 2
        tmp = tmp + "Mon"
      Case 3
        tmp = tmp + "Tue"
      Case 4
        tmp = tmp + "Wed"
      Case 5
        tmp = tmp + "Thu"
      Case 6
        tmp = tmp + "Fri"
      Case 7
        tmp = tmp + "Sat"
      End Select
      
      tmp = tmp + ", "
      
      tmp = tmp + tmpDt.Day.ToText(Xojo.Core.Locale.Raw, "#0")
      tmp = tmp + " "
      
      Select Case tmpdt.Month
      Case 1
        tmp = tmp + "Jan" 
      Case 2
        tmp = tmp + "Feb" 
      Case 3
        tmp = tmp + "Mar"
      Case 4
        tmp = tmp + "Apr"
      Case 5
        tmp = tmp + "May" 
      Case 6
        tmp = tmp + "Jun" 
      Case 7
        tmp = tmp + "Jul" 
      Case 8
        tmp = tmp + "Aug"
      Case 9
        tmp = tmp + "Sep" 
      Case 10
        tmp = tmp + "Oct"
       Case 11
        tmp = tmp + "Nov" 
      Case 12
        tmp = tmp + "Dec"
      End Select
      
      tmp = tmp + " "
      
      tmp = tmp + tmpDt.Year.ToText(Xojo.Core.Locale.Raw, "####")
      tmp = tmp + " "
      
      tmp = tmp + tmpDt.Hour.ToText(Xojo.Core.Locale.Raw, "00")
      tmp = tmp + ":"
      
      tmp = tmp + tmpDt.Minute.ToText(Xojo.Core.Locale.Raw, "00")
      tmp = tmp + ":"
      
      tmp = tmp + tmpDt.Second.ToText(Xojo.Core.Locale.Raw, "00")
      tmp = tmp + " "
      
      tmp = tmp + "GMT"
      
      Return tmp
      
    End Function

    use this like

    Dim dt As Xojo.Core.Date = Xojo.core.Date.Now
    Dim t As Text 
    
    t = dt.ToRFC1123
  4. Norman P

    3 Sep 2017 Xojo Inc North of 50

    and the counterpart

    Public Function FromRFC1123(extends dt as Xojo.Core.Date, input as text) as Xojo.Core.Date
      'Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
      'Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
      'Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
      
      ' All HTTP date/time stamps MUST be represented In Greenwich Mean Time (GMT), without exception
      
      ' HTTP-date    = rfc1123-date | rfc850-date | asctime-date
      ' rfc1123-date = wkday "," SP date1 SP time SP "GMT"
      ' rfc850-date  = weekday "," SP date2 SP time SP "GMT"
      ' asctime-date = wkday SP date3 SP time SP 4DIGIT
      ' date1        = 2DIGIT SP month SP 4DIGIT
      ' ; day month year (e.g., 02 Jun 1982)
      ' date2        = 2DIGIT "-" month "-" 2DIGIT
      ' ; day-month-year (e.g., 02-Jun-82)
      ' date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))
      ' ; month day (e.g., Jun  2)
      ' time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT
      ' ; 00:00:00 - 23:59:59
      ' wkday        = "Mon" | "Tue" | "Wed"
      ' | "Thu" | "Fri" | "Sat" | "Sun"
      ' weekday      = "Monday" | "Tuesday" | "Wednesday"
      ' | "Thursday" | "Friday" | "Saturday" | "Sunday"
      ' month        = "Jan" | "Feb" | "Mar" | "Apr"
      ' | "May" | "Jun" | "Jul" | "Aug"
      ' | "Sep" | "Oct" | "Nov" | "Dec"
      
      Dim tmp As Text = Input.Trim
      
      tmp = tmp.ReplaceAll(", ", " ")
      
      Dim bits() As Text
      
      bits = tmp.Split(" ")
      
      Dim yearStr As Text
      Dim monthName As Text
      Dim dayName As Text
      Dim dayStr As Text
      
      Dim year As Integer
      Dim month As Integer
      Dim day As Integer
      
      Dim timeStr As Text
      Dim hrStr As Text
      Dim minStr As Text
      Dim secStr As Text
      
      Dim hour As Integer
      Dim minutes As Integer
      Dim seconds As Integer
      
      Dim tz As Text
      
      For i As Integer = 0 To bits.ubound
        
        Select Case bits(i)
          
        Case "Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday", "Wed", "Wednesday", "Thu", "Thursday", "Fri", "Friday", "Sat", "Saturday"
          dayName = bits(i)
          
        Case "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
          monthName = bits(i)
          
        Else
          If bits(i).IndexOf(":") >= 0 Then
            timestr = bits(i)
            Dim parts() As Text = timestr.Split(":")
            hrStr = parts(0)
            minStr = parts(1)
            secStr = parts(2)
            
          Elseif bits(i).IndexOf("-") >= 0 Then
            // style 2 date with - between day mon year ?
            Dim parts() As Text = bits(i).Split("-")
            dayStr = parts(0)
            monthName = parts(1)
            yearStr = parts(2)
            
          Elseif dayStr = "" Then
            dayStr = bits(i)
            
          Elseif yearStr = "" Then
            yearStr = bits(i)
          Else 
            tz = bits(i)
          End If
          
        End Select
        
      Next
      
      If yearStr.Length <= 2 then
        If Integer.FromText(yearStr) < 50 Then
          yearStr = "20" + yearStr
        Else
          yearStr = "19" + yearStr
        End If
      End If
      
      year = Integer.FromText(yearStr)
      
      Select Case monthName
      Case "Jan" 
        month = 1
      Case "Feb" 
        month = 2
      Case "Mar"
        month = 3
      Case "Apr"
        month = 4
      Case "May" 
        month = 5
      Case "Jun" 
        month = 6
      Case "Jul" 
        month = 7
      Case "Aug"
        month = 8
      Case "Sep" 
        month = 9
      Case "Oct"
        month = 10
      Case "Nov" 
        month = 11
      Case "Dec"
        month = 12
      End Select
      
      day = Integer.FromText(dayStr)
      
      hour = Integer.FromText(hrStr)
      minutes = Integer.FromText(minStr)
      seconds = Integer.FromText(secStr)
      
      
      Dim gmtTZ As New Xojo.Core.TimeZone(0)
      
      Dim tmpDt As New Xojo.Core.Date(year, month, day, hour, minutes, seconds, gmtTZ)
      
      Return tmpDt
    End Function

    which you can use like

    dim dt as Xojo.Core.Date = Xojo.Core.Date.Now
    
    Dim dt2 As Xojo.Core.Date = dt.FromRFC1123("Sun, 06 Nov 1994 08:49:37 GMT")
    Dim dt3 As Xojo.Core.Date = dt.FromRFC1123("Sunday, 06-Nov-94 08:49:37 GMT")
    Dim dt4 As Xojo.Core.Date = dt.FromRFC1123("Sun Nov  6 08:49:37 1994")

    modify as you see fit

  5. Tim D

    4 Sep 2017 Pre-Release Testers, Xojo Pro, XDC Speakers Richmond, VA

    Awesome! Thanks @Norman P, you've saved me a lot of time.

or Sign Up to reply!