~pmikkelsen/dyalog-competition-2022

d7f28d8ac15c41e8323cb14aa9c1230ef79526fc — Peter Mikkelsen 2 years ago f334720
Update date again
1 files changed, 45 insertions(+), 3 deletions(-)

M date.apln
M date.apln => date.apln +45 -3
@@ 1,9 1,35 @@
:Namespace date
      test1←{
          string←'Thu, 17-Feb-2022 15:10:07'
          pattern←'Ddd, DD-Mmm-YYYY hh:mm:ss'
          newstring←⊃pattern(1200⌶)pattern DDN string
          ⎕←string
          ⎕←newstring
          ⎕←'Same? ',(string≡newstring)
      }
      test2←{
          string←'02/17/22 3P:39'
          pattern←'MM/DD/YY tP:mm'
          newstring←⊃pattern(1200⌶)pattern DDN string
          ⎕←string
          ⎕←newstring
          ⎕←'Same? ',(string≡newstring)
      }
      test3←{
          string←'Thursday'
          pattern←'Dddd'
          newstring←⊃pattern(1200⌶)pattern DDN string
          ⎕←string
          ⎕←newstring
          ⎕←'Same? ',(string≡newstring)
      }
      DDN←{
          info←13⍴⊂⍬ ⍝ The 13 fields from the variant table, without fractional seconds
          info←⊃parse⍣≡info ⍺ ⍵ ⍝ Parse all we can
          info←⊃parse⍣≡info ⍺ ⍵ ⍝ Continue parsing until there is no more to parse
     
          ⍝ At this point, we may not have all information, so we need to fix that
          info←fixSecond fixMinute fixHour fixDay fixMonth fixYear info
          info←fixYear fixMonth fixDay fixHour fixMinute fixSecond info
          ⎕←info
          1 ⎕DT⊂,↑6↑info ⍝ convert the year, month, day, hour, minute, second to DDN
      }
      parse←{


@@ 76,26 102,42 @@
      ⍝ Otherwise they use the most precise information available to set the field.
      ⍝ If no information is available, they just pick a valid value.
      fixYear←{
          ⍝ No information can decide the year, so just use 2022
          ⍵[1]≢⊂⍬:⍵
          (2022@1)⍵
      }
      fixMonth←{
          ⍝ Week information can decide the month
          ⍝ Day of year can decide the month
          ⍝ Day of month can decide month (limit the cases)
          ⍝ Ordinal indicator for month can decide month
          ⍵[2]≢⊂⍬:⍵
          (1@2)⍵
      }
      fixDay←{
          ⍝ The following fields can influence the day:
          ⍝ ⍵[7]: day of week
          ⍝ ⍵[8]: ISO week number
          ⍝ ⍵[9]: Year of iso week number
          ⍝ ⍵[10]: Day of year
          ⍝ ⍵[11]: Ordinal indicator for day of month
      ⍝ PERHAPS: do I need to calculate Y/M/D together, as they all depend on each other?
          ⍵[3]≢⊂⍬:⍵
          (1@3)⍵
      }
      fixHour←{
          ⍵[4]≢⊂⍬:⍵
          (0@4)⍵
          ⍵[12]≢⊂⍬:((⍵[12]+12×⍵[13]='p')@4)⍵ ⍝ If whe have 12 hour clock
          ⍵[13]≡'p':(12@4)⍵ ⍝ If we have AM/PM, but not clock
          (0@4)⍵ ⍝ Default value of 0
      }
      fixMinute←{
          ⍝ No information describes the minutes, so pick 0
          ⍵[5]≢⊂⍬:⍵
          (0@5)⍵
      }
      fixSecond←{
          ⍝ No information describes the seconds, so pick 0
          ⍵[6]≢⊂⍬:⍵
          (0@6)⍵
      }