# How to get a zone name from the Time package?

**URL:** <https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180>\
**Category:** Learn\
**Created:** [October 10, 2018, 8:40pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180 "2018-10-10T20:40:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![waratuman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/waratuman/32/710_2.png) [@waratuman](https://discourse.elm-lang.org/u/waratuman)\
**Post date:** [October 10, 2018, 8:40pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180/1 "2018-10-10T20:40:11Z")

</div>

When working with the Time package I’m able to use [justinmimbs/timezone-data](https://package.elm-lang.org/packages/justinmimbs/timezone-data/latest/) to load timezones. My question is given a `Zone` from the Time package how do I get the name of the zone?

For example:

```
import Time
import Time.TimeZones

zone = Time.TimeZones.america_new_york ()

```

How do get “America/New\_York” from that zone? I’m looking for a function with the signature `Zone -> String` but can’t seem to find one. Or perhaps it just isn’t possible yet?

---

<div class="post-metadata">

**Author:** ![justinmimbs](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/justinmimbs/32/48_2.png) [@justinmimbs](https://discourse.elm-lang.org/u/justinmimbs)\
**Post date:** [October 10, 2018, 9:41pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180/2 "2018-10-10T21:41:43Z")

</div>

The `Zone` from `elm/time` doesn’t store a zone name. But it shouldn’t really need to. In your example you already know the zone name. 😄

The [`TimeZone.getZone`](https://package.elm-lang.org/packages/justinmimbs/timezone-data/latest/TimeZone#getZone) task returns the local zone name with the zone data, so you can store the name too if you’d like.

Is there a case where a `Zone -> String` function would be essential? I can’t think of a way to get a `Zone` without having access to its name.

---

<div class="post-metadata">

**Author:** ![waratuman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/waratuman/32/710_2.png) [@waratuman](https://discourse.elm-lang.org/u/waratuman)\
**Post date:** [October 10, 2018, 9:52pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180/3 "2018-10-10T21:52:40Z")

</div>

Well, in this example I know the name. And I should always know the name. It just a question of storing both the name of the zone itself and the zone data. For example, some JSON is fetched from the server with the zone name. I then pass the Zone around and don’t track the name of it, perhaps updating the value in a view, but then I’m stuck with just the zone data so when I pass the data back to the server I’ve lost track of the name.

In my case I can just use a tuple to track and proxy the functions of the Time library:

```
type alias TimeZone =
    ( String, Zone )

toDay : TimeZone -> Posix -> Int
toDay ( _, z ) =
    Time.toDay z
```

---

<div class="post-metadata">

**Author:** ![allanderek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/allanderek/32/1360_2.png) [@allanderek](https://discourse.elm-lang.org/u/allanderek)\
**Post date:** [October 10, 2018, 10:08pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180/4 "2018-10-10T22:08:39Z")

</div>

There isn’t a `Zone -> String` function, but there is a similar `Task` called simply `getZoneName`:  
[https://package.elm-lang.org/packages/elm/time/latest/Time#getZoneName](https://package.elm-lang.org/packages/elm/time/latest/Time#getZoneName)

You could store time information in a record on your model, something like:

```auto
type alias TimeInfo =
      { zone : Time.Zone
      , zoneName : Time.ZoneName
      }

type alias Model =
    { timeInfo : TimeInfo
    , ...
    }

```

Then you just need to perform two tasks in your init:

```auto
init ...
     let
         initZone =
              { zone = Time.utc
              , zoneName = Time.Offset 0
              }
         timeZoneCommand =
            Task.perform GetTimeZone Time.here

        zoneNameCommand =
            Task.perform GetZoneName Time.getZoneName
    in
    ....

```

Where

```auto
type Msg
type Msg
    = GetTimeZone Time.Zone
    | GetZoneName Time.ZoneName
    ....

```

and the update does the obvious thing of setting the relevant parts of the `timeInfo` on the `Model`.  
Then you just need a function to format the `ZoneName`:

```auto
formatZoneName : Time.ZoneName -> String
formatZoneName zoneName =
    case zoneName of
        Time.Name n ->
            n

        Time.Offset 0 ->
            "UTC"

        Time.Offset o ->
            let
                sign =
                    case o > 0 of
                        True ->
                            "+"

                        False ->
                            "-"

                numMinutes =
                    abs o

                hours =
                    String.fromInt (numMinutes // 60)

                minutes =
                    modBy 60 numMinutes

                minSuffix =
                    case minutes == 0 of
                        True ->
                            ""

                        False ->
                            ":" ++ String.fromInt minutes
            in
            "UTC " ++ sign ++ hours ++ minSuffix

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 20, 2018, 10:08pm UTC](https://discourse.elm-lang.org/t/how-to-get-a-zone-name-from-the-time-package/2180/5 "2018-10-20T22:08:43Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
