bash 在 UNIX 中将 Julian 时间戳转换为常规时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8059172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Julian Timestamp to Regular Time in UNIX
提问by jaypal singh
I need to convert Julian timestamp to Regular timestamp in UNIX using Bash.
我需要使用 Bash 在 UNIX 中将 Julian 时间戳转换为常规时间戳。
On Tandem OS, conversion is pretty straightforward -
在 Tandem OS 上,转换非常简单 -
Example: 212186319010244541
示例:212186319010244541
$OLSAPP SYSTST 1> #interprettimestamp 212186319010244541
#interprettimestamp 212186319010244541 expanded to:
2455860 2011 10 25 16 10 10 244 541
I wish to do the same on UNIX environment. The conversion will be a part of a parser script. So one-liners would be greatly appreciated.
我希望在 UNIX 环境中做同样的事情。转换将成为解析器脚本的一部分。所以单线将不胜感激。
UPDATE:
更新:
INTERPRETTIMESTAMP inbuilt function on Tandem returns a space-separated list of nine numbers, consisting of the Julian day number, year, month, day, hour, minute, second, millisecond, and microsecond.
Tandem 的内置函数 INTERPRETTIMESTAMP 返回一个以空格分隔的九个数字列表,包括儒略日数、年、月、日、小时、分钟、秒、毫秒和微秒。
采纳答案by Sodved
Assuming the number is as @blahdiblah says
假设数字如@blahdiblah 所说
"a value representing the number of microseconds since January 1, 4713 B.C."
“代表自公元前 4713 年 1 月 1 日以来的微秒数的值”
Then you first need to know the Julian timestamp for 01-JAN-1970 which is the epoch for unix time. So a cludgy oracle query gives
然后,您首先需要知道 01-JAN-1970 的 Julian 时间戳,这是 unix 时间的纪元。所以一个笨拙的 oracle 查询给出
210866803200000000
Then you could in theoryjust have a shell command to compute the number of seconds since 1-Jan-1970.
那么理论上你可以只用一个 shell 命令来计算自 1970 年 1 月 1 日以来的秒数。
unixtime=$(( ( 212186319010244541 - 210866803200000000 ) / 1000000 ))
The problems with this are:
这样做的问题是:
- you still need to format it
- your bash may not like integer arithmatic with 18 digit numbers. (think its OK in 64 bit, but not 32 bit).
- 你仍然需要格式化它
- 您的 bash 可能不喜欢带有 18 位数字的整数算术。(认为 64 位可以,但 32 位不行)。
Now if you have perl installed you can solve these using the bigintand POSIXmodules. As a shell "one" liner it looks like
现在,如果您安装了 perl,您可以使用bigint和POSIX模块解决这些问题。作为外壳“一个”衬里,它看起来像
perl -mbigint -mPOSIX -e 'print( POSIX::strftime("%Y-%m-%d %T",localtime( ($ARGV[0]-210866803200000000)/1000000 ) )."\n")' 212186319010244541
Which gives
这使
2011-10-25 15:10:10
The 1 hour difference is probably due to daylight savings differences. It could be either in the perl, or more likely the value I used for 01-Jan-1970 could be an hour out. So you may need to check both of them to be sure its right for your system.
1 小时的差异可能是由于夏令时差异。它可能在 perl 中,或者更有可能是我在 1970 年 1 月 1 日使用的值可能是一个小时。因此,您可能需要检查它们以确保它适合您的系统。
回答by user4893857
this is MJD converter
这是 MJD 转换器
MJD is -3506716800 than epoch
MJD 比 epoch 为 -3506716800
# date -d '1858-11-17 UTC' +%s
-3506716800
example: MDJ 57153 is Mon May 11 00:00:00 UTC 2015
例如:MDJ 57153 是 UTC 2015 年 5 月 11 日星期一 00:00:00
# date -d @`echo 57153*86400-3506716800|bc`
Mon May 11 00:00:00 UTC 2015

