2012-09-12 22:22:10 -04:00
|
|
|
#
|
|
|
|
# xmlrpc/datetime.rb
|
|
|
|
# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
|
|
|
|
#
|
|
|
|
# Released under the same term of license as Ruby.
|
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
require "date"
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
module XMLRPC # :nodoc:
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# This class is important to handle XMLRPC +dateTime.iso8601+ values,
|
|
|
|
# correcly, because normal UNIX-dates, ie: Date, only handle dates
|
|
|
|
# from year 1970 on, and ruby's native Time class handles dates without the
|
|
|
|
# time component.
|
|
|
|
#
|
|
|
|
# XMLRPC::DateTime is able to store a XMLRPC +dateTime.iso8601+ value correctly.
|
2003-07-19 06:05:54 -04:00
|
|
|
class DateTime
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Return the value of the specified date/time component.
|
2003-07-19 06:05:54 -04:00
|
|
|
attr_reader :year, :month, :day, :hour, :min, :sec
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises ArgumentError if the given +value+ is out of range, or in the case
|
|
|
|
# of XMLRPC::DateTime#year= if +value+ is not of type Integer.
|
2003-07-19 06:05:54 -04:00
|
|
|
def year= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless value.is_a? Integer
|
|
|
|
@year = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +value+ isn't between 1 and 12.
|
2003-07-19 06:05:54 -04:00
|
|
|
def month= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless (1..12).include? value
|
|
|
|
@month = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +value+ isn't between 1 and 31.
|
2003-07-19 06:05:54 -04:00
|
|
|
def day= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless (1..31).include? value
|
|
|
|
@day = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +value+ isn't between 0 and 24.
|
2003-07-19 06:05:54 -04:00
|
|
|
def hour= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless (0..24).include? value
|
|
|
|
@hour = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +value+ isn't between 0 and 59.
|
2003-07-19 06:05:54 -04:00
|
|
|
def min= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless (0..59).include? value
|
|
|
|
@min = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Set +value+ as the new date/time component.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +value+ isn't between 0 and 59.
|
2003-07-19 06:05:54 -04:00
|
|
|
def sec= (value)
|
|
|
|
raise ArgumentError, "date/time out of range" unless (0..59).include? value
|
|
|
|
@sec = value
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Alias for XMLRPC::DateTime#month.
|
2003-07-19 06:05:54 -04:00
|
|
|
alias mon month
|
2012-09-12 22:22:10 -04:00
|
|
|
# Alias for XMLRPC::DateTime#month=.
|
2009-03-05 22:56:38 -05:00
|
|
|
alias mon= month=
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Creates a new XMLRPC::DateTime instance with the
|
|
|
|
# parameters +year+, +month+, +day+ as date and
|
|
|
|
# +hour+, +min+, +sec+ as time.
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if a parameter is out of range,
|
|
|
|
# or if +year+ is not of the Integer type.
|
2003-07-19 06:05:54 -04:00
|
|
|
def initialize(year, month, day, hour, min, sec)
|
|
|
|
self.year, self.month, self.day = year, month, day
|
|
|
|
self.hour, self.min, self.sec = hour, min, sec
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Return a Time object of the date/time which represents +self+.
|
|
|
|
# If the <code>@year</code> is below 1970, this method returns +nil+,
|
|
|
|
# because Time cannot handle years below 1970.
|
|
|
|
#
|
|
|
|
# The timezone used is GMT.
|
2003-07-19 06:05:54 -04:00
|
|
|
def to_time
|
|
|
|
if @year >= 1970
|
|
|
|
Time.gm(*to_a)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Return a Date object of the date which represents +self+.
|
|
|
|
#
|
|
|
|
# The Date object do _not_ contain the time component (only date).
|
2003-07-19 06:05:54 -04:00
|
|
|
def to_date
|
|
|
|
Date.new(*to_a[0,3])
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Returns all date/time components in an array.
|
|
|
|
#
|
|
|
|
# Returns +[year, month, day, hour, min, sec]+.
|
2003-07-19 06:05:54 -04:00
|
|
|
def to_a
|
|
|
|
[@year, @month, @day, @hour, @min, @sec]
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Returns whether or not all date/time components are an array.
|
2004-11-15 20:11:49 -05:00
|
|
|
def ==(o)
|
2005-11-29 23:50:04 -05:00
|
|
|
self.to_a == Array(o) rescue false
|
2004-11-15 20:11:49 -05:00
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end # module XMLRPC
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
= History
|
|
|
|
$Id$
|
|
|
|
=end
|