Class: Time

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_formatter(name, format, on_utc: false, utc_tz: nil, tz_format: nil, fractions: false, on: self) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/stdlib/time.rb', line 6

def self.def_formatter(name, format, on_utc: false, utc_tz: nil, tz_format: nil, fractions: false, on: self)
  on.define_method name do |fdigits = 0|
    case self
    when defined?(::DateTime) && ::DateTime
      date = on_utc ? new_offset(0) : self
    when defined?(::Date) && ::Date
      date = ::Time.utc(year, month, day)
    when ::Time
      date = on_utc ? getutc : self
    end
    str = date.strftime(format)
    str += date.strftime(".%#{fdigits}N") if fractions && fdigits > 0
    if utc_tz
      str += utc ? utc_tz : date.strftime(tz_format)
    elsif tz_format
      str += date.strftime(tz_format)
    end
    str
  end
end

.parse(str) ⇒ Object



2
3
4
# File 'opal/stdlib/time.rb', line 2

def self.parse(str)
  `new Date(Date.parse(str))`
end

Instance Method Details

#to_dateObject



33
34
35
# File 'opal/stdlib/time.rb', line 33

def to_date
  Date.wrap(self)
end

#to_datetimeObject



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

def to_datetime
  DateTime.wrap(self)
end

#to_jsonObject



195
196
197
# File 'opal/stdlib/json.rb', line 195

def to_json
  strftime('%FT%T%z').to_json
end

#to_nObject



519
520
521
# File 'opal/stdlib/native.rb', line 519

def to_n
  self
end

#to_timeObject



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

def to_time
  self
end