time-parser.monkey2 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Namespace std.time
  2. Using std.stringio
  3. #rem monkeydoc The TimeParser class.
  4. A very simple parser for use by the [[Time.Parse]] function.
  5. #end
  6. Class TimeParser
  7. Field day:Int
  8. Field month:Int
  9. Field year:Int
  10. Field hours:Int
  11. Field minutes:Int
  12. Field seconds:Int
  13. Method Parse:Bool( str:String )
  14. 'Thu 21 Jun 2018 10:21:2
  15. _str=str
  16. _len=_str.Length
  17. _pos=0
  18. _err=False
  19. Bump()
  20. 'Parse day...
  21. Local dname:=CParseString() 'ignore optional day name
  22. day=ParseInt()
  23. 'Parse month...
  24. If _toke And IsDigit( _toke[0] )
  25. Self.month=ParseInt()-1
  26. Else
  27. Local mname:=ParseString()
  28. For Local month:=0 Until 12
  29. If Not mname.ToLower().StartsWith( _months[month] ) Continue
  30. Self.month=month
  31. Exit
  32. Next
  33. Endif
  34. 'Parse year...
  35. Self.year=ParseInt()
  36. If Not Toke Return Not _err
  37. 'Parse time...
  38. Self.hours=ParseInt()
  39. Self.minutes=ParseInt()
  40. Self.seconds=ParseInt()
  41. Return Not _err
  42. End
  43. Private
  44. Const _months:=New String[]( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )
  45. Field _str:String
  46. Field _len:int
  47. Field _pos:Int
  48. Field _err:Bool
  49. Field _toke:String
  50. Property Toke:String()
  51. Return _toke
  52. End
  53. Method Bump:String()
  54. While _pos<_len
  55. Local chr:=_str[_pos]
  56. If chr>32 And chr<>58 And chr<>47 Exit ' : and / are space...
  57. _pos+=1
  58. Wend
  59. If _pos=_len
  60. _toke=""
  61. Return ""
  62. Endif
  63. Local pos:=_pos
  64. Local chr:=_str[_pos]
  65. _pos+=1
  66. If IsAlpha( chr )
  67. While _pos<_len And IsAlpha( _str[_pos] )
  68. _pos+=1
  69. Wend
  70. Else If IsDigit( chr )
  71. While _pos<_len And IsDigit( _str[_pos] )
  72. _pos+=1
  73. Wend
  74. Endif
  75. _toke=_str.Slice( pos,_pos )
  76. Return _toke
  77. End
  78. Method ParseToke( str:String )
  79. If _err Return
  80. If _toke<>str
  81. DebugStop()
  82. _err=True
  83. Return
  84. Endif
  85. Bump()
  86. End
  87. Method ParseString:String()
  88. If _err Return ""
  89. If Not _toke Or Not IsAlpha( _toke[0] )
  90. DebugStop()
  91. _err=True
  92. Return ""
  93. Endif
  94. Local str:=_toke
  95. Bump()
  96. Return str
  97. End
  98. Method CParseString:String()
  99. If _err Or Not _toke Or Not IsAlpha( _toke[0] ) Return ""
  100. Local str:=_toke
  101. Bump()
  102. Return str
  103. End
  104. Method ParseInt:Int()
  105. If _err Return 0
  106. If Not _toke Or Not IsDigit( _toke[0] )
  107. DebugStop()
  108. _err=True
  109. Return 0
  110. Endif
  111. Local val:=Int( _toke )
  112. Bump()
  113. Return val
  114. End
  115. End