Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Dataserver string to integer help

Travis Lambert
White dog, red collar
Join date: 3 Jun 2004
Posts: 2,819
10-19-2005 13:37
Consider the following code:

CODE

default
{
touch_start(integer num_detected)
{
llRequestAgentData(llDetectedKey(0), DATA_BORN);
}
dataserver(key kQueryid, string sData)
{
llSay(0, "You were born on: " + sData);
}
}


Dataserver returns the born date in the format YYYY-MM-DD, in a string.

I'd like to do some math against that string - such as...

iDays_old = (llGetDate() - sData)

I thought of pumping sData into a list, then parsing it somehow & doing a llList2Integer - but my list comprehension is shakey.

Any ideas on how I could convert the string that's returned from Dataserver into a usable integer? Ultimately, so I can get the number of days old an AV is as of today?


Thanks in advance for your help! :)
_____________________
------------------
The Shelter

The Shelter is a non-profit recreation center for new residents, and supporters of new residents. Our goal is to provide a positive & supportive social environment for those looking for one in our overwhelming world.
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
10-19-2005 14:07
Well, if a - character is used as the separator, you can convert the string into a list by telling it to use '-' as the separator. That'll give you a list of 3 strings, which you can typecast to integers to get the 3 values for year, month and date. After that it's just math :)

CODE

list dateAsList = llParseString2List(sData, "-", "");

integer year = llList2Integer(dateAsList, 0);
integer month = llList2Integer(dateAsList, 1);
integer date = llList2Integer(dateAsList, 2);
Malachi Petunia
Gentle Miscreant
Join date: 21 Sep 2003
Posts: 3,414
post race conditions
10-19-2005 14:16
CODE
default 
{
touch_start(integer num_detected)
{
llRequestAgentData(llDetectedKey(0), DATA_BORN);
}
dataserver(key kQueryid, string sData)
{
list ymd = llParseString2List(sData, ["-"], []);
integer year = llList2Integer(ymd, 0);
integer month = llList2Integer(ymd, 1);
integer day = llList2Integer(ymd, 2);
llSay(0, "You were born on: " + ((string) month) + "/" + ((string) day) + "/" + ((string) year));
}
}
Tested. There is an implicit cast in llList2Integer() which saves you here because llGetListEntryType(ymd) would return TYPE_STRING.
_____________________
Fearless Leader
Sycophant
Join date: 21 Dec 2004
Posts: 19
10-19-2005 14:22
Once you have year, month, and day of both today and the user's birthdate, the easiest way to calculate the range is probably to convert both dates to Julian day numbers and subtract. I hacked the LSL code for this a while ago, but I'm away from my home computer. :-( The algorithm is described here: http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html

You're wanting to go from a Gregorian date to a Julian day number.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
10-19-2005 18:06
llGetTimestamp() <-> unix timestamp
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Travis Lambert
White dog, red collar
Join date: 3 Jun 2004
Posts: 2,819
10-19-2005 19:35
Yay!! Thanks, guys! That was just the push I needed!

Here's the code I came up with, and it works! This will create a simple age detector (in days). My code is a little messy, and of course it's not accurate for a leap year. But newbies are getting a kick out of clicking it already! :)

Thanks again!

CODE
// Get Days Old
// Travis Lambert -- 10-19-2005 (504 days old) :)


list lTdy;
list lYmd;
float fYear;
float fMonth;
float fDay;
integer iDayTotal;
float fYMonth;
float fYDay;
float fBornFloat;
float fTmonth;
float fTday;
float fTodayFloat;
float fYearTotal;
string sToday;
string sName;

default
{
touch_start(integer num_detected)
{
llRequestAgentData(llDetectedKey(0), DATA_BORN);
sName = llDetectedName(0);
}
dataserver(key kQueryid, string sData)
{
//determine born date
list lYmd = llParseString2List(sData, ["-"], []);
float fYear = llList2Float(lYmd, 0);
float fMonth = llList2Float(lYmd, 1);
float fDay = llList2Float(lYmd, 2);


//seperate born month, day, year
fYMonth = (fMonth / 12);
fYDay = (fDay / 365);
fBornFloat = fYear + fYMonth + fYDay;


//determine today's date
string sToday = llGetDate();
list lTdy = llParseString2List(sToday, ["-"], []);
float tyear = llList2Float(lTdy, 0);
float tmonth = llList2Float(lTdy, 1);
float tday = llList2Float(lTdy, 2);

//seperate today's month, day, year
fTmonth = (tmonth / 12);
fTday = (tday / 365);
fTodayFloat = tyear + fTmonth + fTday;


//total up & compute
fYearTotal = fTodayFloat - fBornFloat;
iDayTotal = llRound((float) fYearTotal * 365.0);

llSay(0,sName + " is " + (string) iDayTotal + " Days Old!");



}
}
_____________________
------------------
The Shelter

The Shelter is a non-profit recreation center for new residents, and supporters of new residents. Our goal is to provide a positive & supportive social environment for those looking for one in our overwhelming world.