Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new directive: day with suffix #463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/format/datetime/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@ defmodule Timex.Format.DateTime.Formatter do
do: pad_numeric(date.day, flags, width)
def format_token(_locale, :oday, date, _modifiers, flags, width),
do: pad_numeric(Timex.day(date), flags, width)
def format_token(_locale, :dsuff, %{:day => day}, _modifiers, _flags, _width) do
remainder = day |> rem(10)
suffix =
cond do
remainder == 1 && day != 11 -> "st"
remainder == 2 && day != 12 -> "nd"
remainder == 3 && day != 13 -> "rd"
true -> "th"
end
"#{day}" <> suffix
end
# Weeks
def format_token(_locale, :iso_weeknum, date, _modifiers, flags, width) do
{_, week} = Timex.iso_week(date)
Expand Down
5 changes: 3 additions & 2 deletions lib/format/datetime/formatters/strftime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule Timex.Format.DateTime.Formatters.Strftime do
* `-` - don't pad numerical results (overrides default padding if any)
* `0` - use zeros for padding
* `_` - use spaces for padding
* `:`, `::` - used only in combination with `%z`; see description of `%:z`
and `%::z` below
* `:`, `::` - used only in combination with `%z` and `%d`; see description of `%:d`,
`%:z` and `%::z` below

`<width>` is a non-negative decimal number specifying the minimum field
width.
Expand Down Expand Up @@ -59,6 +59,7 @@ defmodule Timex.Format.DateTime.Formatters.Strftime do
* `%w` - weekday, Sunday first (0..6)
* `%a` - abbreviated weekday name (Mon..Sun, no padding)
* `%A` - full weekday name (Monday..Sunday, no padding)
* `%:d` - combined day number with english ordinal suffix for the day of the month (1st, 2nd, 3rd, 4th..30th)

### Weeks

Expand Down
6 changes: 6 additions & 0 deletions lib/parse/datetime/parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ defmodule Timex.Parse.DateTime.Parsers do
|> label("month abbreviation")
end

def day_suffix(opts \\ []) do
Helpers.integer(opts)
|> satisfy(fn day -> day >= 1 && day <= 31 end)
|> map(fn n -> [day_suffix: n] end)
|> label("english ordinal suffix for the day of the month, 2 characters")
end
def day_of_month(opts \\ []) do
Helpers.integer(opts)
|> satisfy(fn day -> day >= 1 && day <= 31 end)
Expand Down
2 changes: 1 addition & 1 deletion lib/parse/datetime/tokenizers/directive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Timex.Parse.DateTime.Tokenizers.Directive do
]
@mapped_types [iso_year4: :year4, iso_year2: :year2,
month: :month2, mshort: :month_short, mfull: :month_full,
day: :day_of_month, oday: :day_of_year,
day: :day_of_month, oday: :day_of_year, dsuff: :day_suffix,
iso_weeknum: :week_of_year, week_mon: :week_of_year, week_sun: :week_of_year_sun,
wday_mon: :weekday, wday_sun: :weekday, wdshort: :weekday_short, wdfull: :weekday_full,
min: :minute, sec: :second, sec_fractional: :second_fractional, sec_epoch: :seconds_epoch,
Expand Down
2 changes: 2 additions & 0 deletions lib/parse/datetime/tokenizers/strftime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule Timex.Parse.DateTime.Tokenizers.Strftime do
# Compound
"D", "F", "R", "r", "T", "v"
]),
string(":d"),
string(":z"),
string("::z")
])
Expand Down Expand Up @@ -88,6 +89,7 @@ defmodule Timex.Parse.DateTime.Tokenizers.Strftime do
"d" -> force_width(2, :day, directive, opts)
"e" -> force_width(2, :day, directive, Keyword.merge(opts, flags: Keyword.merge([padding: :spaces], get_in(opts, [:flags]))))
"j" -> force_width(3, :oday, directive, opts)
":d" -> Directive.get(:dsuff, directive, opts)
# Weeks
"V" -> force_width(2, :iso_weeknum, directive, opts)
"W" -> force_width(2, :week_mon, directive, opts)
Expand Down
8 changes: 8 additions & 0 deletions test/format_strftime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ defmodule DateFormatTest.FormatStrftime do
assert { :ok, " 1" } = format(@jan12015, "%_d")
end

test "format %:d" do
assert { :ok, "1st" } = format(@jan12015, "%:d")
assert { :ok, "2nd" } = format(Timex.to_datetime({{2015,1,2}, {0,0,0}}), "%:d")
assert { :ok, "3rd" } = format(Timex.to_datetime({{2015,1,3}, {0,0,0}}), "%:d")
assert { :ok, "4th" } = format(Timex.to_datetime({{2015,1,4}, {0,0,0}}), "%:d")
assert { :ok, "5th" } = format(Timex.to_datetime({{2015,1,5}, {0,0,0}}), "%:d")
end

test "format %e" do
assert { :ok, " 1" } = format(@jan12015, "%e")
assert { :ok, "1" } = format(@jan12015, "%-e")
Expand Down