Is there a way of parsing timezone abbreviations (like EST
or MDT
) to a timezone offset (e.g. -5 hours or -7 hours) in Rust? The chrono_tz crate almost seems to do this, but not quite.

- 388,571
- 95
- 1,107
- 1,366

- 6,859
- 10
- 51
- 71
-
You can ckeck it out on [crates.io](https://crates.io/search?q=timezone&sort=downloads) – Akiner Alkan May 09 '19 at 04:57
-
2Keep in mind that time zone abbreviations are not standardized (Hawaii uses both HST or HAST), nor are they unique (CST could belong to US Central Standard Time, Cuba Standard Time, or China Standard TIme), nor do all time zones *have* abbreviations (many zones in the tzdb zones will just show abbreviations as offsets like "+02"). Also some languages use different abbreviations than we might use in English - example EST in English or HNE in French are both used in Canada for the same zone. – Matt Johnson-Pint May 09 '19 at 17:06
-
Thanks @MattJohnson - harmic below mentions that they aren't well standardized. I didn't know that - I guess I would have if I had gone looking for them in ISO 8601 or similar. It's annoying that the protocol that I'm supporting uses them rather than a numerical offset. – Ted Middleton May 09 '19 at 17:34
-
1There are *some* standards that use abbreviations, such as [RFC 2822's `obs-zone` definition](https://tools.ietf.org/html/rfc2822#section-4.3). Though even there they are "obsolete" because they were originally defined in the earlier RFC 822 before such conflicts were well understood. – Matt Johnson-Pint May 09 '19 at 18:21
1 Answers
You can use chrono_tz, by subtracting the time in the requested timezone from the same time at UTC:
use chrono::TimeZone;
use chrono_tz::Tz;
use chrono_tz::UTC;
fn main() {
let tz: Tz = "Australia/Melbourne".parse().unwrap();
let dt = tz.ymd(2019, 05, 09).and_hms(12, 0, 0);
let utc = UTC.ymd(2019, 05, 09).and_hms(12, 0, 0);
let offset = utc - dt;
println!("offset = UTC{:+02}:{:02}", offset.num_hours(), offset.num_minutes() % 60);
}
The result is a Duration from which you can extract hours, minutes, etc. My sample above gives output:
offset = UTC+10:00
Note that the time zones supported by chrono-tz
(which are derived from the IANA TZ database) do not describe fixed offsets. Rather the database contains a set of rules describing the daylight savings changeover times for each time zone. Therefore you can only accurately obtain the offset by supplying the time at which you would like to know the offset (in my example, it was 2019-05-09 12:00:00).
There are also a set of abbreviations for fixed offsets from UTC. These are not well standardized, and in fact the same abbreviation can mean different things in different countries. For these you may be better off making a simple lookup table for the set of abbreviations you want to support.

- 28,606
- 5
- 67
- 91
-
2Thanks @harmic - I knew about chrono-tz, but unfortunately it's those abbreviations that I need to deal with. I wasn't completely certain of their provenance or level of standardization - it's disappointing that the protocol I have to support uses them. I'm going to take your advice and just use a local lookup on those. – Ted Middleton May 09 '19 at 17:04