Difference between revisions of "TimeAgo"
Jump to navigation
Jump to search
Ugleh Ulrik (talk | contribs) |
m (<lsl> tag to <source>) |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 7: | Line 7: | ||
|func_footnote= | |func_footnote= | ||
See also: [[llGetUnixTime]] | See also: [[llGetUnixTime]] | ||
| | |examples= | ||
<source lang="lsl2"> | |||
//Created by Ugleh Ulrik | //Created by Ugleh Ulrik | ||
string TimeAgo(integer time) | |||
{ | |||
// time difference in seconds | |||
integer now = llGetUnixTime(); | |||
integer timeDifference = now - time; | |||
// small bug fix for when timeDifference is 0 | |||
if (timeDifference == 0) | |||
return "just now"; | |||
list periods = ["second", | |||
"minute", | |||
"hour", | |||
"day", | |||
"week", | |||
"month", | |||
"year", | |||
"decade"]; | |||
//the number equivalent to periods | |||
list lenghts = [1, | |||
60, | |||
3600, | |||
86400, | |||
604800, | |||
2630880, | |||
31570560, | |||
315705600]; | |||
integer v = llGetListLength(lenghts) - 1; | |||
integer no; | |||
while((0 <= v) && (no = timeDifference/llList2Integer(lenghts, v) <= 1)) | |||
--v; | |||
string output = llList2String(periods, v); | |||
//this will get the correct time in periods, then divide the timeDifference | |||
integer ntime = timeDifference / llList2Integer(lenghts, llListFindList(periods, [output])); | |||
//if integer 'no' is not equal to 1 then it should have an s at the end | |||
if(no != 1) | |||
output += "s"; | |||
//This produces the finished output | |||
output = (string)ntime + " "+ output + " ago"; | |||
return output; | |||
} | |||
default | default | ||
{ | { | ||
touch_start(integer total_number) | touch_start(integer total_number) | ||
{ | { | ||
integer | integer now = llGetUnixTime(); | ||
integer time = now - 180; | |||
// integer time is the current unix time minus 180 seconds (3 minutes) | |||
// the outcome will be "3 minutes ago" | |||
llSay(PUBLIC_CHANNEL, TimeAgo(time)); | |||
} | |||
} | |||
</source> | |||
|helpers | |helpers | ||
|notes | |notes | ||
Line 43: | Line 81: | ||
|also_articles | |also_articles | ||
|cat1=Examples | |cat1=Examples | ||
|cat2 | |cat2=User-Defined_Functions | ||
|cat3 | |cat3 | ||
|cat4 | |cat4 | ||
}} | }} |
Latest revision as of 14:43, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: string TimeAgo( integer tm );Returns a string that will give you the difference in time.
• integer | tm | – | Unix Time used for difference |
See also: llGetUnixTime
Examples
//Created by Ugleh Ulrik
string TimeAgo(integer time)
{
// time difference in seconds
integer now = llGetUnixTime();
integer timeDifference = now - time;
// small bug fix for when timeDifference is 0
if (timeDifference == 0)
return "just now";
list periods = ["second",
"minute",
"hour",
"day",
"week",
"month",
"year",
"decade"];
//the number equivalent to periods
list lenghts = [1,
60,
3600,
86400,
604800,
2630880,
31570560,
315705600];
integer v = llGetListLength(lenghts) - 1;
integer no;
while((0 <= v) && (no = timeDifference/llList2Integer(lenghts, v) <= 1))
--v;
string output = llList2String(periods, v);
//this will get the correct time in periods, then divide the timeDifference
integer ntime = timeDifference / llList2Integer(lenghts, llListFindList(periods, [output]));
//if integer 'no' is not equal to 1 then it should have an s at the end
if(no != 1)
output += "s";
//This produces the finished output
output = (string)ntime + " "+ output + " ago";
return output;
}
default
{
touch_start(integer total_number)
{
integer now = llGetUnixTime();
integer time = now - 180;
// integer time is the current unix time minus 180 seconds (3 minutes)
// the outcome will be "3 minutes ago"
llSay(PUBLIC_CHANNEL, TimeAgo(time));
}
}