Class: DateTime

Inherits:
Date show all
Defined in:
opal/stdlib/date/date_time.rb,
opal/stdlib/date/formatters.rb

Overview

backtick_javascript: true

Constant Summary

Constants inherited from Date

Date::ABBR_DAYNAMES, Date::ABBR_MONTHNAMES, Date::DAYNAMES, Date::ENGLAND, Date::GREGORIAN, Date::ITALY, Date::JULIAN, Date::MONTHNAMES

Instance Attribute Summary

Attributes inherited from Date

#start

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Date

#<<, #<=>, #>>, _days_in_month, #as_json, #clone, #cwday, #cweek, def_formatter, #downto, gregorian_leap?, #jd, #julian?, #new_start, #next, #next_day, #next_month, #next_year, #prev_day, #prev_month, #prev_year, #step, #strftime, #to_json, #to_n, today, #upto, wrap

Methods included from Forwardable

#def_instance_delegator, #def_instance_delegators, #instance_delegate

Constructor Details

#initialize(year = -4712,, month = 1, day = 1, hours = 0, minutes = 0, seconds = 0, offset = 0, start = ITALY) ⇒ DateTime

Returns a new instance of DateTime.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/stdlib/date/date_time.rb', line 14

def initialize(year = -4712, month = 1, day = 1, hours = 0, minutes = 0, seconds = 0, offset = 0, 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 = Time.new(year, month, day, hours, minutes, seconds, offset)
  @start = start
end

Class Method Details

.nowObject



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

def now
  wrap Time.now
end

.parse(str) ⇒ Object



9
10
11
# File 'opal/stdlib/date/date_time.rb', line 9

def parse(str)
  wrap Time.parse(str)
end

Instance Method Details

#+(other) ⇒ Object



41
42
43
# File 'opal/stdlib/date/date_time.rb', line 41

def +(other)
  ::DateTime.wrap @date + other
end

#-(other) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'opal/stdlib/date/date_time.rb', line 45

def -(other)
  `if (Opal.is_a(other, #{::Date})) other = other.date`
  result = @date - other
  if result.is_a? ::Time
    ::DateTime.wrap result
  else
    result
  end
end

#new_offset(offset) ⇒ Object



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

def new_offset(offset)
  new_date = clone
  offset = Time._parse_offset(offset)
  `new_date.date.timezone = offset`
  new_date
end

#offsetObject



37
38
39
# File 'opal/stdlib/date/date_time.rb', line 37

def offset
  @date.gmt_offset / (24 * 3600r)
end

#sec_fractionObject Also known as: second_fraction



31
32
33
# File 'opal/stdlib/date/date_time.rb', line 31

def sec_fraction
  @date.usec / 1_000_000r
end

#to_dateObject



70
71
72
# File 'opal/stdlib/date/date_time.rb', line 70

def to_date
  Date.new(year, month, day)
end

#to_datetimeObject



62
63
64
# File 'opal/stdlib/date/date_time.rb', line 62

def to_datetime
  self
end

#to_timeObject



66
67
68
# File 'opal/stdlib/date/date_time.rb', line 66

def to_time
  @date.dup
end