Advertisement
Gayngel

Time elapsed between 2 unix datetimes

Sep 30th, 2018
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script gets the time that will elapse between the present time and a future date. Add the later date below.
  2.  
  3. integer tyear = 2018; // add the year
  4. integer tmonth = 10; // add the month
  5. integer tday = 30; // add the day
  6. integer thour = 00; // add the hour
  7. integer tmin = 45; //add the min
  8. integer tsec = 00; // add the seconds
  9. integer thenUnix;
  10. list nowList;
  11. integer nowUnix;
  12.  
  13.  
  14.  
  15. integer DAYS_PER_YEAR        = 365;           // Non leap year
  16. integer SECONDS_PER_YEAR     = 31536000;      // Non leap year
  17. integer SECONDS_PER_DAY      = 86400;
  18. integer SECONDS_PER_HOUR     = 3600;
  19. integer SECONDS_PER_MINUTE   = 60;
  20.  
  21. list MonthNameList = [  "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  22.                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ];
  23.  
  24. // This leap year test works for all years from 1901 to 2099 (yes, including 2000)
  25. // Which is more than enough for UnixTime computations, which only operate over the range [1970, 2038].  (Omei Qunhua)
  26. integer LeapYear( integer year)
  27. {
  28.     return !(year & 3);
  29. }
  30.  
  31. integer DaysPerMonth(integer year, integer month)
  32. {
  33.     // Compact Days-Per-Month algorithm. Omei Qunhua.
  34.     if (month == 2)      return 28 + LeapYear(year);
  35.     return 30 + ( (month + (month > 7) ) & 1);           // Odd months up to July, and even months after July, have 31 days
  36. }
  37.  
  38. integer DaysPerYear(integer year)
  39. {
  40.     return 365 + LeapYear(year);
  41. }
  42.  
  43. ///////////////////////////////////////////////////////////////////////////////////////
  44. // Convert Unix time (integer) to a Date and Time string
  45. ///////////////////////////////////////////////////////////////////////////////////////
  46.  
  47. /////////////////////////////// Unix2DataTime() ///////////////////////////////////////
  48.  
  49. list Unix2DateTime(integer unixtime)
  50. {
  51.     integer days_since_1_1_1970     = unixtime / SECONDS_PER_DAY;
  52.     integer day = days_since_1_1_1970 + 1;
  53.     integer year  = 1970;
  54.     integer days_per_year = DaysPerYear(year);
  55.  
  56.     while (day > days_per_year)
  57.     {
  58.         day -= days_per_year;
  59.         ++year;
  60.         days_per_year = DaysPerYear(year);
  61.     }
  62.  
  63.     integer month = 1;
  64.     integer days_per_month = DaysPerMonth(year, month);
  65.  
  66.     while (day > days_per_month)
  67.     {
  68.         day -= days_per_month;
  69.  
  70.         if (++month > 12)
  71.         {    
  72.             ++year;
  73.             month = 1;
  74.         }
  75.  
  76.         days_per_month = DaysPerMonth(year, month);
  77.     }
  78.  
  79.     integer seconds_since_midnight  = unixtime % SECONDS_PER_DAY;
  80.     integer hour        = seconds_since_midnight / SECONDS_PER_HOUR;
  81.     integer second      = seconds_since_midnight % SECONDS_PER_HOUR;
  82.     integer minute      = second / SECONDS_PER_MINUTE;
  83.     second              = second % SECONDS_PER_MINUTE;
  84.  
  85.     return [ year, month, day, hour, minute, second ];
  86. }
  87.  
  88. ///////////////////////////////// MonthName() ////////////////////////////
  89.  
  90. string MonthName(integer month)
  91. {
  92.     if (month >= 0 && month < 12)
  93.         return llList2String(MonthNameList, month);
  94.     else
  95.         return "";
  96. }
  97.  
  98. ///////////////////////////////// DateString() ///////////////////////////
  99.  
  100. string DateString(list timelist)
  101. {
  102.     integer year       = llList2Integer(timelist,0);
  103.     integer month      = llList2Integer(timelist,1);
  104.     integer day        = llList2Integer(timelist,2);
  105.  
  106.     return (string)day + "-" + MonthName(month - 1) + "-" + (string)year;
  107. }
  108.  
  109. ///////////////////////////////// TimeString() ////////////////////////////
  110.  
  111. string TimeString(list timelist)
  112. {
  113.     string  hourstr     = llGetSubString ( (string) (100 + llList2Integer(timelist, 3) ), -2, -1);
  114.     string  minutestr   = llGetSubString ( (string) (100 + llList2Integer(timelist, 4) ), -2, -1);
  115.     string  secondstr   = llGetSubString ( (string) (100 + llList2Integer(timelist, 5) ), -2, -1);
  116.     return  hourstr + ":" + minutestr + ":" + secondstr;
  117. }
  118.  
  119. ///////////////////////////////////////////////////////////////////////////////
  120. // Convert a date and time to a Unix time integer
  121. ///////////////////////////////////////////////////////////////////////////////
  122.  
  123. ////////////////////////// DateTime2Unix() ////////////////////////////////////
  124.  
  125. integer DateTime2Unix(integer year, integer month, integer day, integer hour, integer minute, integer second)
  126. {
  127.     integer time = 0;
  128.     integer yr = 1970;
  129.     integer mt = 1;
  130.     integer days;
  131.  
  132.     while(yr < year)
  133.     {
  134.         days = DaysPerYear(yr++);
  135.         time += days * SECONDS_PER_DAY;
  136.     }
  137.  
  138.     while (mt < month)
  139.     {
  140.         days = DaysPerMonth(year, mt++);
  141.         time += days * SECONDS_PER_DAY;
  142.     }
  143.  
  144.     days = day - 1;
  145.     time += days * SECONDS_PER_DAY;
  146.     time += hour * SECONDS_PER_HOUR;
  147.     time += minute * SECONDS_PER_MINUTE;
  148.     time += second;
  149.  
  150.     return time;
  151. }
  152.  
  153. // Convert Unix Time to SLT, identifying whether it is currently PST or PDT (i.e. Daylight Saving aware)
  154. // Omei Qunhua December 2013
  155.  
  156. list weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  157.  
  158. string Unix2PST_PDT(integer insecs)
  159. {
  160.     string str = Convert(insecs - (3600 * 8) );   // PST is 8 hours behind GMT
  161.     if (llGetSubString(str, -3, -1) == "PDT")     // if the result indicates Daylight Saving Time ...
  162.         str = Convert(insecs - (3600 * 7) );      // ... Recompute at 1 hour later
  163.     return str;
  164. }
  165.  
  166. // This leap year test is correct for all years from 1901 to 2099 and hence is quite adequate for Unix Time computations
  167.  
  168.  
  169. string Convert(integer insecs)
  170. {
  171.     integer w; integer month; integer daysinyear;
  172.     integer mins = insecs / 60;
  173.     integer secs = insecs % 60;
  174.     integer hours = mins / 60;
  175.     mins = mins % 60;
  176.     integer days = hours / 24;
  177.     hours = hours % 24;
  178.     integer DayOfWeek = (days + 4) % 7;    // 0=Sun thru 6=Sat
  179.  
  180.     integer years = 1970 +  4 * (days / 1461);
  181.     days = days % 1461;                  // number of days into a 4-year cycle
  182.  
  183.     @loop;
  184.     daysinyear = 365 + LeapYear(years);
  185.     if (days >= daysinyear)
  186.     {
  187.         days -= daysinyear;
  188.         ++years;
  189.         jump loop;
  190.     }
  191.     ++days;
  192.  
  193.     for (w = month = 0; days > w; )
  194.     {
  195.         days -= w;
  196.         w = DaysPerMonth(years, ++month);
  197.     }
  198.     string str =  ((string) years + "-" + llGetSubString ("0" + (string) month, -2, -1) + "-" + llGetSubString ("0" + (string) days, -2, -1) + "-"+
  199.     llGetSubString ("0" + (string) hours, -2, -1) + "-" + llGetSubString ("0" + (string) mins, -2, -1)+ "-" + llGetSubString ("0" + (string) secs, -2, -1) );
  200.  
  201.     integer LastSunday = days - DayOfWeek;
  202.     string PST_PDT = " PST";                  // start by assuming Pacific Standard Time
  203.     // Up to 2006, PDT is from the first Sunday in April to the last Sunday in October
  204.     // After 2006, PDT is from the 2nd Sunday in March to the first Sunday in November
  205.     if (years > 2006 && month == 3  && LastSunday >  7)     PST_PDT = " PDT";
  206.     if (month > 3)                                          PST_PDT = " PDT";
  207.     if (month > 10)                                         PST_PDT = " PST";
  208.     if (years < 2007 && month == 10 && LastSunday > 24)     PST_PDT = " PST";
  209.     return (str);
  210. }
  211.  
  212. default
  213. {
  214.     touch_start(integer total_number)
  215.     {
  216.         llOwnerSay("Date: " +  (string)Unix2DateTime(llGetUnixTime())); // displays date as DD-MON-YYYY
  217.      
  218.        
  219.         list nstring = llParseString2List(Unix2PST_PDT(llGetUnixTime()),["-"],[""]);
  220.         integer nyear = (integer)llList2String(nstring,0);
  221.         integer nmonth = (integer)llList2String(nstring,1);
  222.         integer nday = (integer)llList2String(nstring,2);
  223.         integer nhour = (integer)llList2String(nstring,3);
  224.         integer nmin = (integer)llList2String(nstring,4);
  225.         integer nsec = (integer)llList2String(nstring,5);
  226.        
  227.    
  228.         nowUnix = DateTime2Unix(nyear,nmonth,nday,nhour,nmin,nsec);
  229.        
  230.        
  231.        
  232.         llOwnerSay((string)nyear + " " +(string)nmonth + " " +(string)nday + " " +(string)nhour + " " +(string)nmin + " " +(string)nsec);
  233.         llOwnerSay("now unix is " + (string)nowUnix);
  234.        
  235.         thenUnix = DateTime2Unix(tyear,tmonth,tday,thour,tmin,tsec);
  236.         integer elapsed = thenUnix - nowUnix;
  237.        
  238.         elapsed -=3660;
  239.        
  240.         integer ehours = elapsed/3660;
  241.         elapsed = elapsed %=3600;
  242.         integer emins = elapsed /60;
  243.         elapsed = elapsed %= 60;
  244.         integer esecs = elapsed;
  245.        
  246.        
  247.        
  248.         llOwnerSay("time elapsed is " + (string)ehours+ " hours "+ (string)emins + "mins " + (string)esecs + "secs");
  249.        
  250.        
  251.          
  252.        
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement