4

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.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • what is the epoch time for 212186319010244541 ? Is it the real julian time, as in Julian calender? – yosukesabai Nov 09 '11 at 01:22
  • From some [docs](http://www.bsi2.com/PCALLS/JULIANTIMESTAMP): A Julian timestamp is "a value representing the number of microseconds since January 1, 4713 B.C." – blahdiblah Nov 09 '11 at 02:33
  • how do you define "Regular timestamp"? i can guess 2011, 10, 25, 16, 10, 10 being year ad, month, day of month, hour of day (16 oclock), 10 min, 10 sec. but what are the remaining numbers, i.e. 2455860, 244, 541 ? I dont find mention of output format in the doc you quoted. – yosukesabai Nov 09 '11 at 02:48
  • there are dozens of questions here on S.O. about converting dates in Unix. The simplest solution involves using the GNU date utility, if you're using a linux, then you almost certainly have it as /bin/date, so `man date` will give you a lot of info too. Good luck. – shellter Nov 09 '11 at 03:14

2 Answers2

4

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.

Sodved
  • 8,428
  • 2
  • 31
  • 43
  • 1
    The starting point in my system of the Julian timestamp seems to be Jan 1, 4713 B.C. at noon, not at the start of the day. The above then yields an offset of 12 hours. In that case subtract 210866760000000000 instead of the above to arrive at the correct time. – Edwin Nov 18 '15 at 08:13
  • 1
    @Edwin I have similar requirement: converting Julian date numeric from Tandem system into unix epoch time; and your version (subtract 210866760000000000) yields the exact timestamp that I need. Thank you! – aff Aug 02 '18 at 09:47
  • I am trying to convert 212186319010244541 into the Unix epoch and none of the above solutions working. – Akshay Naik Jun 27 '20 at 10:30
  • 1
    @AkshayNaik You can see it working here broken in relevant steps. https://ideone.com/eW8RHl – Sodved Jul 27 '20 at 17:26
0

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