Class: Date

Inherits:
Object show all
Defined in:
opal/stdlib/date.rb,
opal/stdlib/json.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = undefined, month = undefined, day = undefined) ⇒ Date

Returns a new instance of Date



21
22
23
# File 'opal/stdlib/date.rb', line 21

def initialize(year = undefined, month = undefined, day = undefined)
  @date = `new Date(year, month - 1, day)`
end

Class Method Details

.parse(string) ⇒ Object



11
12
13
14
# File 'opal/stdlib/date.rb', line 11

def parse(string)
  match = `/^(\d*)-(\d*)-(\d*)/.exec(string)`
  wrap `new Date(parseInt(match[1]), parseInt(match[2]) - 1, parseInt(match[3]))`
end

.todayObject



16
17
18
# File 'opal/stdlib/date.rb', line 16

def today
  wrap `new Date()`
end

.wrap(native) ⇒ Object



5
6
7
8
9
# File 'opal/stdlib/date.rb', line 5

def wrap(native)
  instance = allocate
  `#{instance}.date = #{native}`
  instance
end

Instance Method Details

#+(date) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'opal/stdlib/date.rb', line 41

def +(date)
  %x{
    if (date.$$is_number) {
      var result = #{clone};
      result.date.setDate(#@date.getDate() + date);
      return result;
    }
    else {
      #{raise TypeError};
    }
  }
end

#-(date) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'opal/stdlib/date.rb', line 25

def -(date)
  %x{
    if (date.$$is_number) {
      var result = #{clone};
      result.date.setDate(#@date.getDate() - date);
      return result;
    }
    else if (date.date) {
      return Math.round((#@date - #{date}.date) / (1000 * 60 * 60 * 24));
    }
    else {
      #{raise TypeError};
    }
  }
end

#<(other) ⇒ Object



54
55
56
57
58
59
60
61
# File 'opal/stdlib/date.rb', line 54

def <(other)
  %x{
    var a = #@date, b = #{other}.date;
    a.setHours(0, 0, 0, 0);
    b.setHours(0, 0, 0, 0);
    return a < b;
  }
end

#<<(n) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'opal/stdlib/date.rb', line 129

def <<(n)
  %x{
    if (!n.$$is_number) {
      #{raise TypeError};
    }

    return #{self >> `-n`};
  }
end

#<=(other) ⇒ Object



63
64
65
66
67
68
69
70
# File 'opal/stdlib/date.rb', line 63

def <=(other)
  %x{
    var a = #@date, b = #{other}.date;
    a.setHours(0, 0, 0, 0);
    b.setHours(0, 0, 0, 0);
    return a <= b;
  }
end

#<=>(other) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'opal/stdlib/date.rb', line 90

def <=>(other)
  %x{
    var a = #@date, b = #{other}.date;
    a.setHours(0, 0, 0, 0);
    b.setHours(0, 0, 0, 0);

    if (a < b) {
      return -1;
    }
    else if (a > b) {
      return 1;
    }
    else {
      return 0;
    }
  }
end

#==(other) ⇒ Object Also known as: eql?



108
109
110
111
112
113
# File 'opal/stdlib/date.rb', line 108

def ==(other)
  %x{
    var a = #@date, b = other.date;
    return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate());
  }
end

#>(other) ⇒ Object



72
73
74
75
76
77
78
79
# File 'opal/stdlib/date.rb', line 72

def >(other)
  %x{
    var a = #@date, b = #{other}.date;
    a.setHours(0, 0, 0, 0);
    b.setHours(0, 0, 0, 0);
    return a > b;
  }
end

#>=(other) ⇒ Object



81
82
83
84
85
86
87
88
# File 'opal/stdlib/date.rb', line 81

def >=(other)
  %x{
    var a = #@date, b = #{other}.date;
    a.setHours(0, 0, 0, 0);
    b.setHours(0, 0, 0, 0);
    return a >= b;
  }
end

#>>(n) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'opal/stdlib/date.rb', line 115

def >>(n)
  %x{
    if (!n.$$is_number) {
      #{raise TypeError};
    }

    var result = #{clone}, date = result.date, cur = date.getDate();
    date.setDate(1);
    date.setMonth(date.getMonth() + n);
    date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth())));
    return result;
  }
end

#as_jsonObject



191
192
193
# File 'opal/stdlib/json.rb', line 191

def as_json
  to_s
end

#cloneObject



141
142
143
# File 'opal/stdlib/date.rb', line 141

def clone
  Date.wrap(`new Date(#@date.getTime())`)
end

#dayObject



145
146
147
# File 'opal/stdlib/date.rb', line 145

def day
  `#@date.getDate()`
end

#friday?Boolean

Returns:



149
150
151
# File 'opal/stdlib/date.rb', line 149

def friday?
  wday == 5
end

#monday?Boolean

Returns:



153
154
155
# File 'opal/stdlib/date.rb', line 153

def monday?
  wday == 1
end

#monthObject



157
158
159
# File 'opal/stdlib/date.rb', line 157

def month
  `#@date.getMonth() + 1`
end

#nextObject Also known as: succ



161
162
163
# File 'opal/stdlib/date.rb', line 161

def next
  self + 1
end

#next_day(n = 1) ⇒ Object



165
166
167
# File 'opal/stdlib/date.rb', line 165

def next_day(n=1)
  self + n
end

#next_monthObject



169
170
171
172
173
174
175
176
177
# File 'opal/stdlib/date.rb', line 169

def next_month
  %x{
    var result = #{clone}, date = result.date, cur = date.getDate();
    date.setDate(1);
    date.setMonth(date.getMonth() + 1);
    date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth())));
    return result;
  }
end

#prev_day(n = 1) ⇒ Object



179
180
181
# File 'opal/stdlib/date.rb', line 179

def prev_day(n=1)
  self - n
end

#prev_monthObject



183
184
185
186
187
188
189
190
191
# File 'opal/stdlib/date.rb', line 183

def prev_month
  %x{
    var result = #{clone}, date = result.date, cur = date.getDate();
    date.setDate(1);
    date.setMonth(date.getMonth() - 1);
    date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth())));
    return result;
  }
end

#saturday?Boolean

Returns:



193
194
195
# File 'opal/stdlib/date.rb', line 193

def saturday?
  wday == 6
end

#strftime(format = '') ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'opal/stdlib/date.rb', line 197

def strftime(format = '')
  %x{
    if (format == '') {
      return #{to_s};
    }

    return #@date.$strftime(#{format});
  }
end

#sunday?Boolean

Returns:



209
210
211
# File 'opal/stdlib/date.rb', line 209

def sunday?
  wday == 0
end

#thursday?Boolean

Returns:



213
214
215
# File 'opal/stdlib/date.rb', line 213

def thursday?
  wday == 4
end

#to_jsonObject



187
188
189
# File 'opal/stdlib/json.rb', line 187

def to_json
  to_s.to_json
end

#to_sObject



217
218
219
220
221
222
223
224
# File 'opal/stdlib/date.rb', line 217

def to_s
  %x{
    var d = #@date, year = d.getFullYear(), month = d.getMonth() + 1, day = d.getDate();
    if (month < 10) { month = '0' + month; }
    if (day < 10) { day = '0' + day; }
    return year + '-' + month + '-' + day;
  }
end

#tuesday?Boolean

Returns:



226
227
228
# File 'opal/stdlib/date.rb', line 226

def tuesday?
  wday == 2
end

#wdayObject



230
231
232
# File 'opal/stdlib/date.rb', line 230

def wday
  `#@date.getDay()`
end

#wednesday?Boolean

Returns:



234
235
236
# File 'opal/stdlib/date.rb', line 234

def wednesday?
  wday == 3
end

#yearObject



238
239
240
# File 'opal/stdlib/date.rb', line 238

def year
  `#@date.getFullYear()`
end