【问题标题】:Elegant timezone conversion in BigQueryBigQuery 中的优雅时区转换
【发布时间】:2018-10-22 15:51:46
【问题描述】:

我已将具有东部时间戳的数据加载到 BigQuery 中,这些时间戳存储为来自源数据的原始时间戳:

placed_eastern
-------------------
2018-07-23 00:00:03

BigQuery 默认假定原始时间戳应存储为 UTC:

2018-07-23 00:00:03 UTC

问题:我需要修正这个时区假设。

我目前有一个丑陋的黑客来修复此错误,以确保正确存储时间戳。这会将时间戳的日期和时间部分提取为字符串,将它们连接起来,然后重新创建时间戳。

select 
  placed_eastern,
  timestamp(
    concat(
      cast(extract(date from placed_eastern) as string), 
      ' ', 
      cast(extract(time from placed_eastern) as string)
    ),
    'US/Eastern'
  ) as actual_placed_utc

问题:是否有一种优雅的记录方式来处理这个问题?

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    您可以转换为DATETIME 以将时间戳视为逻辑日期/时间,然后使用US/Eastern 转换回TIMESTAMP

    SELECT TIMESTAMP(DATETIME(placed_eastern), 'US/Eastern')) AS actual_placed_utc
    FROM dataset.table
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-05
      • 2012-09-11
      • 2015-12-10
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多