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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Easiest way 2 convert the time YYYY-MM-DD HH:MM:SS.subsec to MJD - gAWK?
# 1  
Old 07-11-2010
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 ----------

https://www.unix.com/shell-programmin...lian-date.html
https://www.unix.com/shell-programmin...lations-3.html
https://www.unix.com/shell-programmin...dd-julian.html
this is what I read...

Last edited by fpmurphy; 07-11-2010 at 09:46 PM.. Reason: Updated link to english version of post from portuguese
# 2  
Old 07-12-2010
Hi.

From a description at CPAN:
Code:
  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. )
This User Gave Thanks to drl For This Post:
# 3  
Old 07-12-2010
Thank you

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to convert mmm-yy to mm/dd/yyyy format in UNIX ?

How to convert mmm-yy to mm/dd/yyyy format in unix ? example: Jan-99 to 01/01/1999 Jan-00 to 01/01/2000 Jan-25 to 01/01/2025 Dec-99 to 01/12/1999 Dec-00 to 01/12/2000 Dec-25 to 01/12/2025 YY anything between 00-50 should be 2000-2050 YY anything between 51-99 should be 1951-1999 ... (2 Replies)
Discussion started by: gksenthilkumar
2 Replies

2. Shell Programming and Scripting

Bash to convert to m-d-yyyy

I am using bash that when run downloads a file a verifies that there is data in it. What I am not able to do is have a user enter a date in any format they wish and have it converted to m-d-yyyy. Thank you :). Bash printf " Welcome to NGS analysis, checking for new files and creating a... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Convert date column as yyyy/mm/dd format

Hi All, I have file like “April 10, 2013”,”raj” “April 29, 2013”,”raj1” Output : “2013/04/10”,”raj” “2013/04/29”,”raj1” Please help me how to do... (9 Replies)
Discussion started by: bmk
9 Replies

4. Shell Programming and Scripting

Convert any date format into yyyy/mm/dd

How can I convert any user inputted date into yyyy/mm/dd ? For example user can input date one of the following 20120121 , 2012-01-21 ,01/21/2012,01/21/2012 etc But I need to convert any of the date entered by user into yyyy/mm/dd (2012/01/2012). Any suggestion. Thanks in advance this is... (1 Reply)
Discussion started by: ZeroHedge
1 Replies

5. Shell Programming and Scripting

convert d/m/yyyy to YYYY-MM-DD

My csv has data like this x,x,3452,2/18/1986,abc x,g,19711,1/24/1986,abc i want to replace date in the following format YYYY-mm-dd how do i convert using awk script ? (8 Replies)
Discussion started by: nuthalapati
8 Replies

6. Shell Programming and Scripting

Sed: zero-padding dates (or: convert d/m/yyyy to dd/mm/yyyy)

Hi all I have some pipe-separated data in the form: 5/12/2008 00:00:00|31/1/2009 00:00:00|SOMESTUFF|OTHERSTUFF 12/31/2008 00:00:00|15/1/2009 00:00:00|MORESTUFF|REMAININGSTUFF 1/1/1023 00:00:00|16/5/2047 00:00:00|THEREST|YETMORE I need to zero-pad the single-digit days and months, using... (3 Replies)
Discussion started by: jgrogan
3 Replies

7. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

8. Shell Programming and Scripting

Convert DD-MMM-YYYY to MM-DD-YYYY

I have a file which has 100k+ records like this abc,05-JUN-1974,def,lkj,aaa def,11-SEP-1975,ghj,dis,dea I want to convert ex 05-JUN-1974 to 06/05/1974 Please help me with awk script to convert the whole file into MM-DD-YYYY Thank you! (2 Replies)
Discussion started by: nuthalapati
2 Replies

9. Shell Programming and Scripting

How to convert DDMMYYYY to DD MONTH YYYY in Unix

Hi I am having date as a string in DDMMYYYY format(07082008) in a variable say cdate. I want to Convert it into DD Month YYYY format(7 August 2008). Could someone help. Thanks in Advance. (2 Replies)
Discussion started by: rspk_praveen
2 Replies

10. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies
Login or Register to Ask a Question