Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Library: Time & Date Functions

GarrMe Dagger
LSL Manipulator
Join date: 9 Oct 2007
Posts: 6
10-14-2007 20:50
Here's a list of Time & Date functions which are LSL versions of
some of the VB functions:

CODE

///////////////////////////////////////////////////////////
// GarrMe Dagger's Time & Date Functions
// v10.13.07 Copyright (c) 2007 Mark Timlin
///////////////////////////////////////////////////////////
//
// This program is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// General Public License as published by the Free
// Software Foundation version 3 of the License.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details:
// http://www.gnu.org/licenses/
//
///////////////////////////////////////////////////////////

// Returns the current millisecond.
integer TimeMillisecond()
{
list time = llParseString2List(llGetTimestamp(), ["."], []);
llOwnerSay(llGetTimestamp());
return (integer)llGetSubString(llList2String(time, 1), 0, 1);
}

// Returns the current seconds.
integer TimeSecond()
{
list time = llParseString2List(llGetTimestamp(), ["T",":","Z"], []);
return llList2Integer(time, 3);
}

// Returns the current minutes.
integer TimeMinute()
{
list time = llParseString2List(llGetTimestamp(), ["T",":","Z"], []);
return llList2Integer(time, 2);
}

// Returns the current hour (in military time).
integer TimeHour()
{
list time = llParseString2List(llGetTimestamp(), ["T",":","Z"], []);
return llList2Integer(time, 1);
}

// Returns the time for a specific hour, minute, and second in the format HH:MM:SS AM/PM.
string TimeSerial(integer hour, integer minute, integer second)
{
string AMPM = "AM";
string min = (string)minute;
string sec = (string)second;
if (hour > 12)
{
AMPM = "PM";
hour -= 12;
}
if (minute < 10) min = "0" + min;
if (second < 10) sec = "0" + sec;
return (string)hour + ":" + min + ":" + sec + " " + AMPM;
}

// Returns the current day.
integer DateDay()
{
list time = llParseString2List(llGetTimestamp(), ["-", "T"], []);
return (integer)llList2Float(time, 2);
}

// Returns the current month.
integer DateMonth()
{
list time = llParseString2List(llGetTimestamp(), ["-", "T"], []);
return (integer)llList2Float(time, 1);
}

// Returns the current year.
integer DateYear()
{
list time = llParseString2List(llGetTimestamp(), ["-", "T"], []);
return (integer)llList2Float(time, 0);
}

// Returns the name of a specified month. If abbreviate is TRUE only the 1st 3 letters of
// the month will be returned.
string DateMonthName(integer month, integer abbreviate)
{
list name = ["January", "February", "March", "April", "May", "June", "July", "August",
"September", "November", "December"];
string str = llGetSubString(llList2String(name, month - 1), 0, -1);
if (abbreviate)
str = llGetSubString(str, 0, 2);
return str;
}

// Returns the name of the day of the week for a given date. If abbreviate is TRUE
// only the 1st 3 letters of the day will be returned.
// * Uses Christain Zeller's congruence algorithm to calculate the day of the week.
string DateDayName(integer month, integer day, integer year, integer abbreviate)
{
list name = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
if (month < 3)
{
month += 12;
year -= 1;
}
integer index = (day + 2*month + (3*(month + 1))/5 + year + year/4 - year/100 + year/400 + 2) % 7;
string str = llGetSubString(llList2String(name, index), 0, -1);
if (abbreviate)
str = llGetSubString(str, 0, 2);
return str;
}

default
{
state_entry()
{
// Example usage of these functions
llOwnerSay("Today is: " + DateDayName(DateMonth(), DateDay(), DateYear(), FALSE) + " " +
DateMonthName(DateMonth(), FALSE) + " " + (string)DateDay() + ", " + (string)DateYear() +
" and the time is: " + TimeSerial(TimeHour(), TimeMinute(), TimeSecond()));
}
}
Ollj Oh
Registered User
Join date: 28 Aug 2007
Posts: 522
12-25-2007 00:48
uses llParseString2List over and over in every procedure while once every read should be enough, like, create the list just ONCE. But you dont need to create a list at all because I doubt that;

list time = llParseString2List(llGetTimestamp(), ["T",":","Z"], []);
return llList2Integer(time, 2);

is faster than:

return (integer)(llGetSubString( llGetTimestamp(), 0, 3 ));

//gets the year as integer.
// "YYYY-MM-DDThh:mm:ss.ff..fZ"
// "0123-56-89012-45-78--12"
Infrared Wind
Gridologist
Join date: 7 Jan 2007
Posts: 662
11-03-2008 05:19
list "name" is missing the month October.

-i
_____________________
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
11-03-2008 06:28
small code functions really don't need or require anything as complex or formal as a GPL license. and given the proclivities of some SL users, it's also kind of pointless. best suggestion: ask for credit, ask that it remain open mod, and celebrate if you ever find it that way... cause in SL, there's really no way to check
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -