Editors' picks
PINE LIBRARY

Timezone

3 371
Library with pre-defined timezone enums that can be used to request a timezone input from the user. The library provides a `tostring()` function to convert enum values to a valid string that can be used as a `timezone` parameter in pine script built-in functions. The library also includes a bonus function to get a formatted UTC offset from a UNIX timestamp.

The timezone enums in this library were compiled by referencing the available timezone options from TradingView chart settings as well as multiple Wikipedia articles relating to lists of time zones.

Some enums from this library are used to retrieve an IANA time zone identifier, while other enums only use UTC/GMT offset notation. It is important to note that the Pine Script User Manual recommends using IANA notation in most cases.



HOW TO USE
This library is intended to be used by Pine Coders who wish to provide their users with a simple way to input a timezone. Using this library is as easy as 1, 2, 3:

Step 1
Import the library into your script. Replace <libraryVersion> with the latest available version number for this library.
Pine Script®
//@version=6 indicator("Example") import n00btraders/Timezone/<libraryVersion> as tz


Step 2
Select one of the available enums from the library and use it as an input. Tip: view the library source code and scroll through the enums at the top to find the best choice for your script.
Pine Script®
timezoneInput = input.enum(tz.TimezoneID.EXCHANGE, "Timezone")


Step 3
Convert the user-selected input into a valid string that can be used in one of the pine script built-in functions that have a `timezone` parameter.
Pine Script®
string timezone = tz.tostring(timezoneInput)




EXPORTED FUNCTIONS
There are multiple 𝚝𝚘𝚜𝚝𝚛𝚒𝚗𝚐() functions in this library: one for each timezone enum. The function takes a single parameter: any enum field from one of the available timezone enums that are exported by this library. Depending on the selected enum, the function will return a time zone string in either UTC/GMT notation or IANA notation.

Note: to avoid confusion with the built-in `str.tostring()` function, it is recommended to use this library's `tostring()` as a method rather than a function:
Pine Script®
string timezone = timezoneInput.tostring()



offset(timestamp, format, timezone, prefix, colon)
  Formats the time offset from a UNIX timestamp represented in a specified timezone.
  Namespace types: series OffsetFormat
  Parameters:
    timestamp (int): (series int) A UNIX time.
    format (series OffsetFormat): (series OffsetFormat) A time offset format.
    timezone (string): (series string) A UTC/GMT offset or IANA time zone identifier.
    prefix (string): (series string) Optional 'UTC' or 'GMT' prefix for the result.
    colon (bool): (series bool) Optional usage of colon separator.
  Returns: Time zone offset using the selected format.

The 𝚘𝚏𝚏𝚜𝚎𝚝() function is provided as a convenient alternative to manually using `str.format_time()` and then manipulating the result.

The OffsetFormat enum is used to decide the format of the result from the `offset()` function. The library source code contains comments above this enum declaration that describe how each enum field will modify a time offset.

Tip: hover over the `offset()` function call in the Pine Editor to display a pop-up containing:
  • Function description
  • Detailed parameter list, including default values
  • Example function calls
  • Example outputs for different OffsetFormat.* enum values




NOTES
  • At the time of this publication, Pine cannot be used to access a chart's selected time zone. Therefore, the main benefit of this library is to provide a quick and easy way to create a pine script input for a time zone (most commonly, the same time zone selected in the user's chart settings).
  • At the time of the creation of this library, there are 95 Time Zones made available in the TradingView chart settings. If any changes are made to the time zone settings, this library will be updated to match the new changes.
  • All time zone enums (and their individual fields) in this library were manually verified and tested to ensure correctness.
  • An example usage of this library is included at the bottom of the source code.
  • Credits to HoanGhetti for providing a nice Markdown resource which I referenced to be able to create a formatted informational pop-up for this library's `offset()` function.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.