LocalTime to UTC in Elm

Nicola De Filippo
2 min readJul 10, 2020

In my last application i need a function to transform the local time in a utc time.

At start point i have a utc string of the day of interest and the local time that i want transform in utc time, example “08–07–2020T:12:12:00.000Z” “14:30” and i’d like have 2020–01–01T13:30:00.000Z.

To do this i used this elm package:

"justinmimbs/time-extra"
"justinmimbs/timezone-data"
"rtfeldman/elm-iso8601-date-strings"

The code:

localToUTC : String -> String -> Zone -> String
localToUTC utcTime slot zone =
let
result = Iso8601.toTime utcTime
hourPlusMinutes = String.split ":" slot
hour = case (List.head hourPlusMinutes) of
Just a ->
case String.toInt a of
Just h ->
h
Nothing ->
0
Nothing ->
0
minutes = case List.head (List.drop 1 hourPlusMinutes) of
Just a ->
case String.toInt a of
Just m ->
m
Nothing ->
0
Nothing ->
0
romeDay = case result of
Ok value ->
toDay zone value
Err msg ->
1
romeMonth = case result of
Ok value ->
toMonth zone value
Err msg ->
Jan
romeYear = case result of
Ok value ->
(toYear zone value)
Err msg ->
2020
from = Iso8601.fromTime (partsToPosix rome (Parts romeYear romeMonth romeDay hour minutes 0 0))
in
from

Look the declaration, the utcTime is something utc time of the day of interest, slot is the time in local (HH:mm) and Zone is the timezone.

At first we transform the utc time in time and split the slot in hour and minutes. After we get the day, month and year of the time. At the end we create the utc time from the posix time that we create with the single parts of the datetime.
I hope that this can be helpful to developer that need to play with Date and Time in Elm. Here you can run the code https://ellie-app.com/9nwQB2gjyFGa1.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Nicola De Filippo
Nicola De Filippo

Written by Nicola De Filippo

Software Engineer and Entrepreneur

No responses yet

Write a response