Difference between revisions of "Input number of seconds, get a string back that shows days, hours, minutes, seconds"

From Second Life Wiki
Jump to: navigation, search
m (increasing readability, adding syntax highlighting)
m (<lsl> tag to <source>)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
<lsl>  /*
+
<source lang="lsl2">  /*
 
   *  Submitted Opensource under GPL 3.0
 
   *  Submitted Opensource under GPL 3.0
 
   *  2010 Fire Centaur
 
   *  2010 Fire Centaur
Line 5: Line 5:
 
   *   
 
   *   
 
   *  Input number of seconds, function will return a string with Days, Hours, Minutes, Seconds
 
   *  Input number of seconds, function will return a string with Days, Hours, Minutes, Seconds
 +
  *  Corrections by Traven Sachs to Display Correct Output allowing for 0 seconds or more than 1 day
 +
  *    and improve readability with whitespace 19-Feb-2011
 
   */
 
   */
  
string getTime(integer secs){
+
string getTime(integer secs)
 +
{
 
     string timeStr;
 
     string timeStr;
 
     integer days;
 
     integer days;
 
     integer hours;
 
     integer hours;
 
     integer minutes;
 
     integer minutes;
   
+
     if (secs>=86400){
+
     if (secs>=86400)
 +
    {
 
         days=llFloor(secs/86400);
 
         days=llFloor(secs/86400);
 
         secs=secs%86400;
 
         secs=secs%86400;
         timeStr=(string)days+" day";
+
         timeStr+=(string)days+" day";
         if (days!=1) {timeStr="s";}
+
         if (days>1)  
         if(secs>0) {timeStr+=", ";}
+
        {
 +
            timeStr+="s";
 +
        }
 +
         if(secs>0)  
 +
        {
 +
            timeStr+=", ";
 +
        }
 
     }
 
     }
     if(secs>=3600){
+
     if(secs>=3600)
 +
    {
 
         hours=llFloor(secs/3600);
 
         hours=llFloor(secs/3600);
 
         secs=secs%3600;
 
         secs=secs%3600;
 
         timeStr+=(string)hours+" hour";
 
         timeStr+=(string)hours+" hour";
         if(hours!=1){timeStr+="s";}
+
         if(hours!=1)
         if(secs>0){timeStr+=", ";}
+
        {
 +
            timeStr+="s";
 +
        }
 +
         if(secs>0)
 +
        {
 +
            timeStr+=", ";
 +
        }
 
     }
 
     }
     if(secs>=60){
+
     if(secs>=60)
 +
    {
 
         minutes=llFloor(secs/60);
 
         minutes=llFloor(secs/60);
 
         secs=secs%60;
 
         secs=secs%60;
 
         timeStr+=(string)minutes+" minute";
 
         timeStr+=(string)minutes+" minute";
         if(minutes!=1){timeStr+="s";}
+
         if(minutes!=1)
         if(secs>0){timeStr+=", ";}
+
        {
 +
            timeStr+="s";
 +
        }
 +
         if(secs>0)
 +
        {
 +
            timeStr+=", ";
 +
        }
 +
    }
 +
    if (secs>0)
 +
    {
 +
        timeStr+=(string)secs+" second";
 +
        if(secs!=1)
 +
        {
 +
            timeStr+="s";
 +
        }
 
     }
 
     }
    timeStr+=(string)secs+" second";
 
    if(secs!=1){timeStr+="s";}
 
 
     return timeStr;
 
     return timeStr;
}</lsl>
+
}
 +
</source>

Latest revision as of 09:34, 25 January 2015

  /*
  *  Submitted Opensource under GPL 3.0
  *  2010 Fire Centaur
  *  Description: 
  *  
  *  Input number of seconds, function will return a string with Days, Hours, Minutes, Seconds
  *  Corrections by Traven Sachs to Display Correct Output allowing for 0 seconds or more than 1 day 
  *     and improve readability with whitespace 19-Feb-2011
  */
 
string getTime(integer secs)
{
    string timeStr;
    integer days;
    integer hours;
    integer minutes;
 
    if (secs>=86400)
    {
        days=llFloor(secs/86400);
        secs=secs%86400;
        timeStr+=(string)days+" day";
        if (days>1) 
        {
            timeStr+="s";
        }
        if(secs>0) 
        {
            timeStr+=", ";
        }
    }
    if(secs>=3600)
    {
        hours=llFloor(secs/3600);
        secs=secs%3600;
        timeStr+=(string)hours+" hour";
        if(hours!=1)
        {
            timeStr+="s";
        }
        if(secs>0)
        {
            timeStr+=", ";
        }
    }
    if(secs>=60)
    {
        minutes=llFloor(secs/60);
        secs=secs%60;
        timeStr+=(string)minutes+" minute";
        if(minutes!=1)
        {
            timeStr+="s";
        }
        if(secs>0)
        {
            timeStr+=", ";
        }
    }
    if (secs>0)
    {
        timeStr+=(string)secs+" second";
        if(secs!=1)
        {
            timeStr+="s";
        }
    }
    return timeStr;
}