on 4bec310 help sign in
elk/libraries/Time.elm
99 lines 2.8 kB history raw
1module Time where
3{-| Library for working with time.
5# Units
6@docs Time, millisecond, second, minute, hour,
7 inMilliseconds, inSeconds, inMinutes, inHours
9# Tickers
10@docs fps, fpsWhen, every
12# Timing
13@docs timestamp, delay, since
15-}
17import Basics (..)
18import Native.Time
19import Signal (Signal)
21{-| Type alias to make it clearer when you are working with time values.
22Using the `Time` constants instead of raw numbers is very highly recommended.
23-}
24type Time = Float
26{-| Units of time, making it easier to specify things like a half-second
27`(500 * milliseconds)` without remembering Elm’s underlying units of time.
28-}
29millisecond : Time
30millisecond = 1
32second : Time
33second = 1000 * millisecond
35minute : Time
36minute = 60 * second
38hour : Time
39hour = 60 * minute
41inMilliseconds : Time -> Float
42inMilliseconds t = t
44inSeconds : Time -> Float
45inSeconds t = t / second
47inMinutes : Time -> Float
48inMinutes t = t / minute
50inHours : Time -> Float
51inHours t = t / hour
53{-| Takes desired number of frames per second (fps). The resulting signal
54gives a sequence of time deltas as quickly as possible until it reaches
55the desired FPS. A time delta is the time between the last frame and the
56current frame.
57-}
58fps : number -> Signal Time
59fps = Native.Time.fps
61{-| Same as the fps function, but you can turn it on and off. Allows you
62to do brief animations based on user input without major inefficiencies.
63The first time delta after a pause is always zero, no matter how long
64the pause was. This way summing the deltas will actually give the amount
65of time that the output signal has been running.
66-}
67fpsWhen : number -> Signal Bool -> Signal Time
68fpsWhen = Native.Time.fpsWhen
70{-| Takes a time interval t. The resulting signal is the current time, updated
71every t.
72-}
73every : Time -> Signal Time
74every = Native.Time.every
76{-| Takes a time `t` and any signal. The resulting boolean signal is true for
77time `t` after every event on the input signal. So ``(second `since`
78Mouse.clicks)`` would result in a signal that is true for one second after
79each mouse click and false otherwise.
80-}
81since : Time -> Signal a -> Signal Bool
82since = Native.Time.since
84{-| Add a timestamp to any signal. Timestamps increase monotonically. When you
85create `(timestamp Mouse.x)`, an initial timestamp is produced. The timestamp
86updates whenever `Mouse.x` updates.
88Timestamp updates are tied to individual events, so
89`(timestamp Mouse.x)` and `(timestamp Mouse.y)` will always have the same
90timestamp because they rely on the same underlying event (`Mouse.position`).
91-}
92timestamp : Signal a -> Signal (Time, a)
93timestamp = Native.Time.timestamp
95{-| Delay a signal by a certain amount of time. So `(delay second Mouse.clicks)`
96will update one second later than any mouse click.
97-}
98delay : Time -> Signal a -> Signal a
99delay = Native.Time.delay