Difference between revisions of "TimeAgo"

From Second Life Wiki
Jump to navigation Jump to search
(for great justice (and a scone))
m (some minor readability changes)
Line 7: Line 7:
|func_footnote=
|func_footnote=
See also: [[llGetUnixTime]]
See also: [[llGetUnixTime]]
|spec=<lsl>
|examples=<lsl>//Created by Ugleh Ulrik
//Created by Ugleh Ulrik
 
string TimeAgo(integer tm) {
string TimeAgo(integer time)
     integer dif = llGetUnixTime()-tm; //difference in seconds
{
     if (dif == 0)//Small bug fix if dif is 0
    // time difference in seconds
         return "0 seconds";
     integer now = llGetUnixTime();
     list pds = ["second","minute","hour","day","week","month","year","decade"];
    integer timeDifference = now - time;
     list lngh = [1,60,3600,86400,604800,2630880,31570560,315705600];//the number equivalent to pds
 
     integer v = llGetListLength(lngh)-1;
    // small bug fix for when diff is 0
     if (timeDifference == 0)
         return "just now";
 
     list periods = ["second",
        "minute",
        "hour",
        "day",
        "week",
        "month",
        "year",
        "decade"];
 
    //the number equivalent to pds
     list lenghts = [1,
        60,
        3600,
        86400,
        604800,
        2630880,
        31570560,
        315705600];
 
     integer v = llGetListLength(lenghts) - 1;
     integer no;
     integer no;
     while((v >= 0)&&(no = dif/llList2Integer(lngh,v)<=1))
 
     while((0 <= v) && (no = timeDifference/llList2Integer(lenghts, v) <= 1))
         --v;
         --v;
     string dd = llList2String(pds,v);
 
     integer ntime = dif / llList2Integer(lngh,llListFindList(pds, [dd])); //this will get the correct time in pds, then divide the dif
     string output = llList2String(periods, v);
     if(no != 1)//if integer 'no' is not equal to 1 then it should have an s at the end
 
         dd +="s";
     //this will get the correct time in pds, then divide the dif
     dd = (string)ntime + " "+ dd; //This produces the finished output
     integer ntime = timeDifference / llList2Integer(lenghts, llListFindList(periods, [output]));
     return dd;
 
}  
    //if integer 'no' is not equal to 1 then it should have an s at the end
</lsl>
    if(no != 1)
|examples=<lsl>
         output += "s";
//Created by Ugleh Ulrik
 
     //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 time = llGetUnixTime() - 180;
         integer now = llGetUnixTime();
         llSay(0,  TimeAgo(time) + " ago");
         integer time = now - 180;
        }
}


        // integer time is the current unix time minus 180 seconds (3 minutes)
        // the outcome will be "3 minutes ago"


//integer time is the current unix time minus 180 seconds (3 minutes)
        llSay(PUBLIC_CHANNEL,  TimeAgo(time));
//The outcome will be "3 minutes ago"</lsl>
    }
}
</lsl>
|helpers
|helpers
|notes
|notes

Revision as of 11:21, 30 September 2012

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

<lsl>//Created by Ugleh Ulrik

string TimeAgo(integer time) {

   // time difference in seconds
   integer now = llGetUnixTime();
   integer timeDifference = now - time;
   // small bug fix for when diff is 0
   if (timeDifference == 0)
       return "just now";
   list periods = ["second",
       "minute",
       "hour",
       "day",
       "week",
       "month",
       "year",
       "decade"];
   //the number equivalent to pds
   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 pds, then divide the dif
   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));
   }

}

</lsl>