From 599f7feec64cd633883c89dd176681f3db1ffe08 Mon Sep 17 00:00:00 2001 From: nicholas evans Date: Sun, 5 Nov 2023 16:58:16 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Allow=20`decode=5Fdatetime`=20t?= =?UTF-8?q?o=20work=20without=20dquotes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This modifies the code added in #66 to make it more useful when `date-time` is parsed as a `quoted` string (as it currently is). I'd prefer that ResponseParser simply parse `date-time` values as DateTime objects, but that has backwards compatibility issues. --- lib/net/imap/data_encoding.rb | 10 +++++++++- test/net/imap/test_imap_data_encoding.rb | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/net/imap/data_encoding.rb b/lib/net/imap/data_encoding.rb index e4792305..d11de785 100644 --- a/lib/net/imap/data_encoding.rb +++ b/lib/net/imap/data_encoding.rb @@ -102,8 +102,16 @@ def self.encode_datetime(time) # # Decodes +string+ as an IMAP4 formatted "date-time". # - # Note that double quotes are not optional. See STRFTIME. + # NOTE: Although double-quotes are not optional in the IMAP grammar, + # Net::IMAP currently parses "date-time" values as "quoted" strings and this + # removes the quotation marks. To be useful for strings which have already + # been parsed as a quoted string, this method makes double-quotes optional. + # + # See STRFTIME. def self.decode_datetime(string) + unless string.start_with?(?") && string.end_with?(?") + string = '"%s"' % [string] + end DateTime.strptime(string, STRFTIME) end diff --git a/test/net/imap/test_imap_data_encoding.rb b/test/net/imap/test_imap_data_encoding.rb index 68ee7e35..dfc434f1 100644 --- a/test/net/imap/test_imap_data_encoding.rb +++ b/test/net/imap/test_imap_data_encoding.rb @@ -59,6 +59,8 @@ def test_decode_datetime expected = DateTime.new(2022, 10, 6, 1, 2, 3, "-04:00") actual = Net::IMAP.decode_datetime('"06-Oct-2022 01:02:03 -0400"') assert_equal expected, actual + actual = Net::IMAP.decode_datetime("06-Oct-2022 01:02:03 -0400") + assert_equal expected, actual actual = Net::IMAP.parse_datetime '" 6-Oct-2022 01:02:03 -0400"' assert_equal expected, actual end @@ -69,6 +71,8 @@ def test_decode_time assert_equal expected, actual actual = Net::IMAP.decode_time '" 7-Nov-2020 01:02:03 -0400"' assert_equal expected, actual + actual = Net::IMAP.parse_time "07-Nov-2020 01:02:03 -0400" + assert_equal expected, actual end end From e269e9c4f52cd2b6f5860153a165ca5985815853 Mon Sep 17 00:00:00 2001 From: nicholas evans Date: Mon, 6 Nov 2023 23:03:59 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Use=20`Time#strptime`?= =?UTF-8?q?=20directly=20from=20`decode=5Ftime`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap/data_encoding.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/net/imap/data_encoding.rb b/lib/net/imap/data_encoding.rb index d11de785..ff464c1c 100644 --- a/lib/net/imap/data_encoding.rb +++ b/lib/net/imap/data_encoding.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "date" +require "time" require_relative "errors" @@ -121,7 +122,10 @@ def self.decode_datetime(string) # # Same as +decode_datetime+, but returning a Time instead. def self.decode_time(string) - decode_datetime(string).to_time + unless string.start_with?(?") && string.end_with?(?") + string = '"%s"' % [string] + end + Time.strptime(string, STRFTIME) end class << self