Current section
Files
Jump to
Current section
Files
src/tempo/month.gleam
import gleam/result
import tempo
/// Returns a month's short name.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.to_short_string
/// // -> "Jun"
/// ```
pub fn to_short_string(month: tempo.Month) -> String {
case month {
tempo.Jan -> "Jan"
tempo.Feb -> "Feb"
tempo.Mar -> "Mar"
tempo.Apr -> "Apr"
tempo.May -> "May"
tempo.Jun -> "Jun"
tempo.Jul -> "Jul"
tempo.Aug -> "Aug"
tempo.Sep -> "Sep"
tempo.Oct -> "Oct"
tempo.Nov -> "Nov"
tempo.Dec -> "Dec"
}
}
/// Returns a month's long name.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.to_short_string
/// // -> "June"
/// ```
pub fn to_long_string(month: tempo.Month) -> String {
case month {
tempo.Jan -> "January"
tempo.Feb -> "February"
tempo.Mar -> "March"
tempo.Apr -> "April"
tempo.May -> "May"
tempo.Jun -> "June"
tempo.Jul -> "July"
tempo.Aug -> "August"
tempo.Sep -> "September"
tempo.Oct -> "October"
tempo.Nov -> "November"
tempo.Dec -> "December"
}
}
/// Gets a month from a month string.
///
/// ## Example
///
/// ```gleam
/// month.from_string("Jun")
/// // -> Ok(tempo.Jun)
/// ```
///
/// ```gleam
/// month.from_string("June")
/// // -> Ok(tempo.Jun)
/// ```
///
/// ```gleam
/// month.from_string("Hello")
/// // -> Error(Nil)
/// ```
pub fn from_string(month: String) -> Result(tempo.Month, tempo.Error) {
from_short_string(month)
|> result.try_recover(fn(_) { from_long_string(month) })
}
/// Gets a month from a short month string.
///
/// ## Example
///
/// ```gleam
/// month.from_short_string("Jun")
/// // -> Ok(tempo.Jun)
/// ```
///
/// ```gleam
/// month.from_short_string("June")
/// // -> Error(Nil)
/// ```
pub fn from_short_string(month: String) -> Result(tempo.Month, tempo.Error) {
tempo.month_from_short_string(month)
}
/// Gets a month from a long month string.
///
/// ## Example
///
/// ```gleam
/// month.from_long_string("June")
/// // -> Ok(tempo.Jun)
/// ```
///
/// ```gleam
/// month.from_long_string("Jun")
/// // -> Error(Nil)
/// ```
pub fn from_long_string(month: String) -> Result(tempo.Month, tempo.Error) {
tempo.month_from_long_string(month)
}
/// Returns a month's number on the civil calendar.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.to_int
/// // -> 6
/// ```
pub fn to_int(month: tempo.Month) -> Int {
tempo.month_to_int(month)
}
/// Gets a month from an integer representing the order of the month on the
/// civil calendar.
///
/// ## Example
///
/// ```gleam
/// month.from_int(6)
/// // -> Ok(tempo.Jun)
/// ```
///
/// ```gleam
/// month.from_int(13)
/// // -> Error(Nil)
/// ```
pub fn from_int(month: Int) -> Result(tempo.Month, tempo.Error) {
tempo.month_from_int(month)
}
/// Returns the number of days in a month.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.days
/// // -> 30
/// ```
///
/// ```gleam
/// date.literal("2024-12-03")
/// |> date.get_month
/// |> month.days
/// // -> 31
/// ```
pub fn days(of month: tempo.Month, in year: Int) -> Int {
tempo.days_of_month(month, in: year)
}
/// Returns the next month in the civil calendar.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.next
/// // -> tempo.Jul
/// ```
///
/// ```gleam
/// date.literal("2024-12-03")
/// |> date.get_month
/// |> month.next
/// // -> tempo.Jan
/// ```
pub fn next(month: tempo.Month) -> tempo.Month {
case month {
tempo.Jan -> tempo.Feb
tempo.Feb -> tempo.Mar
tempo.Mar -> tempo.Apr
tempo.Apr -> tempo.May
tempo.May -> tempo.Jun
tempo.Jun -> tempo.Jul
tempo.Jul -> tempo.Aug
tempo.Aug -> tempo.Sep
tempo.Sep -> tempo.Oct
tempo.Oct -> tempo.Nov
tempo.Nov -> tempo.Dec
tempo.Dec -> tempo.Jan
}
}
/// Returns the previous month in the civil calendar.
///
/// ## Example
///
/// ```gleam
/// date.literal("2024-06-13")
/// |> date.get_month
/// |> month.prior
/// // -> tempo.May
/// ```
///
/// ```gleam
/// date.literal("2024-01-03")
/// |> date.get_month
/// |> month.prior
/// // -> tempo.Dec
/// ```
pub fn prior(month: tempo.Month) -> tempo.Month {
case month {
tempo.Jan -> tempo.Dec
tempo.Feb -> tempo.Jan
tempo.Mar -> tempo.Feb
tempo.Apr -> tempo.Mar
tempo.May -> tempo.Apr
tempo.Jun -> tempo.May
tempo.Jul -> tempo.Jun
tempo.Aug -> tempo.Jul
tempo.Sep -> tempo.Aug
tempo.Oct -> tempo.Sep
tempo.Nov -> tempo.Oct
tempo.Dec -> tempo.Nov
}
}