Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

parse-simple-date date string parser

   1  
   2  parse-simple-date: func [
   3  	"Parse a string containing a date value; return a date! value."
   4      date [any-string!]
   5      /def-day def-d [integer! word!] "Default day for mm/yyyy format. Number or 'last."
   6      /local dig sep d m y set-def-day tmp-dt
   7  ][
   8      dig: charset [#"0" - #"9"]
   9      sep: charset " -/."
  10      set [d m y] none
  11      set-def-day: does [
  12          d: any [
  13              all [integer? def-d  def-d]
  14              all [
  15                  'last = def-d
  16                  foreach fld [d m y] [set fld to integer! get fld]
  17                  tmp-dt: subtract make date! reduce [1 m + 1 y] 1
  18                  tmp-dt/day
  19              ]
  20              1
  21          ]
  22      ]
  23      ; assuming mm/dd/yy or mm/yy format
  24      either parse/all date [
  25          [copy m 1 2 dig  sep  copy d 1 2 dig  sep  copy y 1 4 dig]
  26          | [copy m 1 2 dig  sep  copy y 1 4 dig  (set-def-day)]
  27      ][
  28          foreach fld [d m y] [set fld to integer! get fld]
  29          ; add century if necessary; window from 1926-2025
  30          if y < 100 [y: add y pick [1900 2000] y > 25]
  31          ; swap day and month if it makes sense
  32          if all [m > 12  d <= 12] [set [m d] reduce [d m]]
  33          make date! reduce [d m y]
  34      ][none]
  35  ]
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS