/tools/Ruby/lib/ruby/1.8/xmlrpc/datetime.rb

http://github.com/agross/netopenspace · Ruby · 142 lines · 52 code · 22 blank · 68 comment · 2 complexity · 77a3198a781cb8a94c301ec4838eaceb MD5 · raw file

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