Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Astronimical Julian Dates

Pale Spectre
Registered User
Join date: 2 Sep 2005
Posts: 586
09-08-2006 16:20
Astronimical Julian Dates

I've noticed some discussion about date functions. This is a specimen script that shows the use of two fundermental date functions:

DateToAJD: converts a regular date to an Astronimcal Julian Date.
AJDToDate: converts an Astronomical Julian Date to a regular date.

The cool thing about Astronomical Julian Dates is that they are a count of days from a common point in time. So, for example, you can add them together, subtract one from another, increment or decrement them by any number of days, and so on.

Watch out for the date format (which is easily changed - it's only a parameter thing). I'm in the UK and it's day/month/year - the Americans have provided the world with many wonderful things but dates formats aren't one of them. :D

CODE
integer listenHandle;
integer listenChannel = 10;

// Inputs a regular date in ddmmyyyy format and returns the Astronimical Julian Date
integer DateToAJD(string Date)
{
integer Day = (integer)llGetSubString(Date, 0, 1);
integer Month = (integer)llGetSubString(Date, 2, 3);
integer Year = (integer)llGetSubString(Date, 4, 7);

if(Month > 2) Month = Month + 1;
else
{
Year = Year - 1;
Month = Month + 13;
}

integer AJD = (integer)(Year * 365.25) + (integer)(Month * 30.6001) + Day + 1720982;

llOwnerSay("Astronomical Julian Date for:" + llGetSubString(Date, 0, 1) + "/" + llGetSubString(Date, 2, 3) + "/" + llGetSubString(Date, 4, 7) + " is:" + (string)AJD);

return(AJD);
}

// Inputs an Astronimical Julian Date date and returns a regular date in dd/mm/yyyy format
string AJDToDate(integer AJD)
{
string Date;

integer iTemp;
integer dTemp;
integer mTemp;
integer yTemp;
integer Day;
integer Month;
integer Year;

iTemp = AJD + 1537;
yTemp = (integer)(((iTemp - 2439992.1) / 365.25 ) + 6680);
mTemp = (integer)((iTemp - (integer)(yTemp * 365.25)) / 30.6001);
dTemp = (integer)((iTemp - (integer)(yTemp * 365.25)));

Day = dTemp - (integer)(mTemp * 30.6001);
Month = mTemp - 1;
if(Month > 12) Month = Month - 12;
Year = yTemp - 4715;
if(Month > 2) Year = Year - 1;

Date = (string)Day + "/" + (string)Month + "/" + (string)Year;
llOwnerSay("Date for:" + (string)AJD + " is:" + Date);

return(Date);
}

// Test rig
init()
{
llListenRemove(listenHandle);
listenHandle = llListen(listenChannel,"",llGetOwner(),"");
}

default
{
state_entry()
{
init();
}

on_rez(integer start_param)
{
init();
}

listen(integer channel, string name, key id, string message)
{
message = llToLower(message);

if(llGetSubString(llToLower(message), 0, 3) == "date") DateToAJD(llGetSubString(message, 5, -1));
else if(llGetSubString(llToLower(message), 0, 2) == "ajd") AJDToDate((integer)(llGetSubString(message, 4, -1)));
else llOwnerSay("You said:" + message + "> ...I was expecting: date <ddmmyyyy> or: ajd <astronomical julian date>");
}
}

Example of use:
/10 date 08092006
will return: Astronomical Julian Date for:08/09/2006 is:2453987

/10 ajd 2453987
will return: Date for:2453987 is:8/9/2006

I have translated these routines from published routines on another platform. Don't ask me why they work - I have no idea - they just do. :D

The original routines (which I stripped down many years ago) were more complex and included a lot of code for pre-Gregorian dates. This pair will only work for post-Gregorian dates. Which is probably going to be quite sufficient for use within SL.

Someone may be able to package them more elegantly than me but it's the amazingly cunning math that counts!
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
09-10-2006 13:33
/15/4f/136005/1.html
_____________________
i've got nothing. ;)