Du kan konvertere dit tidsstempel med tidszone til UTC og derefter trække epoken fra det:
select timestamp '2018-10-19 09:12:47.0 AMERICA/DENVER'
- timestamp '1970-01-01 00:00:00.0 UTC' as diff
from dual;
som giver dig en intervaldatatype:
DIFF
----------------------
+17823 15:12:47.000000
Du kan derefter udtrække elementerne fra det og gange hvert element med en passende faktor for at konvertere det til millisekunder (dvs. for dage, 60*60*24*1000); og føj dem derefter sammen:
select extract(day from diff) * 86400000
+ extract(hour from diff) * 3600000
+ extract(minute from diff) * 60000
+ extract(second from diff) * 1000 as unixtime
from (
select timestamp '2018-10-19 09:12:47.0 AMERICA/DENVER'
- timestamp '1970-01-01 00:00:00.0 UTC' as diff
from dual
);
UNIXTIME
--------------------
1539961967000
Dette bevarer også millisekunder, hvis starttidsstemplet har dem (dette konverteres fra en 'Unix'-tid, mens de bevares):
select (timestamp '1970-01-01 00:00:00.0 UTC' + (1539961967567 * interval '0.001' second))
at time zone 'America/Denver' as denver_time
from dual;
DENVER_TIME
--------------------------------------------
2018-10-19 09:12:47.567000000 AMERICA/DENVER
derefter for at konvertere tilbage:
select extract(day from diff) * 86400000
+ extract(hour from diff) * 3600000
+ extract(minute from diff) * 60000
+ extract(second from diff) * 1000 as unixtime
from (
select timestamp '2018-10-19 09:12:47.567 AMERICA/DENVER'
- timestamp '1970-01-01 00:00:00.0 UTC' as diff
from dual
);
UNIXTIME
--------------------
1539961967567
Hvis dit starttidsstempel har større præcision end det, bliver du nødt til at afkorte (eller runde/gulv/loft/støbning) for at undgå at få et resultat uden heltal; denne version trunkerer bare den udtrukne millisekunders del:
select diff,
extract(day from diff) * 86400000
+ extract(hour from diff) * 3600000
+ extract(minute from diff) * 60000
+ trunc(extract(second from diff) * 1000) as unixtime
from (
select timestamp '2018-10-19 09:12:47.123456789 AMERICA/DENVER'
- timestamp '1970-01-01 00:00:00.0 UTC' as diff
from dual
);
DIFF UNIXTIME
------------------------- --------------------
+17823 15:12:47.123456789 1539961967123
Uden den trunkering (eller tilsvarende) ville du ende med 1539961967123.456789
.
Jeg havde glemt uoverensstemmelsen i springsekunder; hvis du har brug for/ønsker at håndtere det, se dette svar .