Unix时间戳转换在Mysql和Oracle中有所

Unix时间戳转换在Mysql和Oracle中有所

本文介绍了Unix时间戳转换在Mysql和Oracle中有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unix时间戳转换在mysql和oracle中给出了不同的结果

Unix time stamp conversion giving tow different result in mysql and oracle

select FROM_UNIXTIME(1387444958) from dual;

输出:2013-12-19 10:22:38

Output :2013-12-19 10:22:38

select to_char(to_date('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS')+ (1387444958/86400),'YYYY-MM-DD HH24:MI:SS')from dual;

输出:2013-12-19 09:22:38

output: 2013-12-19 09:22:38

任何人都可以帮助我从MySql中获得与Oracle相同的时间戳.

Can anyone please help me in getting same timestamp from oracle as I am getting in MySql.

推荐答案

Unix时间戳距1970-01-01 00:00:00 UTC的秒数,实际上是您本地时区(或MySQL服务器所在的时区)中的1970-01-01 01:00:00.看来FROM_UNIXTIME考虑到了这一点.

Unix timestamp is seconds from 1970-01-01 00:00:00 UTC which is actually 1970-01-01 01:00:00 in your local timezone (or the timezone where your MySQL server is located). Looks like FROM_UNIXTIME takes this into account.

对于Oracle,您可以使用此功能:

For Oracle you can use this function:

FUNCTION UnixTime2Timestamp(UnixTime IN NUMBER) RETURN TIMESTAMP IS
BEGIN
    RETURN (TIMESTAMP '1970-01-01 00:00:00 UTC' + UnixTime * INTERVAL '1' SECOND) AT LOCAL;
END UnixTime2Timestamp;

我假设如果您想在MySQL中获得UTC时间,那么您必须运行

I assume if you like to get UTC time in MySQL then you have to run

select
   CONVERT_TZ(FROM_UNIXTIME(1387444958),'{your local timezone}','UTC')
from dual;

这篇关于Unix时间戳转换在Mysql和Oracle中有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:17