Convert unix timestamp to julian

2019-02-05 23:55发布

问题:

How can I convert from a unix timestamp (say 1232559922) to a fractional julian date (2454853.03150).

I found a website ( http://aa.usno.navy.mil/data/docs/JulianDate.php ) that performs a similar calculation but I need to do it programatically.

Solutions can be in C/C++, python, perl, bash, etc...

回答1:

The Unix epoch (zero-point) is January 1, 1970 GMT. That corresponds to the Julian day of 2440587.5

So, in pseudo-code:

function float getJulianFromUnix( int unixSecs )
{
   return ( unixSecs / 86400.0 ) + 2440587.5;
}


回答2:

I know that this is an old post, but I'll just say ...

The answer given by Jason Cohen is a good approximation of the conversion.

There is a problem though that relates to the number of seconds in one day. A day is not -exactly- 86400 seconds long, and periodically seconds are added to days in order to keep time synchronized with the various observable standards. These are called Leap Seconds (https://en.wikipedia.org/wiki/Leap_second). Leap seconds are added to UTC in order to keep it within 1 second of UT1.

It stands to reason that as more and more time has elapsed since Jan 1, 1970, the simple conversion above will accrue more and more error from "actual observable time." Between 1972 and 2013 there were added 25 leap seconds.

Part of the beauty and the simplicity of Julian Day numbers is that they don't represent date strings at all. They are just a count of elapsed time since the start of the Julian Epoch, much like POSIX time is a continuous count of milliseconds since the POSIX Epoch. The only problem that exists, then, is when you try to map a Julian Day number to a localized date string.

If you need a date string that is accurate to within a minute (in 2013), then you'll need an algorithm that can account for leap seconds.



回答3:

Here is my JavaScript code to convert Unix timestamp to Julian. Originally is showing the current date and time, but with a little mod is answer to your question:

function computeJulianDate(DD,MM,YY,HR,MN,SC) {
    with (Math) {
        HR = HR + (MN / 60) + (SC/3600);
        GGG = 1;
        if (YY <= 1585) GGG = 0;
        JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
        S = 1;
        if ((MM - 9)<0) S=-1;
        A = abs(MM - 9);
        J1 = floor(YY + S * floor(A / 7));
        J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
        JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
        JD = JD + 1721027 + 2 * GGG + 367 * YY - 0.5;
        JD = JD + (HR / 24);
    }
    return JD;
}
function getUTCDateTimeOrJD(now,jd=0) {
    var hours = now.getUTCHours();
    var minutes = now.getUTCMinutes();
    var seconds = now.getUTCSeconds()
    var month = now.getUTCMonth() + 1;
    var day = now.getUTCDate();
    var year = now.getUTCFullYear();
    if (jd==1)
        return computeJulianDate(month, day, year, hours, minutes, seconds);
    else
        return day+". "+month+". "+year+". "+hours+":"+minutes+":"+seconds;
}

var unixTime = 1473294606;

    getUTCDateTimeOrJD(new Date(unixTime*1000));
    getUTCDateTimeOrJD(new Date(unixTime*1000),1);

Working JSFiddle example here