Skip to content

Commit

Permalink
🔀 Merge pull request #218 from nevans/decode_datetime-optional-dquotes
Browse files Browse the repository at this point in the history
✨ Allow `decode_datetime` to work without dquotes
  • Loading branch information
nevans authored Nov 7, 2023
2 parents 2bcc6ef + e269e9c commit f27dd4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/net/imap/data_encoding.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "date"
require "time"

require_relative "errors"

Expand Down Expand Up @@ -102,8 +103,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

Expand All @@ -113,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
Expand Down
4 changes: 4 additions & 0 deletions test/net/imap/test_imap_data_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit f27dd4c

Please sign in to comment.