lib/sdl4r/parser.rb in sdl4r-0.9.8 vs lib/sdl4r/parser.rb in sdl4r-0.9.9

- old
+ new

@@ -198,29 +198,29 @@ if token.literal? # if a DATE token is followed by a TIME token combine them next_token = ((i + 1) < size)? tokens[i + 1] : nil if token.type == :DATE && next_token && next_token.type == :TIME date = token.object_for_literal() - time_zone_with_zone = next_token.object_for_literal() + time_span_with_zone = next_token.object_for_literal() - if time_zone_with_zone.day != 0 + if time_span_with_zone.day # as there are days specified, it can't be a full precision date tag.add_value(date); tag.add_value( SdlTimeSpan.new( - time_zone_with_zone.day, - time_zone_with_zone.hour, - time_zone_with_zone.min, - time_zone_with_zone.sec)) + time_span_with_zone.day || 0, + time_span_with_zone.hour, + time_span_with_zone.min, + time_span_with_zone.sec)) - if time_zone_with_zone.time_zone_offset + if time_span_with_zone.time_zone_offset parse_error("TimeSpan cannot have a timeZone", t.line, t.position) end else - tag.add_value(combine(date, time_zone_with_zone)) + tag.add_value(combine(date, time_span_with_zone)) end i += 1 else @@ -233,16 +233,11 @@ "TIME (component of date/time)", token.line, token.position) end - tag.add_value( - SdlTimeSpan.new( - value.day, - value.hour, - value.min, - value.sec)) + tag.add_value(SdlTimeSpan.new(value.day || 0, value.hour, value.min, value.sec)) else tag.add_value(value) end end elsif token.type == :IDENTIFIER @@ -368,11 +363,11 @@ if token.type == :DATE and (i + 1) < size and tokens[i + 1].type == :TIME date = token.object_for_literal() time_span_with_zone = tokens[i + 1].object_for_literal() - if time_span_with_zone.day != 0 + if time_span_with_zone.day expecting_but_got( "TIME (component of date/time) in attribute value", "TIME SPAN", token.line, token.position) @@ -391,11 +386,11 @@ token.line, token.position) end time_span = SdlTimeSpan.new( - time_span_with_zone.day, + time_span_with_zone.day || 0, time_span_with_zone.hour, time_span_with_zone.min, time_span_with_zone.sec) tag.set_attribute(name_or_namespace, time_span) else @@ -469,37 +464,35 @@ # Parses the given literal into a returned array # [days, hours, minutes, seconds, time_zone_offset]. # 'days', 'hours' and 'minutes' are integers. # 'seconds' and 'time_zone_offset' are rational numbers. - # 'days' and 'seconds' are equal to 0 if they're not specified in ((|literal|)). + # 'days' and 'seconds' are equal to 0 if they're not specified in +literal+. # 'time_zone_offset' is equal to nil if not specified. # - # ((|allowDays|)) indicates whether the specification of days is allowed - # in ((|literal|)) - # ((|allowTimeZone|)) indicates whether the specification of the timeZone is - # allowed in ((|literal|)) + # +allowDays+ indicates whether the specification of days is allowed + # in +literal+ + # +allowTimeZone+ indicates whether the specification of the timeZone is + # allowed in +literal+ # - # All components are returned disregarding the values of ((|allowDays|)) and - # ((|allowTimeZone|)). + # All components are returned disregarding the values of +allowDays+ and + # +allowTimeZone+. # - # Raises an ArgumentError if ((|literal|)) has a bad format. + # Raises an ArgumentError if +literal+ has a bad format. def Parser.parse_time_span_and_time_zone(literal, allowDays, allowTimeZone) overall_sign = (literal =~ /^-/)? -1 : +1 if literal =~ /^(([+\-]?\d+)d:)/ if allowDays days = Integer($2) - days_specified = true time_part = literal[($1.length)..-1] else # detected a day specification in a pure time literal raise ArgumentError, "unexpected day specification in #{literal}" end else - days = 0; - days_specified = false + days = nil time_part = literal end # We have to parse the string ourselves because AFAIK : # - strptime() can't parse milliseconds @@ -542,11 +535,11 @@ # unexpected timeSpan syntax raise ArgumentError, "unexpected sign on hours : #{literal}" end # take the sign into account - hours *= overall_sign if days_specified # otherwise the sign is already applied to the hours + hours *= overall_sign if days # otherwise the sign is already applied to the hours minutes *= overall_sign seconds *= overall_sign return [ days, hours, minutes, seconds, time_zone_offset ] @@ -555,11 +548,11 @@ end end # Parses the given literal (String) into a returned DateTime object. # - # Raises an ArgumentError if ((|literal|)) has a bad format. + # Raises an ArgumentError if +literal+ has a bad format. def Parser.parse_date_time(literal) raise ArgumentError("date literal is nil") if literal.nil? begin parts = literal.split(" ") @@ -605,24 +598,23 @@ # # Raises an ArgumentError if +literal+ has a bad format. def Parser.parse_date(literal) # here, we're being stricter than strptime() alone as we forbid trailing chars - if literal =~ /^(\d+)\/(\d+)\/(\d+)$/ + if literal =~ /^(-?\d+)\/(\d+)\/(\d+)$/ begin return Date.strptime(literal, "%Y/%m/%d") rescue ArgumentError raise ArgumentError, "Malformed Date <#{literal}> : #{$!.message}" end end raise ArgumentError, "Malformed Date <#{literal}>" end - # Returns a String that contains the binary content corresponding to ((|literal|)). + # Returns a String that contains the binary content corresponding to +literal+. # - # ((|literal|)) : a base-64 encoded literal (e.g. - # "[V2hvIHdhbnRzIHRvIGxpdmUgZm9yZXZlcj8=]") + # +literal+ : a base-64 encoded literal (e.g. "[V2hvIHdhbnRzIHRvIGxpdmUgZm9yZXZlcj8=]") def Parser.parse_binary(literal) clean_literal = literal[1..-2] # remove square brackets return SdlBinary.decode64(clean_literal) end