Git Product home page Git Product logo

usertimezone's Introduction

User::Timezone

Important notice: This module was previously known as Intl::UserTimezone. Its name was changed to align with similar modules. It will still be usable as Intl::UserTimezone until the end of 2024 to give ample time for the change to happen.

A simple Raku module for determining the user's timezone as an Olson (IANA or tz) identifier, particularly useful when formatting dates.

To use:

use User::Timezone;

say user-timezone;  # 'America/New_York'
                    # 'Africa/Malabo'
                    # 'Asia/Tokyo'
                    # 'Etc/GMT'

The returned string does not apply a metazone. If you wish to show a user-facing string like, e.g., "Central European Time", you should use the appropriate functions from Intl::DateTime (NYI, might go into DateTime::*, haven't decided yet ^_^).

Options

The default fallback (in case detection fails) is Etc/GMT. If for some reason you wish to use a different one, pass it as a positional argument in the use statement. Note that this has global effects:

use User::Timezone 'Asia/Qyzylorda'

Similarly, you can force a certain timezone which is useful when testing out aspects of your program that may be sensitive to timezones. To do that, include the override option.

use User::Timezone :override;

This will import two additional subroutines into your scope:

override-user-timezone(Str $olson-id)
clear-user-timezone-override

These do exactly what their names imply. Again, be aware that their effects are global.

Compatibility / Implementation Notes

For Mac OS and most *nix systems, the region is accurately grabbed from /etc/localtime if it exists. Currently timezones indicated by the $TZ environmental variable are ignored, but will eventually be supported.

For Windows, the ID should be acceptable and better than a GMT offset, but may occasionally be suboptimal. This is because Windows uses its own custom timezone identifiers that do not have a one-to-one relationship with Olsen IDs. The Windows timezone ID is combined with the Windows GeoID to provide a best-guess that should generally be accurate. For example, if your Windows timezone is "Central Standard Time", and your GeoID is 244 (United States), then it will be reported as America/Chicago (others are possible, but there is not enough information to differentiate with America/Indiana/Knox, so the broadest is used). But if your GeoID is 39 (Canada), then it will be reported as America/Winnipeg.

What if it doesn't work

If there is a problem determining the timezone, the default will be Etc/GMT, being the most generic, although that's not really an acceptable alternative. If it's clear that User::Timezone cannot determine things for your operating system, or is in some other way not returning the correct results, please file an issue on Github and let's figure out how to make it work on your system.

Version history

  • v0.3.3
    • Add native Mac support
  • v0.3.2
    • Minor adjustment to Windows regions data files
    • Code reorganization for improved future maintenance
  • v0.3.1
    • Updated Windows zones data files
  • v0.3.0
    • Changed name to User::Timezone and added a fallback message for older uses
    • Adjusted Mac OS detection for improved accuracy
  • v0.2
    • Added option for a custom fallback (in case detection fails)
    • Added ability for overriding the timezone (mainly for testing purposes)
  • v0.1.1
    • Removed test code that prevented correct Windows detection.
    • Chomped output to facilitate use in other modules.
  • v0.1
    • Initial release with support for macOS, most *nix machines and beta support Windows

License

Except as indicated, this module and all its files is provided under the Artistic License 2.0.

Except as indicated

The file windowsZones.xml is Copyright 2022 The Unicode Consortium, and distributed without modification in accordance with its license/terms of use.

usertimezone's People

Contributors

alabamenhu avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

usertimezone's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.