lib/vpim/rfc2425.rb in vpim-0.360 vs lib/vpim/rfc2425.rb in vpim-0.597

- old
+ new

@@ -1,7 +1,7 @@ =begin - Copyright (C) 2006 Sam Roberts + Copyright (C) 2008 Sam Roberts This library is free software; you can redistribute it and/or modify it under the same terms as the ruby language itself, see the file COPYING for details. =end @@ -103,16 +103,21 @@ list end # Convert a RFC 2425 date into an array of [year, month, day]. def Vpim.decode_date(v) # :nodoc: - unless v =~ %r{\s*#{Bnf::DATE}\s*} + unless v =~ %r{^\s*#{Bnf::DATE}\s*$} raise Vpim::InvalidEncodingError, "date not valid (#{v})" end [$1.to_i, $2.to_i, $3.to_i] end + # Convert a RFC 2425 date into an array of Date objects. + def self.decode_date_to_date(v) + Date.new(*decode_date(v)) + end + # Note in the following the RFC2425 allows yyyy-mm-ddThh:mm:ss, but RFC2445 # does not. I choose to encode to the subset that is valid for both. # Encode a Date object as "yyyymmdd". def Vpim.encode_date(d) # :nodoc: @@ -129,21 +134,36 @@ "%0.4d%0.2d%0.2dT%0.2d%0.2d%0.2d" % [ d.year, d.mon, d.day, d.hour, d.min, d.sec ] end # Convert a RFC 2425 time into an array of [hour,min,sec,secfrac,timezone] def Vpim.decode_time(v) # :nodoc: - unless match = %r{\s*#{Bnf::TIME}\s*}.match(v) - raise Vpim::InvalidEncodingError, "time not valid (#{v})" + unless match = %r{^\s*#{Bnf::TIME}\s*$}.match(v) + raise Vpim::InvalidEncodingError, "time '#{v}' not valid" end hour, min, sec, secfrac, tz = match.to_a[1..5] [hour.to_i, min.to_i, sec.to_i, secfrac ? secfrac.to_f : 0, tz] end - # Convert a RFC 2425 date-time into an array of [hour,min,sec,secfrac,timezone] + def self.array_datetime_to_time(dtarray) #:nodoc: + # We get [ year, month, day, hour, min, sec, usec, tz ] + begin + tz = (dtarray.pop == "Z") ? :gm : :local + Time.send(tz, *dtarray) + rescue ArgumentError => e + raise Vpim::InvalidEncodingError, "#{tz} #{e} (#{dtarray.join(', ')})" + end + end + + # Convert a RFC 2425 time into an array of Time objects. + def Vpim.decode_time_to_time(v) # :nodoc: + array_datetime_to_time(decode_date_time(v)) + end + + # Convert a RFC 2425 date-time into an array of [year,mon,day,hour,min,sec,secfrac,timezone] def Vpim.decode_date_time(v) # :nodoc: - unless match = %r{\s*#{Bnf::DATE}T#{Bnf::TIME}\s*}.match(v) + unless match = %r{^\s*#{Bnf::DATE}T#{Bnf::TIME}\s*$}.match(v) raise Vpim::InvalidEncodingError, "date-time '#{v}' not valid" end year, month, day, hour, min, sec, secfrac, tz = match.to_a[1..8] [ @@ -170,12 +190,10 @@ v.to_i end # # integer_list - # - # text_list # Convert a RFC2425 date-list into an array of dates. def Vpim.decode_date_list(v) # :nodoc: Vpim.decode_list(v) do |date| date.strip! @@ -226,19 +244,10 @@ end end end def Vpim.encode_text(v) #:nodoc: - v.to_str.gsub(/([.\n])/) do - case $1 - when "\n" - "\\n" - when "\\", ",", ";" - "\\#{$1}" - else - $1 - end - end + v.to_str.gsub(/([\\,;\n])/) { $1 == "\n" ? "\\n" : "\\"+$1 } end # v is an Array of String, or just a single String def Vpim.encode_text_list(v, sep = ",") #:nodoc: begin