解析带时区的时间格式在Presto SQL中

huangapple go评论127阅读模式
英文:

Parsing time format with zone in presto sql

问题

如何将以下时间格式解析为Presto SQL中的时间戳,更具体地说是AWS Athena?

Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)

Mon Feb 27 2023 00:37:44 GMT-0500 (Eastern Standard Time),等等

我尝试了这个:

  1. select date_parse(
  2. 'Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)',
  3. '%a %b %d %Y %H:%i:%s GMT%z (%Z)')

但出现了以下错误:

INVALID_FUNCTION_ARGUMENT: Invalid format: "Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)" is malformed at "+0530 (India Standard Time)"

我不确定Presto SQL中是否存在%z%Z指示符。

请注意,时区可能是任何时区,而不仅仅是GMT+0530,所以我不能忽略时区。

英文:

How to parse the following time format to timestamp in presto sql, more specifically AWS Athena?

Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time),

Mon Feb 27 2023 00:37:44 GMT-0500 (Eastern Standard Time), etc

I tried this,

  1. select date_parse(
  2. 'Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)',
  3. '%a %b %d %Y %H:%i:%s GMT%z (%Z)')

but got the following error

> INVALID_FUNCTION_ARGUMENT: Invalid format: "Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)" is malformed at "+0530 (India Standard Time)"

I'm not sure if the %z or %Z specifier is present in Presto sql.

Note that the timezone may be anything, not just GMT+0530, so i can't ignore the timezone.

答案1

得分: 1

首先,就我所见,印度标准时间东部标准时间似乎不在Athena支持的时区列表中,建议从字符串中移除此部分,然后使用支持JodaTime的parse_datetime进行转换:

  1. -- 示例数据
  2. with dataset(dt) as (
  3. values ('Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)'),
  4. ('Mon Feb 27 2023 00:37:44 GMT-0500 (Eastern Standard Time)')
  5. )
  6. -- 查询
  7. SELECT
  8. PARSE_DATETIME(regexp_replace(dt, '\s*\(.*\)'), 'E MMM dd yyyy HH:mm:ss ''GMT''Z')
  9. FROM dataset;

输出:

  1. _col0
  2. --------------------------------
  3. 2023-02-27 06:21:22.000 +05:30
  4. 2023-02-27 00:37:44.000 -05:00
英文:

First of all as far as I can see India Standard Time and Eastern Standard Time are not in the list of timezones supported by Athena, I suggest to remove this part from the string and then switch to parse_datetime which supports JodaTime’s DateTimeFormat:

  1. -- sample data
  2. with dataset(dt) as (
  3. values ('Mon Feb 27 2023 06:21:22 GMT+0530 (India Standard Time)'),
  4. ('Mon Feb 27 2023 00:37:44 GMT-0500 (Eastern Standard Time)')
  5. )
  6. -- query
  7. SELECT
  8. PARSE_DATETIME(regexp_replace(dt, '\s*\(.*\)'), 'E MMM dd yyyy HH:mm:ss ''GMT''Z')
  9. FROM dataset;

Output:

  1. _col0
  2. --------------------------------
  3. 2023-02-27 06:21:22.000 +05:30
  4. 2023-02-27 00:37:44.000 -05:00

huangapple
  • 本文由 发表于 2023年7月20日 14:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定