Register
It is currently Tue Jul 22, 2014 7:39 pm

Help With A Julian Date Conversion Script (calendar->julian)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jun 20, 2011 3:23 pm   

Joined: Mon Jun 20, 2011 3:00 pm
Posts: 1
I'm trying to write a program in which the user enters a calendar date such as 2011 06 20 11 04 45 (YYYY MM DD hh mm ss), and gets back a Julian date such as 2455732.961632 (I test results with the conversion tool at http://aa.usno.navy.mil/data/docs/JulianDate.php). The day occasionally strays, from 1 day to around maybe 50 days off, with no discernible pattern, at least not chronologically. The time, the numbers after the decimal, is always correct. I was wondering if anyone knew of some vague equation I've forgotten. I've already written the code for leap years, the different amounts of days in each month, the absence of a year 0, etc. Is there anything I may be missing, or something that I'm likely to have miscalculated?

Any help would be appreciated.


Top
 Profile  
 PostPosted: Mon Jun 20, 2011 5:46 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
I did it with awk(1), using the formulas from this website.

Code:
#!/usr/bin/awk -f

BEGIN \
{
    for (i = 1; i < ARGC; i++) {
        if (ARGV[i] ~ /[^[:digit:]]/)
            j++
    }   
    if (j || ARGC-1 != 6) {
        printf("usage: YYYY MM DD hh mm ss\n") > "/dev/stderr"
        exit 1
    }   
    else if (ARGV[1] == 0) {
        printf("There is no year 0 in the Julian system!\n") > "/dev/stderr"
        exit 1
    }   
   
    Y  = ARGV[1]; M  = ARGV[2]; D  = ARGV[3]
    HH = ARGV[4]; MM = ARGV[5]; SS = ARGV[6]
   
    printf("%f\n", 367*Y - int(7*(Y + int((M+9)/12))/4) \
                   - int(3*(int((Y+(M-9)/7)/100)+1)/4) \
                   + int(275*M/9) + D + 1721028.5 \
                   + (HH+(MM+SS/60)/60)/24)
}


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP