Skip to content
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
99 changes: 97 additions & 2 deletions src/DateFormat/Language.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DateFormat.Language exposing
( Language
, english, spanish, dutch, swedish, portuguese
, english, spanish, dutch, swedish, portuguese, finnish
)

{-|
Expand All @@ -22,7 +22,7 @@ This module exposes `Language`, along with a few implementations.

### Languages

@docs english, spanish, dutch, swedish, portuguese
@docs english, spanish, dutch, swedish, portuguese, finnish

-}

Expand Down Expand Up @@ -494,3 +494,98 @@ toPortugueseWeekdayName weekday =

Sun ->
"Domingo"



-- Finnish


toFinnishMonthName : Month -> String
toFinnishMonthName month =
case month of
Jan ->
"tammikuu"

Feb ->
"helmikuu"

Mar ->
"maaliskuu"

Apr ->
"huhtikuu"

May ->
"toukokuu"

Jun ->
"kesäkuu"

Jul ->
"heinäkuu"

Aug ->
"elokuu"

Sep ->
"syyskuu"

Oct ->
"lokakuu"

Nov ->
"marraskuu"

Dec ->
"joulukuu"


toFinnishWeekdayName : Weekday -> String
toFinnishWeekdayName weekday =
case weekday of
Mon ->
"maanantai"

Tue ->
"tiistai"

Wed ->
"keskiviikko"

Thu ->
"torstai"

Fri ->
"perjantai"

Sat ->
"lauantai"

Sun ->
"sunnuntai"


{-| Only 24h formats are used in the Finnish language. AM/PM do technically have translations,
but using them would go against the [guidelines](http://www.kielitoimistonohjepankki.fi/ohje/51).
-}
toFinnishAmPm : Int -> String
toFinnishAmPm _ =
""


toFinnishSuffix : Int -> String
toFinnishSuffix _ =
"."


{-| The Finnish language!
-}
finnish : Language
finnish =
Language
toFinnishMonthName
(toFinnishMonthName >> String.replace "kuu" "")
toFinnishWeekdayName
(toFinnishWeekdayName >> String.left 2)
toFinnishAmPm
toFinnishSuffix
186 changes: 7 additions & 179 deletions src/DateFormat/Relative.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module DateFormat.Relative exposing (relativeTime, relativeTimeWithOptions, RelativeTimeOptions, defaultRelativeOptions)
module DateFormat.Relative exposing (relativeTime, relativeTimeWithOptions)

{-| A reliable way to get a pretty message for the relative time difference between two dates.


# Getting relative time for two dates

@docs relativeTime, relativeTimeWithOptions, RelativeTimeOptions, defaultRelativeOptions
@docs relativeTime, relativeTimeWithOptions

-}

import DateFormat.Relative.Language exposing (RelativeTimeOptions)
import Time exposing (Month(..), Posix, Weekday(..), Zone, utc)


Expand All @@ -26,7 +27,6 @@ Here are a few examples to help:

relativeTime now oneHundredDaysFromNow == "in 100 days"


-- Order matters!
relativeTime now tenSecondsAgo == "just now"

Expand All @@ -35,14 +35,15 @@ Here are a few examples to help:
-}
relativeTime : Posix -> Posix -> String
relativeTime =
relativeTimeWithOptions defaultRelativeOptions
relativeTimeWithOptions DateFormat.Relative.Language.english


{-| Maybe `relativeTime` is too lame. (Or maybe you speak a different language than English!)

With `relativeTimeWithOptions`, you can provide your own custom messages for each time range.
With `relativeTimeWithOptions`, you can use one of the predefined options in
`DateFormat.Relative.Language` or even provide your own custom messages for each time range.

(That's what `relativeTime` uses under the hood!)
That's what `relativeTime` does: it uses the `english` options under the hood!

You can provide a set of your own custom options, and use `relativeTimeWithOptions` instead.

Expand Down Expand Up @@ -78,66 +79,6 @@ relativeTimeWithOptions options start end =
options.inSomeYears


{-| Options for configuring your own relative message formats!

For example, here is how `someSecondsAgo` is implemented by default:

defaultSomeSecondsAgo : Int -> String
defaultSomeSecondsAgo seconds =
if seconds < 30 then
"just now"

else
toString seconds ++ " seconds ago"

And here is how `inSomeHours` might look:

defaultInSomeHours : Int -> String
defaultInSomeHours hours =
if hours < 2 then
"in an hour"

else
"in " ++ toString hours ++ " hours"

-}
type alias RelativeTimeOptions =
{ someSecondsAgo : Int -> String
, someMinutesAgo : Int -> String
, someHoursAgo : Int -> String
, someDaysAgo : Int -> String
, someMonthsAgo : Int -> String
, someYearsAgo : Int -> String
, rightNow : String
, inSomeSeconds : Int -> String
, inSomeMinutes : Int -> String
, inSomeHours : Int -> String
, inSomeDays : Int -> String
, inSomeMonths : Int -> String
, inSomeYears : Int -> String
}


{-| If there is something you'd like to tweak based off of the defaults, this record might be a good starting point!
-}
defaultRelativeOptions : RelativeTimeOptions
defaultRelativeOptions =
{ someSecondsAgo = defaultSomeSecondsAgo
, someMinutesAgo = defaultSomeMinutesAgo
, someHoursAgo = defaultSomeHoursAgo
, someDaysAgo = defaultSomeDaysAgo
, someMonthsAgo = defaultSomeMonthsAgo
, someYearsAgo = defaultSomeYearsAgo
, rightNow = defaultRightNow
, inSomeSeconds = defaultInSomeSeconds
, inSomeMinutes = defaultInSomeMinutes
, inSomeHours = defaultInSomeHours
, inSomeDays = defaultInSomeDays
, inSomeMonths = defaultInSomeMonths
, inSomeYears = defaultInSomeYears
}


toMilliseconds : Posix -> Int
toMilliseconds =
Time.posixToMillis
Expand Down Expand Up @@ -188,116 +129,3 @@ relativeTimeWithFunctions zone millis functions =

else
functions.years <| (days // 365)


defaultRightNow : String
defaultRightNow =
"right now"


defaultSomeSecondsAgo : Int -> String
defaultSomeSecondsAgo seconds =
if seconds < 30 then
"just now"

else
String.fromInt seconds ++ " seconds ago"


defaultSomeMinutesAgo : Int -> String
defaultSomeMinutesAgo minutes =
if minutes < 2 then
"a minute ago"

else
String.fromInt minutes ++ " minutes ago"


defaultSomeHoursAgo : Int -> String
defaultSomeHoursAgo hours =
if hours < 2 then
"an hour ago"

else
String.fromInt hours ++ " hours ago"


defaultSomeDaysAgo : Int -> String
defaultSomeDaysAgo days =
if days < 2 then
"yesterday"

else
String.fromInt days ++ " days ago"


defaultSomeMonthsAgo : Int -> String
defaultSomeMonthsAgo months =
if months < 2 then
"last month"

else
String.fromInt months ++ " months ago"


defaultSomeYearsAgo : Int -> String
defaultSomeYearsAgo years =
if years < 2 then
"last year"

else
String.fromInt years ++ " years ago"


defaultInSomeSeconds : Int -> String
defaultInSomeSeconds seconds =
if seconds < 30 then
"in a few seconds"

else
"in " ++ String.fromInt seconds ++ " seconds"


defaultInSomeMinutes : Int -> String
defaultInSomeMinutes minutes =
if minutes < 2 then
"in a minute"

else
"in " ++ String.fromInt minutes ++ " minutes"


defaultInSomeHours : Int -> String
defaultInSomeHours hours =
if hours < 2 then
"in an hour"

else
"in " ++ String.fromInt hours ++ " hours"


defaultInSomeDays : Int -> String
defaultInSomeDays days =
if days < 2 then
"tomorrow"

else
"in " ++ String.fromInt days ++ " days"


defaultInSomeMonths : Int -> String
defaultInSomeMonths months =
if months < 2 then
"in a month"

else
"in " ++ String.fromInt months ++ " months"


defaultInSomeYears : Int -> String
defaultInSomeYears years =
if years < 2 then
"in a year"

else
"in " ++ String.fromInt years ++ " years"
Loading