Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get time zone(s) from abbreviation and country code #289

Closed
skolesnyk opened this issue Jan 13, 2016 · 30 comments
Closed

Get time zone(s) from abbreviation and country code #289

skolesnyk opened this issue Jan 13, 2016 · 30 comments

Comments

@skolesnyk
Copy link

I have an abbreviation (e.g. "PST"), need to get timezone from that. Is that possible with moment-timezone?

@mattjohnsonpint
Copy link
Contributor

And which of the five "CST" time zones should we return? China? Cuba? What about "BST", British Summer Time, or Bangladesh Standard Time?

There are just too many ambiguities, so no, we can't do this reliably. At least not without some other qualifier, such as country code.

@skolesnyk
Copy link
Author

I've got the country code. Is it still possible to use timezone then?

@mattjohnsonpint
Copy link
Contributor

We don't have this capability today, but I'll leave this open as a feature request.

@mattjohnsonpint mattjohnsonpint changed the title Support request: possible to get timezone from abbr? Get time zone(s) from abbreviation and country code Jan 13, 2016
@mattjohnsonpint
Copy link
Contributor

Note, there will still be some ambiguities, such as MST could be either America/Denver, or America/Phoenix. So the API should return an array of time zones, in order of likelyhood.

@skolesnyk
Copy link
Author

@mj1856 , that would good enough! Thanks

@skolesnyk
Copy link
Author

Any ETA on release?

@mattjohnsonpint
Copy link
Contributor

Consider it on the backlog, but I'm not actively working on it.

A PR would be welcome!

@asantos00
Copy link

Maybe this could help
https://github.com/dmfilipenko/timezones.json

@skolesnyk
Copy link
Author

Thanks, but how would it help?

@asantos00
Copy link

It is a complete list of timezones, you can't search for the country code
but you can search for the "timezone code" returning an array of the
matches
On Mar 15, 2016 2:51 PM, "skolesnyk" notifications@github.com wrote:

Thanks, but how would it help?


You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#289 (comment)

@mattjohnsonpint
Copy link
Contributor

@asantos00 - Thanks, but that won't help. That's an outdated list of Windows time zone identifiers from a non-authoritative source, with lots of missing data and invented abbreviations. I can guarantee you any system using that file is going to have problems.

moment-timezone is based on standardized IANA time zones. I recommend you read https://en.wikipedia.org/wiki/Tz_database and http://stackoverflow.com/tags/timezone/info

@asantos00
Copy link

@mj1856 oh, yeah, you're right, sorry for the bad suggestion!

@adgrace
Copy link

adgrace commented Jul 19, 2016

Is this feature still needed? This would be my first pull request but I would like to give it a go?

@skolesnyk
Copy link
Author

@alexgrace95 yes, it is needed!

@adgrace
Copy link

adgrace commented Jul 19, 2016

Great I can do this and it seems a nice first pull request project.
So to confirm my plan:
• I will map ISO Country Codes and telephone codes to the relevent country to work out the local time. It will return an array of current times in that country with only key value pair for the UK for example which has 1 time zone but many key value pairs for the US that has 3 time zones, will return either every city in that countries time zone or the abbreviation and the time zone such as CST, PST & EST.
Does this fit your requirement @skolesnyk? And how does it sound @mj1856 ?
Please bare with me as it is my first pull request and I will do my best job.
After this another feature I may add after this one that would be cool is the ability to enter a geotag and the function return the local time.

@skolesnyk
Copy link
Author

@alexgrace95 yes, it does completely.

@mimckenna
Copy link

And for telephone country codes, we have NANP countries (North American Numbering Plan) that all use +1 as their country code. +1 is used by all of US, Canada, Caribbean, and others ranging from Guam (UTC +10:00) to Nova Scotia (UTC -03:00) .

Perhaps you can include NANP entries matching to the four-digit codes. E.g. Jamaica would be +1876. For just +1, you'll need to list an array with all countries.

@maggiepint
Copy link
Member

I would be curious to hear if @mj1856 thinks there's a right way to do this within the library since timezones are his thing. That said, @icambron and I kinda think it would be better implemented as a plugin, given the level of complexity and the number of approaches that could be taken.

@mattjohnsonpint
Copy link
Contributor

@alexgrace95 - Thanks for your enthusiasm, but much of what you propose is out of scope. Telephone codes and geotags are not something that belong with this functionality. If one needs to map telephone code to country code, they can do that separately, and then later deal with time zone.

Also, please don't go and build a spreadsheet of data to send a PR. The problem with building any mappings manually, is that they have to not only be painstakingly researched, but they also have to be maintained over time. Things change, and sometimes opinions influence the data.

A library like moment can really only use data from either IANA or CLDR sources. In this particular case, it would be sufficient to use the country data for the limit subset of zones in the zone.tab file that is part of the IANA sources. We don't really need to get into CLDR for this one item.

Since we're already using IANA data, it's really just a matter of adding a country-code element to the time zone data during the build process, and exposing some functions to use that field along with the existing zone and abbreviation data. I think this would be small enough to do without introducing another plugin. It's not really all that complex.

@adgrace
Copy link

adgrace commented Jul 22, 2016

@mj1856 - Thanks for your description and feedback, it all makes sense. Are you happy for me to do a PR to implement using country codes from the IANA data?

@mattjohnsonpint
Copy link
Contributor

That would be greatly appreciated!

Take a look at the packed and unpacked formats in the docs. Essentially, you'd be adding a country parameter to the unpacked form. For the packed form, which is pipe-delimited, place it at the end of the string please (for compatibility reasons).

@mattjohnsonpint
Copy link
Contributor

Also, I see that we're already extracting the countries from zone.tab and placing it in the "meta" files here: https://github.com/moment/moment-timezone/tree/develop/data/meta

I'm not sure how we're using those presently, but I know they don't get shipped with the normal moment-timezone-with-data distributions.

You should be able to hook into the existing build process to grab the country code and push it into the regular zone data.

@adgrace
Copy link

adgrace commented Jul 27, 2016

Okay I will do the work this weekend. Thanks for all your support

@adgrace
Copy link

adgrace commented Aug 15, 2016

Hi, I am still working on this but I am quite busy at the moment, hope to have the feature completed soon and the documentation updated.

@adgrace
Copy link

adgrace commented Sep 1, 2016

Pull Request has been submitted and is passing all checks:
#377

Please feel free to review and let me know if there is anything that can be improved or any functionality to be added 👍 I hope this will be useful and is what people wanted.

Sorry it has taken a while to finish, I have been moving house as well as working full time with a long commute!

As I have said before this is my first Pull Request and I appreciate any feedback

@adgrace
Copy link

adgrace commented Nov 1, 2016

Pull Request has been submitted and is passing all checks:
#410

Appreciate any feedback!

@BadgerBadgerBadgerBadger

Any movement on this?

@cmalard
Copy link

cmalard commented Dec 31, 2017

Until the PR is merged, if you're using Webpack or similar, you can directly use built-in data like this :

import { zones } from 'moment-timezone/data/meta/latest.json';

function getCountryCode(zone) {
  return zones[zone] && zones[zone].countries[0];
}

getCountryCode(date.tz())

@gilmoreorless
Copy link
Member

gilmoreorless commented Sep 16, 2022

Moment Timezone has the country data exposed since version 0.5.28. However, adding a new API to get a single zone from a specific abbreviation/country combo isn't likely to be supported, given Moment Timezone's current maintenance-only status.

@gilmoreorless gilmoreorless closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2022
@toviszsolt
Copy link

The question is not silly at all, I also need a solution.

  • In such cases, the expected operation is that if there are multiple matches, an array is returned with all matching values.
  • The developer then decides which element of the array to use. In most cases, probably the first element, but one has to think of more complex uses, so it would be perfect if all time zones could be obtained in one array.
  • In addition, the array should somehow return in a form that allows further operations to be performed.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants