Class: Time
Class Method Summary collapse
- .at(seconds, frac = undefined) ⇒ Object
- .gm(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) ⇒ Object (also: utc)
- .local(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) ⇒ Object (also: mktime)
- .new(year = undefined, month = nil, day = nil, hour = nil, min = nil, sec = nil, utc_offset = nil) ⇒ Object
- .now ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #asctime ⇒ Object (also: #ctime)
- #cweek_cyear ⇒ Object
- #day ⇒ Object (also: #mday)
- #dup ⇒ Object
- #eql?(other) ⇒ Boolean
- #friday? ⇒ Boolean
- #getgm ⇒ Object (also: #getutc)
- #gmt? ⇒ Boolean (also: #utc?)
- #gmt_offset ⇒ Object (also: #gmtoff, #utc_offset)
- #gmtime ⇒ Object (also: #utc)
- #hash ⇒ Object
- #hour ⇒ Object
- #inspect ⇒ Object (also: #to_s)
- #isdst ⇒ Object (also: #dst?)
- #min ⇒ Object
- #mon ⇒ Object (also: #month)
- #monday? ⇒ Boolean
- #saturday? ⇒ Boolean
- #sec ⇒ Object
- #strftime(format) ⇒ Object
- #succ ⇒ Object
- #sunday? ⇒ Boolean
- #thursday? ⇒ Boolean
- #to_a ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object (also: #tv_sec)
- #tuesday? ⇒ Boolean
- #usec ⇒ Object (also: #tv_usec)
- #wday ⇒ Object
- #wednesday? ⇒ Boolean
- #yday ⇒ Object
- #year ⇒ Object
- #zone ⇒ Object
Methods included from Comparable
#<, #<=, #>, #>=, #between?, #clamp
Class Method Details
.at(seconds, frac = undefined) ⇒ Object
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 |
# File 'opal/opal/corelib/time.rb', line 15 def self.at(seconds, frac = undefined) %x{ var result; if (#{::Time === seconds}) { if (frac !== undefined) { #{::Kernel.raise ::TypeError, "can't convert Time into an exact number"} } result = new Date(seconds.getTime()); result.is_utc = seconds.is_utc; return result; } if (!seconds.$$is_number) { seconds = #{::Opal.coerce_to!(seconds, ::Integer, :to_int)}; } if (frac === undefined) { return new Date(seconds * 1000); } if (!frac.$$is_number) { frac = #{::Opal.coerce_to!(frac, ::Integer, :to_int)}; } return new Date(seconds * 1000 + (frac / 1000)); } end |
.gm(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) ⇒ Object Also known as: utc
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 |
# File 'opal/opal/corelib/time.rb', line 195 def self.gm(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) # The _dummy args are there only because the MRI version accepts up to 10 arguments %x{ var args, result; if (arguments.length === 10) { args = $slice.call(arguments); year = args[5]; month = args[4]; day = args[3]; hour = args[2]; min = args[1]; sec = args[0]; } args = time_params(year, month, day, hour, min, sec); year = args[0]; month = args[1]; day = args[2]; hour = args[3]; min = args[4]; sec = args[5]; result = new Date(Date.UTC(year, month, day, hour, min, 0, sec * 1000)); if (year < 100) { result.setUTCFullYear(year); } result.is_utc = true; return result; } end |
.local(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) ⇒ Object Also known as: mktime
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 |
# File 'opal/opal/corelib/time.rb', line 164 def self.local(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, millisecond = nil, _dummy1 = nil, _dummy2 = nil, _dummy3 = nil) # The _dummy args are there only because the MRI version accepts up to 10 arguments %x{ var args, result; if (arguments.length === 10) { args = $slice.call(arguments); year = args[5]; month = args[4]; day = args[3]; hour = args[2]; min = args[1]; sec = args[0]; } args = time_params(year, month, day, hour, min, sec); year = args[0]; month = args[1]; day = args[2]; hour = args[3]; min = args[4]; sec = args[5]; result = new Date(year, month, day, hour, min, 0, sec * 1000); if (year < 100) { result.setFullYear(year); } return result; } end |
.new(year = undefined, month = nil, day = nil, hour = nil, min = nil, sec = nil, utc_offset = nil) ⇒ Object
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 |
# File 'opal/opal/corelib/time.rb', line 136 def self.new(year = undefined, month = nil, day = nil, hour = nil, min = nil, sec = nil, utc_offset = nil) %x{ var args, result; if (year === undefined) { return new Date(); } if (utc_offset !== nil) { #{::Kernel.raise ::ArgumentError, 'Opal does not support explicitly specifying UTC offset for Time'} } args = time_params(year, month, day, hour, min, sec); year = args[0]; month = args[1]; day = args[2]; hour = args[3]; min = args[4]; sec = args[5]; result = new Date(year, month, day, hour, min, 0, sec * 1000); if (year < 100) { result.setFullYear(year); } return result; } end |
.now ⇒ Object
227 228 229 |
# File 'opal/opal/corelib/time.rb', line 227 def self.now new end |
Instance Method Details
#+(other) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'opal/opal/corelib/time.rb', line 231 def +(other) if ::Time === other ::Kernel.raise ::TypeError, 'time + time?' end %x{ if (!other.$$is_number) { other = #{::Opal.coerce_to!(other, ::Integer, :to_int)}; } var result = new Date(self.getTime() + (other * 1000)); result.is_utc = self.is_utc; return result; } end |
#-(other) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'opal/opal/corelib/time.rb', line 246 def -(other) if ::Time === other return `(self.getTime() - other.getTime()) / 1000` end %x{ if (!other.$$is_number) { other = #{::Opal.coerce_to!(other, ::Integer, :to_int)}; } var result = new Date(self.getTime() - (other * 1000)); result.is_utc = self.is_utc; return result; } end |
#<=>(other) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'opal/opal/corelib/time.rb', line 261 def <=>(other) if ::Time === other to_f <=> other.to_f else r = other <=> self if r.nil? nil elsif r > 0 -1 elsif r < 0 1 else 0 end end end |
#==(other) ⇒ Object
278 279 280 |
# File 'opal/opal/corelib/time.rb', line 278 def ==(other) ::Time === other && `#{to_f} === #{other.to_f}` end |
#asctime ⇒ Object Also known as: ctime
282 283 284 |
# File 'opal/opal/corelib/time.rb', line 282 def asctime strftime '%a %b %e %H:%M:%S %Y' end |
#cweek_cyear ⇒ Object
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'opal/opal/corelib/time.rb', line 694 def cweek_cyear jan01 = ::Time.new(year, 1, 1) jan01_wday = jan01.wday first_monday = 0 year = self.year if jan01_wday <= 4 && jan01_wday != 0 # Jan 01 is in the first week of the year offset = jan01_wday - 1 else # Jan 01 is in the last week of the previous year offset = jan01_wday - 7 - 1 offset = -1 if offset == -8 # Adjust if Jan 01 is a Sunday end week = ((yday + offset) / 7.00).ceil if week <= 0 # Get the last week of the previous year return ::Time.new(self.year - 1, 12, 31).cweek_cyear elsif week == 53 # Find out whether this is actually week 53 or already week 01 of the following year dec31 = ::Time.new(self.year, 12, 31) dec31_wday = dec31.wday if dec31_wday <= 3 && dec31_wday != 0 week = 1 year += 1 end end [week, year] end |
#day ⇒ Object Also known as: mday
286 287 288 |
# File 'opal/opal/corelib/time.rb', line 286 def day `self.is_utc ? self.getUTCDate() : self.getDate()` end |
#dup ⇒ Object
309 310 311 312 313 314 315 316 |
# File 'opal/opal/corelib/time.rb', line 309 def dup copy = `new Date(self.getTime())` copy.copy_instance_variables(self) copy.initialize_dup(self) copy end |
#eql?(other) ⇒ Boolean
318 319 320 |
# File 'opal/opal/corelib/time.rb', line 318 def eql?(other) other.is_a?(::Time) && (self <=> other).zero? end |
#friday? ⇒ Boolean
322 323 324 |
# File 'opal/opal/corelib/time.rb', line 322 def friday? `#{wday} == 5` end |
#getgm ⇒ Object Also known as: getutc
395 396 397 398 399 400 401 |
# File 'opal/opal/corelib/time.rb', line 395 def getgm %x{ var result = new Date(self.getTime()); result.is_utc = true; return result; } end |
#gmt? ⇒ Boolean Also known as: utc?
410 411 412 |
# File 'opal/opal/corelib/time.rb', line 410 def gmt? `self.is_utc === true` end |
#gmt_offset ⇒ Object Also known as: gmtoff, utc_offset
414 415 416 |
# File 'opal/opal/corelib/time.rb', line 414 def gmt_offset `self.is_utc ? 0 : -self.getTimezoneOffset() * 60` end |
#gmtime ⇒ Object Also known as: utc
403 404 405 406 407 408 |
# File 'opal/opal/corelib/time.rb', line 403 def gmtime %x{ self.is_utc = true; return self; } end |
#hash ⇒ Object
326 327 328 |
# File 'opal/opal/corelib/time.rb', line 326 def hash `'Time:' + self.getTime()` end |
#hour ⇒ Object
330 331 332 |
# File 'opal/opal/corelib/time.rb', line 330 def hour `self.is_utc ? self.getUTCHours() : self.getHours()` end |
#inspect ⇒ Object Also known as: to_s
334 335 336 337 338 339 340 |
# File 'opal/opal/corelib/time.rb', line 334 def inspect if utc? strftime '%Y-%m-%d %H:%M:%S UTC' else strftime '%Y-%m-%d %H:%M:%S %z' end end |
#isdst ⇒ Object Also known as: dst?
301 302 303 304 305 306 307 |
# File 'opal/opal/corelib/time.rb', line 301 def isdst %x{ var jan = new Date(self.getFullYear(), 0, 1), jul = new Date(self.getFullYear(), 6, 1); return self.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); } end |
#min ⇒ Object
342 343 344 |
# File 'opal/opal/corelib/time.rb', line 342 def min `self.is_utc ? self.getUTCMinutes() : self.getMinutes()` end |
#mon ⇒ Object Also known as: month
346 347 348 |
# File 'opal/opal/corelib/time.rb', line 346 def mon `(self.is_utc ? self.getUTCMonth() : self.getMonth()) + 1` end |
#monday? ⇒ Boolean
350 351 352 |
# File 'opal/opal/corelib/time.rb', line 350 def monday? `#{wday} == 1` end |
#saturday? ⇒ Boolean
354 355 356 |
# File 'opal/opal/corelib/time.rb', line 354 def saturday? `#{wday} == 6` end |
#sec ⇒ Object
358 359 360 |
# File 'opal/opal/corelib/time.rb', line 358 def sec `self.is_utc ? self.getUTCSeconds() : self.getSeconds()` end |
#strftime(format) ⇒ Object
418 419 420 421 422 423 424 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
# File 'opal/opal/corelib/time.rb', line 418 def strftime(format) %x{ return format.replace(/%([\-_#^0]*:{0,2})(\d+)?([EO]*)(.)/g, function(full, flags, width, _, conv) { var result = "", zero = flags.indexOf('0') !== -1, pad = flags.indexOf('-') === -1, blank = flags.indexOf('_') !== -1, upcase = flags.indexOf('^') !== -1, invert = flags.indexOf('#') !== -1, colons = (flags.match(':') || []).length; width = parseInt(width, 10); if (zero && blank) { if (flags.indexOf('0') < flags.indexOf('_')) { zero = false; } else { blank = false; } } switch (conv) { case 'Y': result += #{year}; break; case 'C': zero = !blank; result += Math.round(#{year} / 100); break; case 'y': zero = !blank; result += (#{year} % 100); break; case 'm': zero = !blank; result += #{mon}; break; case 'B': result += long_months[#{mon} - 1]; break; case 'b': case 'h': blank = !zero; result += short_months[#{mon} - 1]; break; case 'd': zero = !blank result += #{day}; break; case 'e': blank = !zero result += #{day}; break; case 'j': zero = !blank; width = isNaN(width) ? 3 : width; result += #{yday}; break; case 'H': zero = !blank; result += #{hour}; break; case 'k': blank = !zero; result += #{hour}; break; case 'I': zero = !blank; result += (#{hour} % 12 || 12); break; case 'l': blank = !zero; result += (#{hour} % 12 || 12); break; case 'P': result += (#{hour} >= 12 ? "pm" : "am"); break; case 'p': result += (#{hour} >= 12 ? "PM" : "AM"); break; case 'M': zero = !blank; result += #{min}; break; case 'S': zero = !blank; result += #{sec} break; case 'L': zero = !blank; width = isNaN(width) ? 3 : width; result += self.getMilliseconds(); break; case 'N': width = isNaN(width) ? 9 : width; result += #{`self.getMilliseconds().toString()`.rjust(3, '0')}; result = #{`result`.ljust(`width`, '0')}; break; case 'z': var offset = self.getTimezoneOffset(), hours = Math.floor(Math.abs(offset) / 60), minutes = Math.abs(offset) % 60; result += offset < 0 ? "+" : "-"; result += hours < 10 ? "0" : ""; result += hours; if (colons > 0) { result += ":"; } result += minutes < 10 ? "0" : ""; result += minutes; if (colons > 1) { result += ":00"; } break; case 'Z': result += #{zone}; break; case 'A': result += days_of_week[#{wday}]; break; case 'a': result += short_days[#{wday}]; break; case 'u': result += (#{wday} + 1); break; case 'w': result += #{wday}; break; case 'V': result += #{cweek_cyear[0].to_s.rjust(2, '0')}; break; case 'G': result += #{cweek_cyear[1]}; break; case 'g': result += #{cweek_cyear[1][-2..-1]}; break; case 's': result += #{to_i}; break; case 'n': result += "\n"; break; case 't': result += "\t"; break; case '%': result += "%"; break; case 'c': result += #{strftime('%a %b %e %T %Y')}; break; case 'D': case 'x': result += #{strftime('%m/%d/%y')}; break; case 'F': result += #{strftime('%Y-%m-%d')}; break; case 'v': result += #{strftime('%e-%^b-%4Y')}; break; case 'r': result += #{strftime('%I:%M:%S %p')}; break; case 'R': result += #{strftime('%H:%M')}; break; case 'T': case 'X': result += #{strftime('%H:%M:%S')}; break; default: return full; } if (upcase) { result = result.toUpperCase(); } if (invert) { result = result.replace(/[A-Z]/, function(c) { c.toLowerCase() }). replace(/[a-z]/, function(c) { c.toUpperCase() }); } if (pad && (zero || blank)) { result = #{`result`.rjust(`isNaN(width) ? 2 : width`, `blank ? " " : "0"`)}; } return result; }); } end |
#succ ⇒ Object
362 363 364 365 366 367 368 |
# File 'opal/opal/corelib/time.rb', line 362 def succ %x{ var result = new Date(self.getTime() + 1000); result.is_utc = self.is_utc; return result; } end |
#sunday? ⇒ Boolean
658 659 660 |
# File 'opal/opal/corelib/time.rb', line 658 def sunday? `#{wday} == 0` end |
#thursday? ⇒ Boolean
662 663 664 |
# File 'opal/opal/corelib/time.rb', line 662 def thursday? `#{wday} == 4` end |
#to_a ⇒ Object
666 667 668 |
# File 'opal/opal/corelib/time.rb', line 666 def to_a [sec, min, hour, day, month, year, wday, yday, isdst, zone] end |
#to_f ⇒ Object
670 671 672 |
# File 'opal/opal/corelib/time.rb', line 670 def to_f `self.getTime() / 1000` end |
#to_i ⇒ Object Also known as: tv_sec
674 675 676 |
# File 'opal/opal/corelib/time.rb', line 674 def to_i `parseInt(self.getTime() / 1000, 10)` end |
#tuesday? ⇒ Boolean
678 679 680 |
# File 'opal/opal/corelib/time.rb', line 678 def tuesday? `#{wday} == 2` end |
#usec ⇒ Object Also known as: tv_usec
370 371 372 |
# File 'opal/opal/corelib/time.rb', line 370 def usec `self.getMilliseconds() * 1000` end |
#wday ⇒ Object
682 683 684 |
# File 'opal/opal/corelib/time.rb', line 682 def wday `self.is_utc ? self.getUTCDay() : self.getDay()` end |
#wednesday? ⇒ Boolean
686 687 688 |
# File 'opal/opal/corelib/time.rb', line 686 def wednesday? `#{wday} == 3` end |
#yday ⇒ Object
290 291 292 293 294 295 296 297 298 299 |
# File 'opal/opal/corelib/time.rb', line 290 def yday # http://javascript.about.com/library/bldayyear.htm # also see moment.js implementation: http://git.io/vCKNE start_of_year = Time.new(year).to_i start_of_day = Time.new(year, month, day).to_i one_day = 86_400 ((start_of_day - start_of_year) / one_day).round + 1 end |
#year ⇒ Object
690 691 692 |
# File 'opal/opal/corelib/time.rb', line 690 def year `self.is_utc ? self.getUTCFullYear() : self.getFullYear()` end |
#zone ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'opal/opal/corelib/time.rb', line 374 def zone %x{ var string = self.toString(), result; if (string.indexOf('(') == -1) { result = string.match(/[A-Z]{3,4}/)[0]; } else { result = string.match(/\((.+)\)(?:\s|$)/)[1] } if (result == "GMT" && /(GMT\W*\d{4})/.test(string)) { return RegExp.$1; } else { return result; } } end |