Packages

Pop3 client to download email (including attachments) from the inbox. Decodes multipart content, quoted-printables, base64 and encoded-words. Uses an Erlang pop3 client with SSL support derived from the epop package.

Current section

Files

Jump to
pop3mail lib pop3mail date_converter.ex
Raw

lib/pop3mail/date_converter.ex

defmodule Pop3mail.DateConverter do
@moduledoc "Date conversions and date utilities"
@doc "add zero's at left side of the number"
def zero_pad(number, len \\ 2) do
String.rjust(to_string(number), len, ?0)
end
@doc """
Convert date from email header to a standard date format: YYYYMMDD_HHMMSS
`datestr` - must be conform RFC 2822 date format
"""
def convert_date(date_str) do
# Example of correctly formatted date: Tue, 14 Oct 2014 19:59:31 +0200
# Sometimes the day of the week is missing in the date. Fix that:
# if date starts with digit, add the day first. We don't care which day it is, just add ???
date_str =
case date_str =~ ~r/^\s?\d/ do
true -> "???, " <> date_str
false -> date_str
end
# httpd_util requires that single digit days have a leading zero. This is not always the case.
day_and_date = date_str
|> String.slice(5..-1)
|> String.lstrip
# add leading zero
date_str =
case day_and_date =~ ~r/^\d\s/ do
true -> String.slice(date_str, 0..4) <> "0" <> day_and_date
false -> date_str
end
date_char_list = to_char_list(date_str)
{{year, month, day}, {hour, minutes, seconds}} = :httpd_util.convert_request_date(date_char_list)
zero_pad(year, 4) <> zero_pad(month) <> zero_pad(day) <> "_" <> zero_pad(hour) <> zero_pad(minutes) <> zero_pad(seconds)
end
end