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

How to create time zones by name? #369

Closed
DanielSWolf opened this issue Nov 16, 2018 · 5 comments
Closed

How to create time zones by name? #369

DanielSWolf opened this issue Nov 16, 2018 · 5 comments

Comments

@DanielSWolf
Copy link
Contributor

I need to create a Zone object based on the time zone name, e.g., "Europe/Berlin". Looking at Luxon's code, there is a convenient function normalizeZone that does just that. Unfortunately, Luxon's main module doesn't re-export this function; what's more, it is marked as @private.

Is there another way to create a Zone instance based on a string (and, ideally, an explicit fallback value)?

@icambron
Copy link
Member

Not a supported way, no. Can I ask why you need a Zone instance?

@DanielSWolf
Copy link
Contributor Author

The following explanation refers both to this issue and to #368.

We're planning to use Luxon as part of an Enterprise solution. Our end users regularly travel between time zones, so we're doing a lot of time zone-based calculations. For example:

  • Does a time zone string stored in the database represent a valid time zone?
  • Do two DateTimes share the same time zone?
  • Convert a bunch of DateTimes to a common time zone selected by the user
  • etc.

Most of these tasks can somehow be accomplished with the current interface. But our code would be much clearer if we could represent time zones as objects (in the OOP sense), not just strings. This is especially true since we're programming in TypeScript. If a Zone is modelled as an abstract base class with a number of concrete implementations, TypeScript can verify we're passing the correct kind of information. If a Zone is simply a string, TypeScript isn't much help.

The thing is that most of the Zone API is already exposed. Luxon currently exports the abstract base class Zone as well as the derived concrete classes FixedOffsetZone, IANAZone, and LocalZone. What's more, all Luxon methods that expect a time zone as input allow either a string or a Zone instance.

Almost everything is already exposed. Only two pieces are missing to make complete use of these classes:

  • a factory method that takes a string and returns a Zone instance of the appropriate type (that's what this issue is about)
  • a way to get the Zone instance for an existing DateTime (see issue Why is DateTime.zone private? #368).

@icambron
Copy link
Member

I thought this over and I'm good with it. If you make a PR making the zone factory public and the zone accessor public, along with docs, I'll take it.

@DanielSWolf
Copy link
Contributor Author

Thanks, I'll do that!

DanielSWolf added a commit to DanielSWolf/luxon that referenced this issue Jan 10, 2019
@icambron
Copy link
Member

Fixed in #404

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

2 participants