Unix时间戳到ISO-8601字符串

时间:2016-11-16 17:46:42

标签: oracle timestamp iso8601

我有一个名为ACQDATA的Oracle表,其字段为READDATETIME,其中我以毫秒为单位存储Unix时间戳INTEGERNUMBER(38))类型。

SQL> select READDATETIME from ACQDATA where ID=1000;

READDATETIME
____________
   1.4793E+12

我需要选择该值作为ISO-8601字符串(YYYY-MM-DDTHH:MM:SS.mmm):

SQL> select READDATETIME from ACQDATA where ID=1000;

READDATETIME
-------------------
   1.4793E+12

我曾尝试使用TO_CHAR转换它,但结果很混乱:

   SQL> select TO_CHAR(TO_DATE('1970-01-01','YYYY-MM-DD') + NUMTODSINTERVAL(READDATETIME, 'SECOND'), 'YYYY-MM-DD HH24:MI:SS') from ACQDATA where ID=1000;

Error at line 1:
ORA-01873: the leading precision of the interval is too small    

帮助表示赞赏。

2 个答案:

答案 0 :(得分:3)

Alex的回答并不完全正确。 Unix时间戳始终基于1970-01-01 00:00:00 UTC

除非你的会话在UTC时区运行,否则精确的解决方案将是这样的:

select 
   TO_CHAR((TIMESTAMP '1970-01-01 00:00:00 UTC' + readdatetime/1000 * INTERVAL '1' SECOND) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;

select 
   TO_CHAR((TIMESTAMP '1970-01-01 00:00:00' AT TIME ZONE 'UTC' + readdatetime/1000 * INTERVAL '1' SECOND) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;

或者如果您更喜欢函数而不是文字:

select 
   TO_CHAR((TO_TIMESTAMP_TZ('1970-01-01 00:00:00 UTC', 'YYYY-MM-DD HH24:MI:SS TZR') + numtodsinterval(readdatetime/1000, 'SECOND')) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;

答案 1 :(得分:1)

您的readdatetime似乎是以毫秒为单位。 Oracle日期算法基于天数工作,因此您需要将该数字转换为它所代表的天数;一天是86400秒,所以它是86400000毫秒:

with acqdata (id, readdatetime) as (
  select 1000, 1479318995000 from dual
)
select to_char(date '1970-01-01' + (READDATETIME/86400000), 'YYYY-MM-DD"T"HH24:MI:SS')
from ACQDATA where ID=1000;

TO_CHAR(DATE'1970-0
-------------------
2016-11-16T17:56:35

T被添加为a character literal

SQL Developer默认显示科学记数法中的数字。您可以使用set numformat更改默认值,或使用to_char()显示整个值:

select readdatetime, to_char(readdatetime, '9999999999999') as string
from ACQDATA where ID=1000;

READDATETIME STRING        
------------ --------------
  1.4793E+12  1479318995000

如果您的值有小数秒,那么最后三位数字不是零,您可以将日期转换为时间戳并添加小数剩余;这也增加了UTC' Z'有趣的指标:

with acqdata (id, readdatetime) as (
  select 1000, 1479300462063 from dual
)
select to_char(cast(date '1970-01-01' + (readdatetime/86400000) as timestamp)
    + numtodsinterval(remainder(readdatetime, 1000)/1000, 'SECOND'),
  'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
from acqdata where id=1000;

TO_CHAR(CAST(DATE'1970-01-01'+
------------------------------
2016-11-16T12:47:42.063Z

或者没有中间date值,从时间戳文字开始:

with acqdata (id, readdatetime) as (
  select 1000, 1479300462063 from dual
)
select to_char(timestamp '1970-01-01 00:00:00'
  + numtodsinterval(readdatetime/1000, 'SECOND'),
  'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
from acqdata where id=1000;

TO_CHAR(TIMESTAMP'1970-0
------------------------
2016-11-16T12:47:42.063Z

正如@Wernfried所说,最好明确表明纪元时间是从UTC开始的:

alter session set time_zone='America/New_York';

with acqdata (readdatetime) as (
  select 1479300462063 from dual
  union all select 1467331200000 from dual
  union all select 1467648000000 from dual
)
select readdatetime,
  to_char(timestamp '1970-01-01 00:00:00' + numtodsinterval(readdatetime/1000, 'SECOND'),
    'YYYY-MM-DD"T"HH24:MI:SS.FF3') as implicit,
  to_char(cast(timestamp '1970-01-01 00:00:00' as timestamp with time zone)
    + numtodsinterval(readdatetime/1000, 'SECOND'),
    'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') as local_offset,
  to_char(timestamp '1970-01-01 00:00:00 UTC' + numtodsinterval(readdatetime/1000, 'SECOND'),
    'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') as utc_offset,
  to_char(timestamp '1970-01-01 00:00:00 UTC' + numtodsinterval(readdatetime/1000, 'SECOND'),
    'YYYY-MM-DD"T"HH24:MI:SS.FF3TZR') as utc
from acqdata;

  READDATETIME IMPLICIT                LOCAL_OFFSET                  UTC_OFFSET                    UTC                       
-------------- ----------------------- ----------------------------- ----------------------------- --------------------------
 1479300462063 2016-11-16T12:47:42.063 2016-11-16T12:47:42.063-05:00 2016-11-16T12:47:42.063+00:00 2016-11-16T12:47:42.063UTC
 1467331200000 2016-07-01T00:00:00.000 2016-07-01T01:00:00.000-04:00 2016-07-01T00:00:00.000+00:00 2016-07-01T00:00:00.000UTC
 1467648000000 2016-07-04T16:00:00.000 2016-07-04T17:00:00.000-04:00 2016-07-04T16:00:00.000+00:00 2016-07-04T16:00:00.000UTC