Skip to content

Commit f27dd4c

Browse files
authored
🔀 Merge pull request #218 from nevans/decode_datetime-optional-dquotes
✨ Allow `decode_datetime` to work without dquotes
2 parents 2bcc6ef + e269e9c commit f27dd4c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

‎lib/net/imap/data_encoding.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "date"
4+
require "time"
45

56
require_relative "errors"
67

@@ -102,8 +103,16 @@ def self.encode_datetime(time)
102103
#
103104
# Decodes +string+ as an IMAP4 formatted "date-time".
104105
#
105-
# Note that double quotes are not optional. See STRFTIME.
106+
# NOTE: Although double-quotes are not optional in the IMAP grammar,
107+
# Net::IMAP currently parses "date-time" values as "quoted" strings and this
108+
# removes the quotation marks. To be useful for strings which have already
109+
# been parsed as a quoted string, this method makes double-quotes optional.
110+
#
111+
# See STRFTIME.
106112
def self.decode_datetime(string)
113+
unless string.start_with?(?") && string.end_with?(?")
114+
string = '"%s"' % [string]
115+
end
107116
DateTime.strptime(string, STRFTIME)
108117
end
109118

@@ -113,7 +122,10 @@ def self.decode_datetime(string)
113122
#
114123
# Same as +decode_datetime+, but returning a Time instead.
115124
def self.decode_time(string)
116-
decode_datetime(string).to_time
125+
unless string.start_with?(?") && string.end_with?(?")
126+
string = '"%s"' % [string]
127+
end
128+
Time.strptime(string, STRFTIME)
117129
end
118130

119131
class << self

‎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)