How to use time zones in Propel GraphQL queries
In this guide, you will learn what the time zone input field does and how to use it in GraphQL queries to power a localized user experience.
What does the timeZone
input field do?​
The timeZone
field tells Propel what time zone to use when aggregating data in a query.
For example, if a user requests a Counter with a TODAY
relative time range, the definition of today depends on the time zone. By passing the time zone field at query time, users can get data for today in their own time zone. Similarly, if a user asks for a Time Series with DAY
granularity, the start and end of each day granule depend on the time zone.
The big advantage of passing the time zone at query time is that you can serve users in multiple time zones with the same data.
How to use the timeZone
input field​
You can set the timeZone
field in Counter, Time Series, Leaderboard, and Metric Report queries. By default, it’s set to UTC.
A Time Series query with a timeZone
field looks like this:
- GraphQL Query
- GraphQL Variables
- JSON Response
query TimeSeriesExample($input: TimeSeriesInput!) {
timeSeries(input: $input) {
labels
values
}
}
{
"input": {
"metricName": "Revenue",
"granularity": "DAY",
"timeRange": {
"relative": "LAST_N_DAYS",
"n": 3
},
"timeZone": "America/Los_Angeles",
"filters": [
{
"column": "restaurant_name",
"operator": "EQUALS",
"value": "El Buen Sabor"
}
]
}
}
{
"data": {
"timeSeries": {
"labels": [
"2023-09-06T07:00:00Z",
"2023-09-07T07:00:00Z",
"2023-09-08T07:00:00Z"
],
"values": [
"288.25",
"0",
"0"
]
}
}
}
The timeZone
field can be any timezone in the tz database, for example "America/New_York" or "Europe/Paris". You can view the full list of tz database timezones on this Wikipedia page.
What about the absolute timeRange
?​
The absolute time range is defined by start and end ISO 8601 timestamps that include time zone information, for example, "2023-08-03T00:00:00Z" or "2023-08-03T00:00:00+02:00". Therefore, the time zone parameter has no effect on the absolute time range. However, in Time Series queries, it does affect how the granules of the time granularity are calculated.
For instance, here’s a Time Series query for the month of August 2023 in the "America/New_York" time zone (UTC-5 when Daylight Saving Time is not in effect).
- GraphQL Query
- GraphQL Variables
- JSON Response
query TimeSeriesExample($input: TimeSeriesInput!) {
timeSeries(input: $input) {
labels
values
}
}
{
"input": {
"metricName": "Revenue",
"granularity": "DAY",
"timeRange": {
"start": "2023-08-01T00:00:00-05:00Z",
"stop": "2023-08-31T23:59:59-05:00Z"
},
"timeZone": "America/New_York",
"filters": [
{
"column": "restaurant_name",
"operator": "EQUALS",
"value": "El Buen Sabor"
}
]
}
}
{
"data": {
"timeSeries": {
"labels": [
"2023-08-01T04:00:00Z",
"2023-08-02T04:00:00Z",
"2023-08-03T04:00:00Z",
"2023-08-04T04:00:00Z",
...
"2023-08-29T04:00:00Z",
"2023-08-30T04:00:00Z",
"2023-08-31T04:00:00Z",
"2023-09-01T04:00:00Z"
],
"values": [
"0",
"0",
"0",
"253.75",
...
"361",
"552",
"783",
"36.25"
]
}
}
}
Note: We used “2023-08-01T00:00:00-05:00Z” and “2023-08-31T23:59:59-05:00Z” as the start and stop time stamps to specify the absolute time range to query.
Then we used "timeZone": "America/New_York"
to specify the starting and end times for "granularity": "DAY"
.
JavaScript developers can use the built-in Date object and call toISOString()
. It will return the correct format for timeRange
.