File: time.rb

package info (click to toggle)
ruby-extlib 0.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 576 kB
  • sloc: ruby: 7,014; makefile: 5
file content (44 lines) | stat: -rw-r--r-- 1,091 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
require 'date'
require 'extlib/datetime'

class Time

  ##
  # Convert to ISO 8601 representation
  #
  #   Time.now.to_json        #=> "\"2008-03-28T17:54:20-05:00\""
  #
  # @return [String]
  #   ISO 8601 compatible representation of the Time object.
  #
  # @api public
  def to_json(*)
    self.xmlschema.to_json
  end

  ##
  # Return receiver (for DateTime/Time conversion protocol).
  #
  #   Time.now.to_time        #=> Wed Nov 19 20:08:28 -0800 2008
  #
  # @return [Time] Receiver
  #
  # @api public
  remove_method :to_time if instance_methods(false).any? { |m| m.to_sym == :to_time }
  def to_time
    self
  end

  ##
  # Convert to DateTime (for DateTime/Time conversion protocol).
  #
  #   Time.now.to_datetime    #=> #<DateTime: 106046956823/43200,-1/3,2299161>
  #
  # @return [DateTime] DateTime object representing the same moment as receiver
  #
  # @api public
  remove_method :to_datetime if instance_methods(false).any? { |m| m.to_sym == :to_datetime }
  def to_datetime
    DateTime.new(year, month, day, hour, min, sec, Rational(gmt_offset, 24 * 3600))
  end
end