I have millions of records in Azure Data Explorer. Each of this record has a timestamp value associated with it. I want to be able to convert this timestamp value in the specific time zone.
For example in SQL I use AT TIME ZONE to convert timestamp value from one zone into another -
Select CONVERT(datetime, timestampvalueColumn) AT TIME ZONE 'UTC' AT TIME ZONE 'US Eastern Standard Time' as 'TimeInEST' from Table;
I am not willing to use offset value as it doesn't support daylight saving changes.
How I can do this with Kusto query language in ADX?
CodePudding user response:
Luckily, US East Time is relatively a simple case.
The code below is valid for dates in the range of 2007 and beyond.
In the United States Daylight Saving Time begins at 2:00 a.m. local time on the second Sunday in March.
On the first Sunday in November areas on Daylight Saving Time return to Standard Time at 2:00 a.m.
When Daylight Saving Time begins in the United States turn your clocks ahead one hour. At the end of Daylight Saving Time turn your clocks back one hour.
https://www.timetemperature.com/tzus/daylight_saving_time.shtml
// Sample generation. Not part of the solution
let t = materialize(range i from 1 to 15 step 1 | extend dt_utc = ago(rand()*365d*10));
// Solution Starts here
let et2utc_mar_2nd_sun_2am = (dt_utc:datetime){make_datetime(getyear(dt_utc),3,1,2,0,0) (7d - dayofweek(make_datetime(getyear(dt_utc),3,1)))} 7d 5h};
let et2utc_nov_2nd_sun_2am = (dt_utc:datetime){make_datetime(getyear(dt_utc),11,1,2,0,0) (7d - dayofweek(make_datetime(getyear(dt_utc),3,1)))} 4h};
let EasternTimeOffset = (dt_utc:datetime){iff(dt_utc between (et2utc_mar_2nd_sun_2am(dt_utc) .. et2utc_nov_2nd_sun_2am(dt_utc)), -4h, -5h)};
t
| extend EasternTimeOffset = EasternTimeOffset(dt_utc)
| extend EasternTime = dt_utc EasternTimeOffset
i | dt_utc | EasternTimeOffset | EasternTime |
---|---|---|---|
2 | 2013-01-02T09:46:21.2553241Z | -05:00:00 | 2013-01-02T04:46:21.2553241Z |
12 | 2013-04-29T19:53:51.3170521Z | -04:00:00 | 2013-04-29T15:53:51.3170521Z |
1 | 2013-09-03T18:31:31.2211331Z | -04:00:00 | 2013-09-03T14:31:31.2211331Z |
11 | 2013-09-25T13:45:33.3380811Z | -04:00:00 | 2013-09-25T09:45:33.3380811Z |
8 | 2013-10-02T23:44:54.8682621Z | -04:00:00 | 2013-10-02T19:44:54.8682621Z |
15 | 2014-03-30T12:44:47.6381621Z | -04:00:00 | 2014-03-30T08:44:47.6381621Z |
10 | 2015-02-21T15:34:46.8657901Z | -05:00:00 | 2015-02-21T10:34:46.8657901Z |
9 | 2016-06-16T19:44:31.0144371Z | -04:00:00 | 2016-06-16T15:44:31.0144371Z |
3 | 2016-10-21T04:03:51.0185641Z | -04:00:00 | 2016-10-21T00:03:51.0185641Z |
5 | 2017-11-11T01:50:21.6729811Z | -05:00:00 | 2017-11-10T20:50:21.6729811Z |
14 | 2017-12-02T01:02:18.3206731Z | -05:00:00 | 2017-12-01T20:02:18.3206731Z |
7 | 2018-11-06T06:00:52.4156001Z | -05:00:00 | 2018-11-06T01:00:52.4156001Z |
13 | 2019-11-18T14:05:15.4887631Z | -05:00:00 | 2019-11-18T09:05:15.4887631Z |
4 | 2021-02-16T01:02:37.7791421Z | -05:00:00 | 2021-02-15T20:02:37.7791421Z |
6 | 2021-07-28T04:16:18.3716831Z | -04:00:00 | 2021-07-28T00:16:18.3716831Z |
CodePudding user response:
Based on the Python plugin
pandas relevant documentation: tz_localize, tz_convert & strftime
// Sample generation. Not part of the solution
let t = materialize(range i from 1 to 15 step 1 | extend dt_utc = bin(ago(rand()*365d*10), 1s));
// Solution Starts here
let p_schema = typeof(*, dt_et:datetime);
let p_arguments = dynamic({"TZ" : "US/Eastern"});
let p_script =
```
iso_fmt = "%Y-%m-%d %H:%M:%S"
result = df
result["dt_et"] = df["dt_utc"].dt.tz_localize("UTC").dt.tz_convert(kargs["TZ"]).dt.strftime(iso_fmt)
```;
t
| evaluate python(p_schema, p_script, p_arguments)
| extend offset = dt_et - dt_utc
i | dt_utc | dt_et | offset |
---|---|---|---|
5 | 2013-03-20T02:43:54Z | 2013-03-19T22:43:54Z | -04:00:00 |
11 | 2013-03-20T04:29:50Z | 2013-03-20T00:29:50Z | -04:00:00 |
15 | 2013-05-12T06:03:49Z | 2013-05-12T02:03:49Z | -04:00:00 |
8 | 2014-02-21T22:23:32Z | 2014-02-21T17:23:32Z | -05:00:00 |
13 | 2015-12-26T08:55:41Z | 2015-12-26T03:55:41Z | -05:00:00 |
2 | 2016-06-09T10:53:04Z | 2016-06-09T06:53:04Z | -04:00:00 |
4 | 2016-12-17T21:19:15Z | 2016-12-17T16:19:15Z | -05:00:00 |
10 | 2017-06-23T06:54:48Z | 2017-06-23T02:54:48Z | -04:00:00 |
7 | 2017-07-25T21:12:58Z | 2017-07-25T17:12:58Z | -04:00:00 |
1 | 2019-12-05T16:43:48Z | 2019-12-05T11:43:48Z | -05:00:00 |
3 | 2019-12-18T17:20:17Z | 2019-12-18T12:20:17Z | -05:00:00 |
12 | 2019-12-30T02:51:46Z | 2019-12-29T21:51:46Z | -05:00:00 |
14 | 2021-01-31T08:36:00Z | 2021-01-31T03:36:00Z | -05:00:00 |
6 | 2021-04-25T13:31:14Z | 2021-04-25T09:31:14Z | -04:00:00 |
9 | 2021-07-15T11:20:58Z | 2021-07-15T07:20:58Z | -04:00:00 |