summaryrefslogtreecommitdiffstats
path: root/lib/parsedate.rb
blob: 7fc75cf0c2cfff34879880c07272938dc22fff58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# parsedate3.rb: Written by Tadayoshi Funaba 2000, 2001
# $Id: parsedate3.rb,v 1.3 2001-01-18 12:09:47+09 tadf Exp $

module ParseDate

  MONTHS = {
    'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
    'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
    'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12
  }
  MONTHPAT = MONTHS.keys.join('|')

  DAYS = {
    'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3,
    'thu' => 4, 'fri' => 5, 'sat' => 6
  }
  DAYPAT = DAYS.keys.join('|')

  def parsedate(date, cyear=false)
    date = date.dup

    # day
    if date.sub!(/(#{DAYPAT})\S*/ino, ' ')
      wday = DAYS[$1.downcase]
    end

    # time
    if date.sub!(
		 /(\d+):(\d+)(?::(\d+))?
		  (?:
		    \s*
		    ([ap])\.?m\.?
		    \b
		  )?
		  (?:
		    \s*
		    (
		      [a-z]+(?:\s+dst)?
		    |
		      [-+]\d+(?::?\d+)
		    )
		    \b
		  )?
		 /inox,
		 ' ')
      hour = $1.to_i
      min = $2.to_i
      sec = $3.to_i if $3
      if $4
	hour %= 12
	if $4.downcase == 'p'
	  hour += 12
	end
      end
      zone = $5
    end

    # eu
    if date.sub!(
		 /(\d+)\S*
		  \s+
		  (#{MONTHPAT})\S*
		  (?:
		    \s+
		    (\d+)
		  )?
		 /inox,
		 ' ')
      mday = $1.to_i
      mon = MONTHS[$2.downcase]
      year = $3.to_i if $3

    # us
    elsif date.sub!(
		    /(#{MONTHPAT})\S*
		     \s+
		     (\d+)\S*
		     (?:
		       \s+
		       (\d+)
		     )?
		    /inox,
		    ' ')
      mon = MONTHS[$1.downcase]
      mday = $2.to_i
      year = $3.to_i if $3

    # iso
    elsif date.sub!(/(\d+)-(\d+)-(\d+)/no, ' ')
      year = $1.to_i
      mon = $2.to_i
      mday = $3.to_i
      mday, mon, year = year, mon, mday if $3.size >= 4

    # jis
    elsif date.sub!(/([MTSH])(\d+)\.(\d+)\.(\d+)/no, ' ')
      e = { 'M'=>1867,
	    'T'=>1911,
	    'S'=>1925,
	    'H'=>1988
	  }[$1]
      year, mon, mday = $2.to_i + e, $3.to_i, $4.to_i

    # vms
    elsif date.sub!(/(\d+)-(#{MONTHPAT})\S*-(\d+)/ino, ' ')
      mday = $1.to_i
      mon = MONTHS[$2.downcase]
      year = $3.to_i
      year, mon, mday = mday, mon, year if $1.size >= 4

    # sla
    elsif date.sub!(%r|(\d+)/(\d+)(?:/(\d+))?|no, ' ')
      mon = $1.to_i
      mday = $2.to_i
      year = $3.to_i if $3
      year, mon, mday = mon, mday, year if $1.size >= 4

    # ddd
    elsif date.sub!(
		    /(\d{4,14})
		     (?:
		       \s*
		       T?
		       \s*
		       (\d{2,6})
		     )?
		     (?:
		       \s*
		       (
			 Z
		       |
			 [-+]\d{2,4}
		       )
		       \b
		     )?
		    /nox,
		    ' ')
      case $1.size
      when 4
	mon  = $1[ 0, 2].to_i
	mday = $1[ 2, 2].to_i
      when 6
	year = $1[ 0, 2].to_i
	mon  = $1[ 2, 2].to_i
	mday = $1[ 4, 2].to_i
      when 8, 10, 12, 14
	year = $1[ 0, 4].to_i
	mon  = $1[ 4, 2].to_i
	mday = $1[ 6, 2].to_i
	hour = $1[ 8, 2].to_i if $1.size >= 10
	min  = $1[10, 2].to_i if $1.size >= 12
	sec  = $1[12, 2].to_i if $1.size >= 14
      end
      if $2
	case $2.size
	when 2, 4, 6
	  hour = $2[ 0, 2].to_i
	  min  = $2[ 2, 2].to_i if $2.size >= 4
	  sec  = $2[ 4, 2].to_i if $2.size >= 6
	end
      end
      zone = $3
    end

    if cyear and year
      if year < 100
	if year >= 69
	  year += 1900
	else
	  year += 2000
	end
      end
    end

    return year, mon, mday, hour, min, sec, zone, wday

  end

  module_function :parsedate

end