Script Library: 1238 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Archive version of: to-iso-8601-date.r ... version: 1 ... peterwood 24-Jan-2005

Amendment note: new script || Publicly available? Yes

REBOL [
    Title: "to-iso8601-date"
    Version: 1.0.0
    Date: 24-01-2005
    Author: "Peter WA Wood"
    Copyright: "Peter WA Wood"
    File: %to-iso-8601-date.r
    Purpose: {A function which converts a Rebol date! to a string which complies
              with ISO 8601.
              
              If the time is not provided, a default of 00:00 is used.
              If the seconds are not set, 00 is used.
              If the date has been set 
              (NB The zone defaults to 00:00 in a Rebol date)
             }              
    Usage: { example: to-iso8601-date rebol-date }
    Library: [
        level: 'beginner
        type: 'function
        domain: [web ]
        platform: 'all
        tested-under: [ core 2.5.6.2.4 "Mac OS X 10.2.8"
                        core 2.5.6.3.1 "Windows XP Professional"]
        support: none
        license: cc-by 
	   {see htt4p://www.rebol.org/cgi-bin/cgiwrap/rebol/license-help.r}
    ]
]

to-iso8601-date: func [
     {converts a date! to a string which complies with the ISO 8602 standard.
      If the time is not set on the input date, a default of 00:00 is used.
      If the seconds of the time is not set, 00 is used.}
     
    the-date [date!]
        "The date to be reformatted"
    /local
        iso-date
            "The string to be returned"
][ 

    iso-date: copy ""           

    either the-date/time [ 
                                                    ; the date has a time
        insert iso-date rejoin [
            " "
            either the-date/time/hour > 9           ; insert leading zero if
                [the-date/time]                     ;  needed
                [join "0" [the-date/time]] 
            either the-date/time/second > 0         ; Rebol only returns 
                [""]                                ;  seconds if non-zero
                [":00"]
           ;; Date zone need to remove ":" 
            either the-date/zone = 0:00 [
                "Z"                                 ; UTC
            ][
                rejoin [
                    either the-date/zone/hour > 0   ; + or - UTC
                        ["+"]
                        ["-"]
                    either  (absolute the-date/zone/hour) < 10
                        [join "0" [absolute the-date/zone/hour]]
                        [absolute the-date/zone/hour]
                    either the-date/zone/minute < 10
                        [join "0" [the-date/zone/minute]]
                        [the-date/zone/minute]
                ]
            ]
        ] ; end insert  
    ][
        iso-date: " 00:00:00Z"                     ; the date has no time
    ] 
     
    insert iso-date rejoin [
        join copy/part "000" (4 - length? to-string the-date/year)
            [the-date/year]
        "-"
        either the-date/month > 9
            [the-date/month]
            [join "0" [the-date/month]]
        "-"
        either the-date/day > 9
            [the-date/day]
            [join "0" [the-date/day]]
     ] ; end insert
   
    return head iso-date
    
] ; end to-fussy-idate

;; History
;; 
;; 1.0.0 24-Jan-2005    Initial release (no optimisation)
;;