Class: Date

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

Defined Under Namespace

Classes: Infinity

Constant Summary

JULIAN =
Infinity.new
GREGORIAN =
-Infinity.new
ITALY =

1582-10-15

2299161
ENGLAND =

1752-09-14

2361222
MONTHNAMES =
[nil] + %w(January February March April May June July August September October November December)
DAYNAMES =
%w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
ABBR_DAYNAMES =
%w(Sun Mon Tue Wed Thu Fri Sat)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = -4712,, month = 1, day = 1, start = ITALY) ⇒ Date

Returns a new instance of Date



101
102
103
# File 'opal/stdlib/date.rb', line 101

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

Class Method Details

.parse(string) ⇒ Object



91
92
93
94
# File 'opal/stdlib/date.rb', line 91

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

.todayObject



96
97
98
# File 'opal/stdlib/date.rb', line 96

def today
  wrap `new Date()`
end

.wrap(native) ⇒ Object



85
86
87
88
89
# File 'opal/stdlib/date.rb', line 85

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

Instance Method Details

#+(date) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'opal/stdlib/date.rb', line 121

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

#-(date) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'opal/stdlib/date.rb', line 105

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



134
135
136
137
138
139
140
141
# File 'opal/stdlib/date.rb', line 134

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



209
210
211
212
213
214
215
216
217
# File 'opal/stdlib/date.rb', line 209

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

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

#<=(other) ⇒ Object



143
144
145
146
147
148
149
150
# File 'opal/stdlib/date.rb', line 143

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'opal/stdlib/date.rb', line 170

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?



188
189
190
191
192
193
# File 'opal/stdlib/date.rb', line 188

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



152
153
154
155
156
157
158
159
# File 'opal/stdlib/date.rb', line 152

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



161
162
163
164
165
166
167
168
# File 'opal/stdlib/date.rb', line 161

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



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

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



221
222
223
# File 'opal/stdlib/date.rb', line 221

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

#dayObject



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

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

#friday?Boolean

Returns:



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

def friday?
  wday == 5
end

#jdObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'opal/stdlib/date.rb', line 233

def jd
  %x{
  //Adapted from http://www.physics.sfasu.edu/astro/javascript/julianday.html

  var mm = #@date.getMonth() + 1,
      dd = #@date.getDate(),
      yy = #@date.getFullYear(),
      hr = 12, mn = 0, sc = 0,
      ggg, s, a, j1, jd;

  hr = hr + (mn / 60) + (sc/3600);

  ggg = 1;
  if (yy <= 1585) {
    ggg = 0;
  }

  jd = -1 * Math.floor(7 * (Math.floor((mm + 9) / 12) + yy) / 4);

  s = 1;
  if ((mm - 9) < 0) {
    s =- 1;
  }

  a = Math.abs(mm - 9);
  j1 = Math.floor(yy + s * Math.floor(a / 7));
  j1 = -1 * Math.floor((Math.floor(j1 / 100) + 1) * 3 / 4);

  jd = jd + Math.floor(275 * mm / 9) + dd + (ggg * j1);
  jd = jd + 1721027 + 2 * ggg + 367 * yy - 0.5;
  jd = jd + (hr / 24);

  return jd;
  }
end

#julian?Boolean

Returns:



269
270
271
# File 'opal/stdlib/date.rb', line 269

def julian?
  `#@date < new Date(1582, 10 - 1, 15, 12)`
end

#monday?Boolean

Returns:



273
274
275
# File 'opal/stdlib/date.rb', line 273

def monday?
  wday == 1
end

#monthObject



277
278
279
# File 'opal/stdlib/date.rb', line 277

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

#nextObject Also known as: succ



281
282
283
# File 'opal/stdlib/date.rb', line 281

def next
  self + 1
end

#next_day(n = 1) ⇒ Object



285
286
287
# File 'opal/stdlib/date.rb', line 285

def next_day(n=1)
  self + n
end

#next_monthObject



289
290
291
292
293
294
295
296
297
# File 'opal/stdlib/date.rb', line 289

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



299
300
301
# File 'opal/stdlib/date.rb', line 299

def prev_day(n=1)
  self - n
end

#prev_monthObject



303
304
305
306
307
308
309
310
311
# File 'opal/stdlib/date.rb', line 303

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:



313
314
315
# File 'opal/stdlib/date.rb', line 313

def saturday?
  wday == 6
end

#strftime(format = '') ⇒ Object



317
318
319
320
321
322
323
324
325
# File 'opal/stdlib/date.rb', line 317

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

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

#sunday?Boolean

Returns:



329
330
331
# File 'opal/stdlib/date.rb', line 329

def sunday?
  wday == 0
end

#thursday?Boolean

Returns:



333
334
335
# File 'opal/stdlib/date.rb', line 333

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



337
338
339
340
341
342
343
344
# File 'opal/stdlib/date.rb', line 337

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:



346
347
348
# File 'opal/stdlib/date.rb', line 346

def tuesday?
  wday == 2
end

#wdayObject



350
351
352
# File 'opal/stdlib/date.rb', line 350

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

#wednesday?Boolean

Returns:



354
355
356
# File 'opal/stdlib/date.rb', line 354

def wednesday?
  wday == 3
end

#yearObject



358
359
360
# File 'opal/stdlib/date.rb', line 358

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