1#
2# xmlrpc/datetime.rb
3# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
4#
5# Released under the same term of license as Ruby.
6#
7require "date"
8
9module XMLRPC # :nodoc:
10
11# This class is important to handle XMLRPC +dateTime.iso8601+ values,
12# correcly, because normal UNIX-dates, ie: Date, only handle dates
13# from year 1970 on, and ruby's native Time class handles dates without the
14# time component.
15#
16# XMLRPC::DateTime is able to store a XMLRPC +dateTime.iso8601+ value correctly.
17class DateTime
18
19  # Return the value of the specified date/time component.
20  attr_reader :year, :month, :day, :hour, :min, :sec
21
22  # Set +value+ as the new date/time component.
23  #
24  # Raises ArgumentError if the given +value+ is out of range, or in the case
25  # of XMLRPC::DateTime#year= if +value+ is not of type Integer.
26  def year= (value)
27    raise ArgumentError, "date/time out of range" unless value.is_a? Integer
28    @year = value
29  end
30
31  # Set +value+ as the new date/time component.
32  #
33  # Raises an ArgumentError if the given +value+ isn't between 1 and 12.
34  def month= (value)
35    raise ArgumentError, "date/time out of range" unless (1..12).include? value
36    @month = value
37  end
38
39  # Set +value+ as the new date/time component.
40  #
41  # Raises an ArgumentError if the given +value+ isn't between 1 and 31.
42  def day= (value)
43    raise ArgumentError, "date/time out of range" unless (1..31).include? value
44    @day = value
45  end
46
47  # Set +value+ as the new date/time component.
48  #
49  # Raises an ArgumentError if the given +value+ isn't between 0 and 24.
50  def hour= (value)
51    raise ArgumentError, "date/time out of range" unless (0..24).include? value
52    @hour = value
53  end
54
55  # Set +value+ as the new date/time component.
56  #
57  # Raises an ArgumentError if the given +value+ isn't between 0 and 59.
58  def min= (value)
59    raise ArgumentError, "date/time out of range" unless (0..59).include? value
60    @min = value
61  end
62
63  # Set +value+ as the new date/time component.
64  #
65  # Raises an ArgumentError if the given +value+ isn't between 0 and 59.
66  def sec= (value)
67    raise ArgumentError, "date/time out of range" unless (0..59).include? value
68    @sec = value
69  end
70
71  # Alias for XMLRPC::DateTime#month.
72  alias mon  month
73  # Alias for XMLRPC::DateTime#month=.
74  alias mon= month=
75
76
77  # Creates a new XMLRPC::DateTime instance with the
78  # parameters +year+, +month+, +day+ as date and
79  # +hour+, +min+, +sec+ as time.
80  #
81  # Raises an ArgumentError if a parameter is out of range,
82  # or if +year+ is not of the Integer type.
83  def initialize(year, month, day, hour, min, sec)
84    self.year, self.month, self.day = year, month, day
85    self.hour, self.min, self.sec   = hour, min, sec
86  end
87
88  # Return a Time object of the date/time which represents +self+.
89  # If the <code>@year</code> is below 1970, this method returns +nil+,
90  # because Time cannot handle years below 1970.
91  #
92  # The timezone used is GMT.
93  def to_time
94    if @year >= 1970
95      Time.gm(*to_a)
96    else
97      nil
98    end
99  end
100
101  # Return a Date object of the date which represents +self+.
102  #
103  # The Date object do _not_ contain the time component (only date).
104  def to_date
105    Date.new(*to_a[0,3])
106  end
107
108  # Returns all date/time components in an array.
109  #
110  # Returns +[year, month, day, hour, min, sec]+.
111  def to_a
112    [@year, @month, @day, @hour, @min, @sec]
113  end
114
115  # Returns whether or not all date/time components are an array.
116  def ==(o)
117    self.to_a == Array(o) rescue false
118  end
119
120end
121
122
123end # module XMLRPC
124
125
126=begin
127= History
128    $Id: datetime.rb 36958 2012-09-13 02:22:10Z zzak $
129=end
130