[フレーム]

Class: Date

Inherits:
Object show all
Includes:
Comparable
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日

2_299_161
ENGLAND =

1752年09月14日

2_361_222
MONTHNAMES =
[nil] + %w[JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember]
ABBR_MONTHNAMES =
%w[janfebmaraprmayjunjulaugsepoctnovdec]
DAYNAMES =
%w[SundayMondayTuesdayWednesdayThursdayFridaySaturday]
ABBR_DAYNAMES =
%w[SunMonTueWedThuFriSat]

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

349
350
351
352
353
354
355
356
357
358
# File 'opal/stdlib/date.rb', line 349
def initialize(year = -4712, month = 1, day = 1, start = ITALY )
 %x{
 // Because of Gregorian reform calendar goes from 1582年10月04日 to 1582年10月15日.
 // All days in between end up as 4 october.
 if (year === 1582 && month === 10 && day > 4 && day < 15) {
 day = 4;
 }
 }
 @date = `new Date(year, month - 1, day)`
end

Class Method Details

.gregorian_leap?(year) ⇒ Boolean

Returns:

344
345
346
# File 'opal/stdlib/date.rb', line 344
def gregorian_leap?(year)
 `(new Date(#{year}, 1, 29).getMonth()-1) === 0`
end

.parse(string, comp = true) ⇒ Object

Raises:

  • (ArgumentError)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'opal/stdlib/date.rb', line 94
def parse(string, comp = true)
 %x{
 var current_date = new Date();
 var current_day = current_date.getDate(),
 current_month = current_date.getMonth(),
 current_year = current_date.getFullYear(),
 current_wday = current_date.getDay(),
 full_month_name_regexp = #{MONTHNAMES .compact.join('|')};
 function match1(match) { return match[1]; }
 function match2(match) { return match[2]; }
 function match3(match) { return match[3]; }
 function match4(match) { return match[4]; }
 // Converts passed short year (0..99)
 // to a 4-digits year in the range (1969..2068)
 function fromShortYear(fn) {
 return function(match) {
 var short_year = fn(match);
 if (short_year >= 69) {
 short_year += 1900;
 } else {
 short_year += 2000;
 }
 return short_year;
 }
 }
 // Converts month abbr (nov) to a month number
 function fromMonthAbbr(fn) {
 return function(match) {
 var abbr = fn(match).toLowerCase();
 return #{ABBR_MONTHNAMES }.indexOf(abbr) + 1;
 }
 }
 function toInt(fn) {
 return function(match) {
 var value = fn(match);
 return parseInt(value, 10);
 }
 }
 // Depending on the 'comp' value appends 20xx to a passed year
 function to2000(fn) {
 return function(match) {
 var value = fn(match);
 if (comp) {
 return value + 2000;
 } else {
 return value;
 }
 }
 }
 // Converts passed week day name to a day number
 function fromDayName(fn) {
 return function(match) {
 var dayname = fn(match),
 wday = #{DAYNAMES .map(&:downcase)}.indexOf(#{`dayname`.downcase});
 return current_day - current_wday + wday;
 }
 }
 // Converts passed month name to a month number
 function fromFullMonthName(fn) {
 return function(match) {
 var month_name = fn(match);
 return #{MONTHNAMES .compact.map(&:downcase)}.indexOf(#{`month_name`.downcase}) + 1;
 }
 }
 var rules = [
 {
 // DD as month day number
 regexp: /^(\d{2})$/,
 year: current_year,
 month: current_month,
 day: toInt(match1)
 },
 {
 // DDD as year day number
 regexp: /^(\d{3})$/,
 year: current_year,
 month: 0,
 day: toInt(match1)
 },
 {
 // MMDD as month and day
 regexp: /^(\d{2})(\d{2})$/,
 year: current_year,
 month: toInt(match1),
 day: toInt(match2)
 },
 {
 // YYDDD as year and day number in 1969--2068
 regexp: /^(\d{2})(\d{3})$/,
 year: fromShortYear(toInt(match1)),
 month: 0,
 day: toInt(match2)
 },
 {
 // YYMMDD as year, month and day in 1969--2068
 regexp: /^(\d{2})(\d{2})(\d{2})$/,
 year: fromShortYear(toInt(match1)),
 month: toInt(match2),
 day: toInt(match3)
 },
 {
 // YYYYDDD as year and day number
 regexp: /^(\d{4})(\d{3})$/,
 year: toInt(match1),
 month: 0,
 day: toInt(match2)
 },
 {
 // YYYYMMDD as year, month and day number
 regexp: /^(\d{4})(\d{2})(\d{2})$/,
 year: toInt(match1),
 month: toInt(match2),
 day: toInt(match3)
 },
 {
 // mmm YYYY
 regexp: /^([a-z]{3})[\s\.\/\-](\d{3,4})$/,
 year: toInt(match2),
 month: fromMonthAbbr(match1),
 day: 1
 },
 {
 // DD mmm YYYY
 regexp: /^(\d{1,2})[\s\.\/\-]([a-z]{3})[\s\.\/\-](\d{3,4})$/i,
 year: toInt(match3),
 month: fromMonthAbbr(match2),
 day: toInt(match1)
 },
 {
 // mmm DD YYYY
 regexp: /^([a-z]{3})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{3,4})$/i,
 year: toInt(match3),
 month: fromMonthAbbr(match1),
 day: toInt(match2)
 },
 {
 // YYYY mmm DD
 regexp: /^(\d{3,4})[\s\.\/\-]([a-z]{3})[\s\.\/\-](\d{1,2})$/i,
 year: toInt(match1),
 month: fromMonthAbbr(match2),
 day: toInt(match3)
 },
 {
 // YYYY-MM-DD YYYY/MM/DD YYYY.MM.DD
 regexp: /^(\-?\d{3,4})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{1,2})$/,
 year: toInt(match1),
 month: toInt(match2),
 day: toInt(match3)
 },
 {
 // YY-MM-DD
 regexp: /^(\d{2})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{1,2})$/,
 year: to2000(toInt(match1)),
 month: toInt(match2),
 day: toInt(match3)
 },
 {
 // DD-MM-YYYY
 regexp: /^(\d{1,2})[\s\.\/\-](\d{1,2})[\s\.\/\-](\-?\d{3,4})$/,
 year: toInt(match3),
 month: toInt(match2),
 day: toInt(match1)
 },
 {
 // ddd
 regexp: new RegExp("^(" + #{DAYNAMES .join('|')} + ")$", 'i'),
 year: current_year,
 month: current_month,
 day: fromDayName(match1)
 },
 {
 // monthname daynumber YYYY
 regexp: new RegExp("^(" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)[\\s\\.\\/\\-](\\-?\\d{3,4})$", "i"),
 year: toInt(match4),
 month: fromFullMonthName(match1),
 day: toInt(match2)
 },
 {
 // monthname daynumber
 regexp: new RegExp("^(" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)", "i"),
 year: current_year,
 month: fromFullMonthName(match1),
 day: toInt(match2)
 },
 {
 // daynumber monthname YYYY
 regexp: new RegExp("^(\\d{1,2})(th|nd|rd)[\\s\\.\\/\\-](" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\-?\\d{3,4})$", "i"),
 year: toInt(match4),
 month: fromFullMonthName(match3),
 day: toInt(match1)
 },
 {
 // YYYY monthname daynumber
 regexp: new RegExp("^(\\-?\\d{3,4})[\\s\\.\\/\\-](" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)$", "i"),
 year: toInt(match1),
 month: fromFullMonthName(match2),
 day: toInt(match3)
 }
 ]
 var rule, i, match;
 for (i = 0; i < rules.length; i++) {
 rule = rules[i];
 match = rule.regexp.exec(string);
 if (match) {
 var year = rule.year;
 if (typeof(year) === 'function') {
 year = year(match);
 }
 var month = rule.month;
 if (typeof(month) === 'function') {
 month = month(match) - 1
 }
 var day = rule.day;
 if (typeof(day) === 'function') {
 day = day(match);
 }
 var result = new Date(year, month, day);
 // an edge case, JS can't handle 'new Date(1)', minimal year is 1970
 if (year >= 0 && year <= 1970) {
 result.setFullYear(year);
 }
 return #{wrap `result`};
 }
 }
 }
 raise ArgumentError, 'invalid date'
end

.todayObject

340
341
342
# File 'opal/stdlib/date.rb', line 340
def today
 wrap `new Date()`
end

.wrap(native) ⇒ Object

88
89
90
91
92
# File 'opal/stdlib/date.rb', line 88
def wrap(native)
 instance = allocate
 `#{instance}.date = #{native}`
 instance
end

Instance Method Details

#+(date) ⇒ Object

376
377
378
379
380
381
382
383
384
385
386
387
# File 'opal/stdlib/date.rb', line 376
def +(date)
 %x{
 if (date.$$is_number) {
 var result = #{clone};
 result.date.setDate(#{@date}.getDate() + date);
 return result;
 }
 else {
 #{raise TypeError};
 }
 }
end

#-(date) ⇒ Object

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'opal/stdlib/date.rb', line 360
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

389
390
391
392
393
394
395
396
# File 'opal/stdlib/date.rb', line 389
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

465
466
467
468
469
470
471
472
473
# File 'opal/stdlib/date.rb', line 465
def <<(n)
 %x{
 if (!n.$$is_number) {
 #{raise TypeError};
 }
 return #{self >> `-n`};
 }
end

#<=(other) ⇒ Object

398
399
400
401
402
403
404
405
# File 'opal/stdlib/date.rb', line 398
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

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'opal/stdlib/date.rb', line 425
def <=>(other)
 %x{
 if (other.$$is_number) {
 return #{jd <=> other}
 }
 if (#{Date  === other}) {
 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;
 }
 } else {
 return nil;
 }
 }
end

#>(other) ⇒ Object

407
408
409
410
411
412
413
414
# File 'opal/stdlib/date.rb', line 407
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

416
417
418
419
420
421
422
423
# File 'opal/stdlib/date.rb', line 416
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

451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'opal/stdlib/date.rb', line 451
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

203
204
205
# File 'opal/stdlib/json.rb', line 203
def as_json
 to_s
end

#cloneObject

477
478
479
# File 'opal/stdlib/date.rb', line 477
def clone
 Date .wrap (`new Date(#{@date}.getTime())`)
end

#cwdayObject

664
665
666
# File 'opal/stdlib/date.rb', line 664
def cwday
 `#{@date}.getDay() || 7`
end

#cweekObject

668
669
670
671
672
673
674
675
# File 'opal/stdlib/date.rb', line 668
def cweek
 %x{
 var d = new Date(#{@date});
 d.setHours(0,0,0);
 d.setDate(d.getDate()+4-(d.getDay()||7));
 return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
 }
end

#dayObject

481
482
483
# File 'opal/stdlib/date.rb', line 481
def day
 `#{@date}.getDate()`
end

#downto(min, &block) ⇒ Object

648
649
650
# File 'opal/stdlib/date.rb', line 648
def downto(min, &block)
 step(min, -1, &block)
end

#friday?Boolean

Returns:

485
486
487
# File 'opal/stdlib/date.rb', line 485
def friday?
 wday == 5
end

#jdObject

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'opal/stdlib/date.rb', line 489
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:

525
526
527
# File 'opal/stdlib/date.rb', line 525
def julian?
 `#{@date} < new Date(1582, 10 - 1, 15, 12)`
end

#monday?Boolean

Returns:

529
530
531
# File 'opal/stdlib/date.rb', line 529
def monday?
 wday == 1
end

#monthObject

533
534
535
# File 'opal/stdlib/date.rb', line 533
def month
 `#{@date}.getMonth() + 1`
end

#nextObject Also known as: succ

537
538
539
# File 'opal/stdlib/date.rb', line 537
def next
 self + 1
end

#next_day(n = 1) ⇒ Object

541
542
543
# File 'opal/stdlib/date.rb', line 541
def next_day(n = 1)
 self + n
end

#next_month(n = 1) ⇒ Object

545
546
547
548
549
550
551
552
553
# File 'opal/stdlib/date.rb', line 545
def next_month(n = 1)
 %x{
 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

#next_year(years = 1) ⇒ Object

555
556
557
# File 'opal/stdlib/date.rb', line 555
def next_year(years = 1)
 self.class.new(year + years, month, day)
end

#prev_day(n = 1) ⇒ Object

559
560
561
# File 'opal/stdlib/date.rb', line 559
def prev_day(n = 1)
 self - n
end

#prev_month(n = 1) ⇒ Object

563
564
565
566
567
568
569
570
571
# File 'opal/stdlib/date.rb', line 563
def prev_month(n = 1)
 %x{
 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

#prev_year(years = 1) ⇒ Object

573
574
575
# File 'opal/stdlib/date.rb', line 573
def prev_year(years = 1)
 self.class.new(year - years, month, day)
end

#saturday?Boolean

Returns:

577
578
579
# File 'opal/stdlib/date.rb', line 577
def saturday?
 wday == 6
end

#step(limit, step = 1, &block) ⇒ Object

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'opal/stdlib/date.rb', line 622
def step(limit, step = 1, &block)
 steps_count = (limit - self).to_i
 steps = if steps_count * step < 0
 []
 elsif steps_count < 0
 (0..-steps_count).step(step.abs).map(&:-@) .reverse
 else
 (0..steps_count).step(step.abs)
 end
 result = steps.map { |i| self + i }
 if block_given?
 result.each { |i| yield(i) }
 self
 else
 result
 end
end

#strftime(format = '') ⇒ Object

581
582
583
584
585
586
587
588
589
# File 'opal/stdlib/date.rb', line 581
def strftime(format = '')
 %x{
 if (format == '') {
 return #{to_s};
 }
 return #{@date}.$strftime(#{format});
 }
end

#sunday?Boolean

Returns:

593
594
595
# File 'opal/stdlib/date.rb', line 593
def sunday?
 wday == 0
end

#thursday?Boolean

Returns:

597
598
599
# File 'opal/stdlib/date.rb', line 597
def thursday?
 wday == 4
end

#to_jsonObject

199
200
201
# File 'opal/stdlib/json.rb', line 199
def to_json
 to_s.to_json
end

#to_nObject

614
615
616
# File 'opal/stdlib/date.rb', line 614
def to_n
 @date
end

#to_sObject

601
602
603
604
605
606
607
608
# File 'opal/stdlib/date.rb', line 601
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

#to_timeObject

610
611
612
# File 'opal/stdlib/date.rb', line 610
def to_time
 Time .new(year, month, day)
end

#tuesday?Boolean

Returns:

618
619
620
# File 'opal/stdlib/date.rb', line 618
def tuesday?
 wday == 2
end

#upto(max, &block) ⇒ Object

644
645
646
# File 'opal/stdlib/date.rb', line 644
def upto(max, &block)
 step(max, 1, &block)
end

#wdayObject

652
653
654
# File 'opal/stdlib/date.rb', line 652
def wday
 `#{@date}.getDay()`
end

#wednesday?Boolean

Returns:

656
657
658
# File 'opal/stdlib/date.rb', line 656
def wednesday?
 wday == 3
end

#yearObject

660
661
662
# File 'opal/stdlib/date.rb', line 660
def year
 `#{@date}.getFullYear()`
end

AltStyle によって変換されたページ (->オリジナル) /