Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

leading zeros

Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-08-2006 19:47
Ok, so today I was going to work on my script, but every time i get started something comes up.. it's been a trying day.. appointments, chores, ppl needing rides.. and now.. when I get sat down they take the wikki offline.. frustrating.. ah well..

Now that I ranted, let me ask my question.. I had just finished writting the following function which is supposed to return the time in 'hh:mm' format, with leading zeros.

CODE

// fncSec2Time does the opposite of the above. It converts integer of seconds to a string
// formatted as "hh:mm".
string fncSec2Time(integer intSeconds)
{
integer intHours = intSeconds / 3600; // Get hours
integer intMins = (intSeconds % 3600) / 60; // And minutes

string strTime = "";
if (llStringLength((string)intHours) < 2)
{
strTime = strTime + "0";
}

strTime = strTime + (string)intHours + ":";

if (llStringLength((string)intMins) < 2)
{
strTime = strTime + "0";
}

strTime = strTime + (string)intMins;

return strTime;
}


Now, as i go through that, I know it'll work, but it looks way too long, inefficient.. There's gotta be some way to clean that up some. (I know I could use strTime+= args; but i'm trying to see if this form will save memory, like it does with lists.)

I wouldn't mind being able to do the math, add leading zeros, and create the string all in one shot, but I also know that I need to keep it readable. I was looking at the llDumpString2List function in the wikki when they suddenly took it offline. My thought at the moment was that I could create a list such as ["leading zero", intHours, ":", "leading zero", intMins] and dump it into a string quickly.. That would make for some cleaner code.

But then comes the fact that I gotta check if a leading zero is needed at all. If Hours is less than 10, or if Mins is left than 10, then yes i need leading zero, otherwise no i don't.

anyone have any idea's of how I could do this effeciently?

:note: oh, and yes, I realize this would return 24 hour clock format, 0 through 23 instead of 1 through 12 am/pm. That's ok, as that's what I'm going to use for this script anyway.
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
09-08-2006 20:08
From: Aakanaar LaSalle
anyone have any idea's of how I could do this effeciently?


return llGetSubstring( "0" + (string)intHours, -2, -1 ) + ":" + llGetSubstring( "0" + (string)intMins, -2, -1 );

... maybe? Although if it's about current time you could possibly just slice the relevant part of llGetTimestamp() and pass it as it is o.O;
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-08-2006 20:17
From: Joannah Cramer
return llGetSubstring( "0" + (string)intHours, -2, -1 ) + ":" + llGetSubstring( "0" + (string)intMins, -2, -1 );

... maybe? Although if it's about current time you could possibly just slice the relevant part of llGetTimestamp() and pass it as it is o.O;


Took me a moment to see what you were doing, but that does work nicely. Thanks. It may even be better than using that DumpList2String idea I had.

As for the timestamp, I'm storing the time in a list, for comparing against later. (basically to make sure the second time is at least 15 minutes later than the first time. Though even that will be adjustable by owner configuration) So I thought it would be easier to store from llGetWallClock and convert to 'hh:mm' format at the end of the night, when emailing the times to the owner.

but actually, since I have a function already that does the exact opposite, I guess I could work the other way and store the time in "hh:mm" format, then when I'm comparing convert both times to seconds.

I'll have to give this some thought.
Thanks.

Edit: yea, had to take a look at the overall script again, I've got some configurations where the end user will provide hours and minutes for delays etc, using "hh:mm" format.. well ok.. mainly delays will be minutes, not hours, but it'll also allow the user to set time slots throughout the day, again using hh:mm format. (24 hour clock)

So I made a function to convert hh:mm to seconds for comparing against llGetWallClock. and figgured I should store times for comparisons in the same way, as seconds.. so I only need to convert back to hh:mm format at the end of the day.
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
09-08-2006 20:36
From: Aakanaar LaSalle
As for the timestamp, I'm storing the time in a list, for comparing against later. (basically to make sure the second time is at least 15 minutes later than the first time. Though even that will be adjustable by owner configuration) So I thought it would be easier to store from llGetWallClock and convert to 'hh:mm' format at the end of the night, when emailing the times to the owner.

but actually, since I have a function already that does the exact opposite, I guess I could work the other way and store the time in "hh:mm" format, then when I'm comparing convert both times to seconds.

I'll have to give this some thought.

Ohh. I use llGetUnixTime() for this sort of comparison/sorting. You get current time in a single integer as amount of seconds since whatever, so it's very easy to do a check if new value returned by llGetUnixTime() is x seconds (say, 15 * 60 in your example) larger than cached value you store when you get your first timestamp ^^;;
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-08-2006 20:55
Yea, i'm trying not to give too much info on this project, as it's supposed to be done low key.. trying to avoid industrial espionage (sp?) but I will explain this much.

Basically, I'm keeping a list of people who interact with this object. The list will contain their key (for comparing against), their name, how many times they've interacted with the object today, and time of last interaction.

If someone interacts with the object, it's supposed to look for them in the list, by key, and if found check the time of last interaction. If a certain amount of time (default will be 15 minutes, but could be configured to any amount of time) has passed since the last interaction, I update the count and put in new "last time". If the amount hasn't passed, I just update the "last time".

At the end of the day, (I'll have a timer checking every 5 min or so, to see if it's passed midnight) I'll email the owner the information and clear the list.

The reason I'm using llGetWallClock instead of llGetUnixTime is because the former gives me number of seconds since midnight PST, as opposed to getting number of seconds since 1970.

If that makes sense at all.
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
09-09-2006 03:35
Would be great if we had sprintf!!!