Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User friendly timezone names from tz database format

Tags:

timezone

I have timezones in the following array format:

'America/New_York' => '(GMT-05:00) Eastern Time (US & Canada)',
'Europe/Lisbon' => '(GMT) Greenwich Mean Time : Lisbon',

etc.

How do I go about displaying a user-friendly summer/daylight savings time dependent timezone identifier to the user?

For example, displaying the time now in New York would append "(EDT)" to the time, which would make sense for local users. I want to avoid having to display ((GMT-05:00) Eastern Time (US & Canada)) or just (GMT-05:00), which isn't strictly accurate all year round.

Ideally then, is there a web service/database that can take a tz string in the format "America/New_York", and a timestamp as paramters and return the abbreviation in the formats here?

like image 228
bcmcfc Avatar asked Oct 28 '25 00:10

bcmcfc


1 Answers

strftime's %Z format specifier gives you this abbreviation. You didn't say what programming language you are using, but most programming languages give you access to strftime in one way or another.

Python:

import pytz
from datetime import datetime

here = pytz.timezone('Asia/Tokyo')
print here.localize(datetime.utcnow()).strftime("%Z")

there = pytz.timezone('America/Montreal')
print there.localize(datetime.utcnow()).strftime("%Z")

PHP:

date_default_timezone_set("Asia/Tokyo");
echo strftime("%Z");

date_default_timezone_set("America/Montreal");
echo strftime("%Z");
like image 137
Celada Avatar answered Oct 29 '25 15:10

Celada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!