Easiest way 2 convert the time YYYY-MM-DD HH:MM:SS.subsec to MJD - gAWK?

What is the easiest way to convert the time (Gregorian YYYY-MM-DD HH:MM:SS.subseconds) to MJD (Modified Julian time) inside a text file:

2010-06-28 15:11:08.0902173 otherstuff
2010-06-28 15:12:09.3452183 otherstuff
...

to

55375 54668.0902173 otherstuff
55375 54729.3452183 otherstuff
...

that is MJD, Seconds of the day (HH x 3600)+(MM*60)+(SS), subSeconds) and other text.
It was a long file that was cleared by sed. It could have more than 20k lines.
I tried to use strftime and mktime...

---------- Post updated at 18:31 ---------- Previous update was at 18:29 ----------

http://www.unix.com/shell-programming-scripting/20559-time-date-manupulations-3.html

this is what I read...

Hi.

From a description at CPAN:

  use DateTime::Format::Epoch::MJD;

  my $dt = DateTime::Format::Epoch::MJD->parse_datetime( 53244 );
   # 2004-08-27T00:00:00

  DateTime::Format::Epoch::MJD->format_datetime($dt);
   # 53244

  my $formatter = DateTime::Format::Epoch::MJD->new();
  my $dt2 = $formatter->parse_datetime( 53244 );
   # 2004-08-27T00:00:00
  $formatter->format_datetime($dt2);
   # 53244

See DateTime::Format::Epoch::MJD - search.cpan.org

I have not tried this or any other method of these kinds of conversions, so I don't know what is easiest. This looks like a start, if nothing else.

Best wishes ... cheers, drl

( For the curious, see Julian day - Wikipedia, the free encyclopedia for an article on this. )

1 Like

I will try it and post the results here, to be useful for other people.

---------- Post updated at 23:34 ---------- Previous update was at 12:47 ----------

why the results are different?
{
typeset year=$1; typeset month=$2; typeset day=$3
typeset tmpmonth=$((12 * year + month - 3))
typeset tmpyear=$((tmpmonth / 12))
JD=$(( (734 * tmpmonth + 15) / 24 - 2 * tmpyear + \
tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
MJD=$((JD - 2400001)) #0.5 truncated
echo $MJD
}
echo $(date2julian 1987 5 31)

is different from:
gawk '{tmpmonth=((12*$1)+$2-3);tmpyear=(tmpmonth/12)}\
{ print (((tmpmonth*734)+15)/24-(2*tmpyear)+(tmpyear/4)-(tmpyear/100)+(tmpyear/400)+$3 +1721119-2400001)}' temp6.txt

temp6 > 1987 05 31 12 29 13