Add time module with to_duration_string
Which turn a float into a `mm:ss` string
This commit is contained in:
23
src/musicplayer/time/time.gleam
Normal file
23
src/musicplayer/time/time.gleam
Normal file
@@ -0,0 +1,23 @@
|
||||
import gleam/float
|
||||
import gleam/int
|
||||
import gleam/string
|
||||
|
||||
pub fn to_duration_string(seconds: Float) -> String {
|
||||
let total =
|
||||
seconds
|
||||
|> float.max(0.0)
|
||||
|> float.truncate
|
||||
|
||||
let minutes = total / 60
|
||||
let seconds = total % 60
|
||||
|
||||
let mins =
|
||||
int.to_string(minutes)
|
||||
|> string.pad_start(to: 2, with: "0")
|
||||
|
||||
let secs =
|
||||
int.to_string(seconds)
|
||||
|> string.pad_start(to: 2, with: "0")
|
||||
|
||||
mins <> ":" <> secs
|
||||
}
|
||||
Reference in New Issue
Block a user