-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f9540c
commit cad8bd0
Showing
7 changed files
with
206 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Main exposing (..) | ||
|
||
import Browser exposing (staticPage) | ||
import DateFormat | ||
import Html exposing (text) | ||
import Time exposing (Posix, Zone, utc) | ||
|
||
|
||
|
||
-- Let's create a custom formatter we can use later: | ||
|
||
|
||
ourFormatter : Zone -> Posix -> String | ||
ourFormatter = | ||
DateFormat.format | ||
[ DateFormat.monthNameFull | ||
, DateFormat.text " " | ||
, DateFormat.dayOfMonthSuffix | ||
, DateFormat.text ", " | ||
, DateFormat.yearNumber | ||
] | ||
|
||
|
||
|
||
-- With our formatter, we can format any date as a string! | ||
|
||
|
||
ourTimezone : Zone | ||
ourTimezone = | ||
utc | ||
|
||
|
||
|
||
-- 2018-05-20T19:18:24.911Z | ||
|
||
|
||
ourPosixTime : Posix | ||
ourPosixTime = | ||
Time.millisToPosix 1526843861289 | ||
|
||
|
||
ourPrettyDate : String | ||
ourPrettyDate = | ||
ourFormatter ourTimezone ourPosixTime | ||
|
||
|
||
|
||
-- Show on the screen | ||
|
||
|
||
main = | ||
staticPage (text ourPrettyDate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Main exposing (..) | ||
|
||
import Browser | ||
import DateFormat.Relative as Relative | ||
import Html exposing (..) | ||
import Task | ||
import Time exposing (Posix, Zone, utc) | ||
|
||
|
||
main = | ||
Browser.embed | ||
{ init = init | ||
, view = view | ||
, update = update | ||
, subscriptions = subscriptions | ||
} | ||
|
||
|
||
type alias Model = | ||
{ timeSinceExample : Posix | ||
, initialTime : Maybe Posix | ||
, now : Maybe Posix | ||
} | ||
|
||
|
||
init : () -> ( Model, Cmd Msg ) | ||
init _ = | ||
( Model | ||
(Time.millisToPosix 1526852818792) | ||
Nothing | ||
Nothing | ||
, Task.perform SetInitialTime Time.now | ||
) | ||
|
||
|
||
view : Model -> Html Msg | ||
view model = | ||
div [] | ||
[ h3 [] [ text "Model" ] | ||
, p [] [ text <| Debug.toString model ] | ||
, case ( model.initialTime, model.now ) of | ||
( Just initialTime, Just now ) -> | ||
p [] | ||
[ strong [] [ text "Time since you loaded this page: " ] | ||
, span [] [ text <| Relative.relativeTime now initialTime ] | ||
] | ||
|
||
( _, _ ) -> | ||
p [] [ text "Getting times..." ] | ||
, case model.now of | ||
Just now -> | ||
p [] | ||
[ strong [] [ text "Time since Ryan wrote this example: " ] | ||
, span [] [ text <| Relative.relativeTime now model.timeSinceExample ] | ||
] | ||
|
||
Nothing -> | ||
p [] [ text "Getting now time..." ] | ||
] | ||
|
||
|
||
type Msg | ||
= SetInitialTime Posix | ||
| SetNow Posix | ||
|
||
|
||
update : Msg -> Model -> ( Model, Cmd Msg ) | ||
update msg model = | ||
case msg of | ||
SetInitialTime initialTime -> | ||
( { model | ||
| initialTime = Just initialTime | ||
, now = Just initialTime | ||
} | ||
, Cmd.none | ||
) | ||
|
||
SetNow now -> | ||
( { model | now = Just now }, Cmd.none ) | ||
|
||
|
||
subscriptions : Model -> Sub Msg | ||
subscriptions model = | ||
Time.every 1000 SetNow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"type": "application", | ||
"source-directories": [ | ||
".", | ||
"../src" | ||
], | ||
"elm-version": "0.19.0", | ||
"dependencies": { | ||
"elm-lang/browser": "1.0.0", | ||
"elm-lang/core": "6.0.0", | ||
"elm-lang/html": "3.0.0", | ||
"elm-lang/json": "1.0.0", | ||
"elm-lang/time": "1.0.0" | ||
}, | ||
"test-dependencies": {}, | ||
"do-not-edit-this-by-hand": { | ||
"transitive-dependencies": { | ||
"elm-lang/url": "1.0.0", | ||
"elm-lang/virtual-dom": "3.0.0" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters