Skip to content

Commit 599f7fe

Browse files
committed
✨ Allow decode_datetime to work without dquotes
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.
1 parent 2bcc6ef commit 599f7fe

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/net/imap/data_encoding.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,16 @@ def self.encode_datetime(time)
102102
#
103103
# Decodes +string+ as an IMAP4 formatted "date-time".
104104
#
105-
# Note that double quotes are not optional. See STRFTIME.
105+
# NOTE: Although double-quotes are not optional in the IMAP grammar,
106+
# Net::IMAP currently parses "date-time" values as "quoted" strings and this
107+
# removes the quotation marks. To be useful for strings which have already
108+
# been parsed as a quoted string, this method makes double-quotes optional.
109+
#
110+
# See STRFTIME.
106111
def self.decode_datetime(string)
112+
unless string.start_with?(?") && string.end_with?(?")
113+
string = '"%s"' % [string]
114+
end
107115
DateTime.strptime(string, STRFTIME)
108116
end
109117

test/net/imap/test_imap_data_encoding.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def test_decode_datetime
5959
expected = DateTime.new(2022, 10, 6, 1, 2, 3, "-04:00")
6060
actual = Net::IMAP.decode_datetime('"06-Oct-2022 01:02:03 -0400"')
6161
assert_equal expected, actual
62+
actual = Net::IMAP.decode_datetime("06-Oct-2022 01:02:03 -0400")
63+
assert_equal expected, actual
6264
actual = Net::IMAP.parse_datetime '" 6-Oct-2022 01:02:03 -0400"'
6365
assert_equal expected, actual
6466
end
@@ -69,6 +71,8 @@ def test_decode_time
6971
assert_equal expected, actual
7072
actual = Net::IMAP.decode_time '" 7-Nov-2020 01:02:03 -0400"'
7173
assert_equal expected, actual
74+
actual = Net::IMAP.parse_time "07-Nov-2020 01:02:03 -0400"
75+
assert_equal expected, actual
7276
end
7377

7478
end

0 commit comments

Comments
 (0)