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.
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.
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.
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:
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:
NOTES
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.
Pine腳本庫
秉持 TradingView 一貫的共享精神,作者將此 Pine 程式碼發佈為開源庫,讓社群中的其他 Pine 程式設計師能夠重複使用。向作者致敬!您可以在私人專案或其他開源發佈中使用此庫,但在公開發佈中重複使用該程式碼需遵守社群規範。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。
Pine腳本庫
秉持 TradingView 一貫的共享精神,作者將此 Pine 程式碼發佈為開源庫,讓社群中的其他 Pine 程式設計師能夠重複使用。向作者致敬!您可以在私人專案或其他開源發佈中使用此庫,但在公開發佈中重複使用該程式碼需遵守社群規範。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。