Convert Julian Timestamp to Regular Time in UNIX

此生再无相见时 提交于 2021-02-09 01:57:12

问题


I need to convert Julian timestamp to Regular timestamp in UNIX using Bash.

On Tandem OS, conversion is pretty straightforward -

Example: 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.

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.


回答1:


Assuming the number is as @blahdiblah says

"a value representing the number of microseconds since January 1, 4713 B.C."

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

210866803200000000

Then you could in theory just have a shell command to compute the number of seconds since 1-Jan-1970.

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).

Now if you have perl installed you can solve these using the bigint and POSIX modules. As a shell "one" liner it looks like

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.




回答2:


this is MJD converter

MJD is -3506716800 than epoch

# date -d '1858-11-17 UTC' +%s
-3506716800

example: MDJ 57153 is Mon May 11 00:00:00 UTC 2015

# date -d @`echo 57153*86400-3506716800|bc`
Mon May 11 00:00:00 UTC 2015


来源:https://stackoverflow.com/questions/8059172/convert-julian-timestamp-to-regular-time-in-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!