1- # timezone
1+ # tzif
22
33<!-- #
44[](https://hex.pm/packages/timezone)
55[](https://hexdocs.pm/timezone/)
66-->
77
8- Timezone support for Gleam time using the operating system's timezone database .
9- This package loads the timezone database from the standard location
8+ Time zone support for Gleam time using the IANA Time Zone Database .
9+ This package loads the time zone database from the standard location
1010(` /usr/share/zoneinfo ` ) on MacOS and Linux computers. It includes a parser for
1111the Time Zone Information Format (TZif) or ` tzfile ` format, as well as utility
1212functions to convert a timestamp from the
1313[ gleam_time] ( https://hexdocs.pm/gleam_time/ ) library into a date and time
14- of day in the given timezone .
14+ of day in the given time zone .
1515
1616> We could really do with a timezone database package with a
1717> fn(Timestamp, Zone) -> #(Date, TimeOfDay) function
@@ -21,13 +21,61 @@ of day in the given timezone.
2121To use, add the following entry in your ` gleam.toml ` file dependencies:
2222
2323```
24- timezone = { git = "[email protected] :devries/timezone.git", ref = "main" }24+ tzif = { git = "[email protected] :devries/timezone.git", ref = "main" }2525```
26+
27+ # Using the Package
28+ The most straightforward use would be to load the database from the default
29+ location on the operating system, and then obtain a timestamp using the
30+ [ gleam_time] ( https://hexdocs.pm/gleam_time/ ) package, and convert that timestamp
31+ into a time of day in a time zone using the IANA time zone name. An example
32+ of that is shown in the code below.
33+
34+ ``` gleam
35+ import gleam/int
36+ import gleam/io
37+ import gleam/string
38+ import gleam/time/timestamp
39+ import tzif/database
40+ import tzif/tzcalendar
41+
42+ pub fn main() {
43+ let now = timestamp.system_time()
44+
45+ // Load the database from the operating system
46+ let db = database.load_from_os()
47+
48+ case tzcalendar.get_time_and_zone(now, "America/New_York", db) {
49+ Ok(time_and_zone) -> {
50+ // Successfully converted time to the requested time zone
51+ io.println(
52+ int.to_string(time_and_zone.time_of_day.hours)
53+ |> string.pad_start(2, "0")
54+ <> ":"
55+ <> int.to_string(time_and_zone.time_of_day.minutes)
56+ |> string.pad_start(2, "0")
57+ <> ":"
58+ <> int.to_string(time_and_zone.time_of_day.seconds)
59+ |> string.pad_start(2, "0")
60+ <> " "
61+ <> time_and_zone.designation
62+ )
63+ }
64+ Error(database.ZoneNotFound) -> io.println("Time zone not found")
65+ Error(database.ProcessingError) -> io.println("Error processing time zone conversion")
66+ }
67+ }
68+ ```
69+ If you are on windows and have installed the IANA Time Zone Database, or want
70+ to use a custom version you can use the ` database.load_from_path ` function
71+ instead of the ` database.load_from_os ` function to specify a path to your
72+ database files.
73+
2674# Installing the zoneinfo data files
27- Timezone information is frequently updated, therefore it makes sense to use the
28- package manager for your operating system to keep the timezone database up to
29- date. All common unix variants have timezone database packages and install the
30- timezone database files into the ` /usr/share/zoneinfo ` directory by default.
75+ Time zone information is frequently updated, therefore it makes sense to use the
76+ package manager for your operating system to keep the time zone database up to
77+ date. All common unix variants have time zone database packages and install the
78+ time zone database files into the ` /usr/share/zoneinfo ` directory by default.
3179
3280## MacOS
3381The files should be included in your operating system by default. Check the
@@ -42,7 +90,7 @@ sudo apt install tzdata
4290```
4391
4492### Debian based docker containers
45- Installing and configuring the timezone database on a Debian or Ubuntu based
93+ Installing and configuring the time zone database on a Debian or Ubuntu based
4694docker container can be done by adding the following to your ` Dockerfile ` :
4795
4896```
@@ -58,14 +106,14 @@ RUN apt-get update && \
58106```
59107
60108## Alpine Linux Systems
61- The Alpine Package Keeper can install the timezone database using the command:
109+ The Alpine Package Keeper can install the time zone database using the command:
62110
63111```
64112sudo apk add tzdata
65113```
66114
67115### Alpine based docker containers
68- Installing and configuring the timezone database on an Alpine based docker
116+ Installing and configuring the time zone database on an Alpine based docker
69117container can be done by adding the following to your ` Dockerfile ` :
70118
71119```
@@ -81,7 +129,7 @@ RUN apk add --no-cache tzdata && \
81129```
82130
83131## Red Hat/Rocky/Alma Linux Systems
84- You can use the YUM package manager or DNF to install the timezone database
132+ You can use the YUM package manager or DNF to install the time zone database
85133on Red Hat variants. To use YUM run the command:
86134
87135```
@@ -94,4 +142,7 @@ Similarly, using DNF:
94142sudo dnf install tzdata
95143```
96144## Windows
97- At this time we have not tested the windows operating system.
145+ Microsoft Windows has a different mechanism for handling time zones, however
146+ you can install the IANA Time Zone Database by [ downloading the latest
147+ version] ( https://www.iana.org/time-zones ) and compiling the zone files using
148+ [ the directions in the repository] ( https://data.iana.org/time-zones/tz-link.html ) .
0 commit comments