如何從包含 Oracle 中時區偏移的日期/時間字符串中獲取 UTC 日期/時間 (How to get UTC date/time from a date/time string that contains timezone offset in Oracle)


問題描述

如何從包含 Oracle 中時區偏移的日期/時間字符串中獲取 UTC 日期/時間 (How to get UTC date/time from a date/time string that contains timezone offset in Oracle)

給定這樣的字符串:'2015‑11‑23T07:00:10.563‑04:00' 我正在嘗試確定我期望的 UTC 日期/時間:'2015‑11‑23 11:00 :10.5630000'

我嘗試了以下方法:

SELECT sys_extract_utc(
        to_timestamp_tz('2015‑11‑23T07:00:10.563‑04:00', 
                        'yyyy‑mm‑dd"T"hh24:mi:ss.FF TZH:TZM')
         )  
FROM DUAL;

但這會產生結果:

2015/11/23 03:00:10.563000000

即使我將時區偏移量更改為正值,我也會得到同樣的結果。我一定是誤會了,所以非常感謝您的幫助。


參考解法

方法 1:

There is no space between fractional seconds and timezone hour. So remove space from you pattern, and it'll give desired result.

SQL> SELECT sys_extract_utc(to_timestamp_tz('2015‑11‑23T07:00:10.563‑04:00', 'yyyy‑mm‑dd"T"hh24:mi:ss.FFTZH:TZM'))  
FROM DUAL; 

SYS_EXTRACT_UTC(TO_TIMESTAMP_TZ('2015‑11‑23T07:00:10.563‑04:00','YYYY‑MM‑DD
‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
23‑NOV‑15 11.00.10.563000000 AM

方法 2:

Fixed it ‑ the space character between ".....hh24:mi:ss.FF TZH:TZM" was causing the problem.

This now works perfectly:

SELECT sys_extract_utc(to_timestamp_tz('2015‑11‑23T07:00:10.563+04:00', 'yyyy‑mm‑dd"T"hh24:mi:ss.FFTZH:TZM'))  FROM DUAL;

(by PaologNoelPaolog)

參考文件

  1. How to get UTC date/time from a date/time string that contains timezone offset in Oracle (CC BY‑SA 2.5/3.0/4.0)

#utc #offset #SQL #timezone #oracle






相關問題

Java - 從外部服務器獲取 POSIX UTC 時間戳的最佳選擇? (Java - Best choice to get a POSIX UTC timestamp from external server?)

PHP - Параўнанне лакальнага і UTC часу са зрушэннем гадзіннага пояса (PHP - Local vs UTC Time Comparison with Timezone Offset)

以 UTC 格式轉換日期時間 (Convert date time in utc)

如何確定日期字符串是否包含已在 Javascript 中添加的時間偏移量? (How to figure out if the Date string contains time offset added already in Javascript?)

Delphi - Tải TTimeZone cho múi giờ không thuộc địa phương và chuyển đổi giữa các múi giờ (Delphi - Get TTimeZone for non-local timezone and convert between timezones)

什麼是“標準”時區縮寫? (What are the "standard" timezone abbreviations?)

如何從包含 Oracle 中時區偏移的日期/時間字符串中獲取 UTC 日期/時間 (How to get UTC date/time from a date/time string that contains timezone offset in Oracle)

如何在不使用 Javascript 的情況下在 ASP.Net 中呈現給定 UTC 日期時間值的本地時間? (How to render local time given UTC datetime values in ASP.Net without using Javascript?)

如何讓 Java 解析和格式化具有相同時區的日期/時間?我一直在獲取本地時區 (How do I get Java to parse and format a date/time with the same time zone? I keep getting the local timezone)

Python - 從 DST 調整的本地時間到 UTC (Python - From DST-adjusted local time to UTC)

7 位小數計算的 UTC 時間 (UTC time with 7 decimals calculation)

我們應用程序中的日期格式 (Date format in our Application)







留言討論