Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This script gets the time that will elapse between the present time and a future date. Add the later date below.
- integer tyear = 2018; // add the year
- integer tmonth = 10; // add the month
- integer tday = 30; // add the day
- integer thour = 00; // add the hour
- integer tmin = 45; //add the min
- integer tsec = 00; // add the seconds
- integer thenUnix;
- list nowList;
- integer nowUnix;
- integer DAYS_PER_YEAR = 365; // Non leap year
- integer SECONDS_PER_YEAR = 31536000; // Non leap year
- integer SECONDS_PER_DAY = 86400;
- integer SECONDS_PER_HOUR = 3600;
- integer SECONDS_PER_MINUTE = 60;
- list MonthNameList = [ "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
- "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ];
- // This leap year test works for all years from 1901 to 2099 (yes, including 2000)
- // Which is more than enough for UnixTime computations, which only operate over the range [1970, 2038]. (Omei Qunhua)
- integer LeapYear( integer year)
- {
- return !(year & 3);
- }
- integer DaysPerMonth(integer year, integer month)
- {
- // Compact Days-Per-Month algorithm. Omei Qunhua.
- if (month == 2) return 28 + LeapYear(year);
- return 30 + ( (month + (month > 7) ) & 1); // Odd months up to July, and even months after July, have 31 days
- }
- integer DaysPerYear(integer year)
- {
- return 365 + LeapYear(year);
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- // Convert Unix time (integer) to a Date and Time string
- ///////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////// Unix2DataTime() ///////////////////////////////////////
- list Unix2DateTime(integer unixtime)
- {
- integer days_since_1_1_1970 = unixtime / SECONDS_PER_DAY;
- integer day = days_since_1_1_1970 + 1;
- integer year = 1970;
- integer days_per_year = DaysPerYear(year);
- while (day > days_per_year)
- {
- day -= days_per_year;
- ++year;
- days_per_year = DaysPerYear(year);
- }
- integer month = 1;
- integer days_per_month = DaysPerMonth(year, month);
- while (day > days_per_month)
- {
- day -= days_per_month;
- if (++month > 12)
- {
- ++year;
- month = 1;
- }
- days_per_month = DaysPerMonth(year, month);
- }
- integer seconds_since_midnight = unixtime % SECONDS_PER_DAY;
- integer hour = seconds_since_midnight / SECONDS_PER_HOUR;
- integer second = seconds_since_midnight % SECONDS_PER_HOUR;
- integer minute = second / SECONDS_PER_MINUTE;
- second = second % SECONDS_PER_MINUTE;
- return [ year, month, day, hour, minute, second ];
- }
- ///////////////////////////////// MonthName() ////////////////////////////
- string MonthName(integer month)
- {
- if (month >= 0 && month < 12)
- return llList2String(MonthNameList, month);
- else
- return "";
- }
- ///////////////////////////////// DateString() ///////////////////////////
- string DateString(list timelist)
- {
- integer year = llList2Integer(timelist,0);
- integer month = llList2Integer(timelist,1);
- integer day = llList2Integer(timelist,2);
- return (string)day + "-" + MonthName(month - 1) + "-" + (string)year;
- }
- ///////////////////////////////// TimeString() ////////////////////////////
- string TimeString(list timelist)
- {
- string hourstr = llGetSubString ( (string) (100 + llList2Integer(timelist, 3) ), -2, -1);
- string minutestr = llGetSubString ( (string) (100 + llList2Integer(timelist, 4) ), -2, -1);
- string secondstr = llGetSubString ( (string) (100 + llList2Integer(timelist, 5) ), -2, -1);
- return hourstr + ":" + minutestr + ":" + secondstr;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Convert a date and time to a Unix time integer
- ///////////////////////////////////////////////////////////////////////////////
- ////////////////////////// DateTime2Unix() ////////////////////////////////////
- integer DateTime2Unix(integer year, integer month, integer day, integer hour, integer minute, integer second)
- {
- integer time = 0;
- integer yr = 1970;
- integer mt = 1;
- integer days;
- while(yr < year)
- {
- days = DaysPerYear(yr++);
- time += days * SECONDS_PER_DAY;
- }
- while (mt < month)
- {
- days = DaysPerMonth(year, mt++);
- time += days * SECONDS_PER_DAY;
- }
- days = day - 1;
- time += days * SECONDS_PER_DAY;
- time += hour * SECONDS_PER_HOUR;
- time += minute * SECONDS_PER_MINUTE;
- time += second;
- return time;
- }
- // Convert Unix Time to SLT, identifying whether it is currently PST or PDT (i.e. Daylight Saving aware)
- // Omei Qunhua December 2013
- list weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
- string Unix2PST_PDT(integer insecs)
- {
- string str = Convert(insecs - (3600 * 8) ); // PST is 8 hours behind GMT
- if (llGetSubString(str, -3, -1) == "PDT") // if the result indicates Daylight Saving Time ...
- str = Convert(insecs - (3600 * 7) ); // ... Recompute at 1 hour later
- return str;
- }
- // This leap year test is correct for all years from 1901 to 2099 and hence is quite adequate for Unix Time computations
- string Convert(integer insecs)
- {
- integer w; integer month; integer daysinyear;
- integer mins = insecs / 60;
- integer secs = insecs % 60;
- integer hours = mins / 60;
- mins = mins % 60;
- integer days = hours / 24;
- hours = hours % 24;
- integer DayOfWeek = (days + 4) % 7; // 0=Sun thru 6=Sat
- integer years = 1970 + 4 * (days / 1461);
- days = days % 1461; // number of days into a 4-year cycle
- @loop;
- daysinyear = 365 + LeapYear(years);
- if (days >= daysinyear)
- {
- days -= daysinyear;
- ++years;
- jump loop;
- }
- ++days;
- for (w = month = 0; days > w; )
- {
- days -= w;
- w = DaysPerMonth(years, ++month);
- }
- string str = ((string) years + "-" + llGetSubString ("0" + (string) month, -2, -1) + "-" + llGetSubString ("0" + (string) days, -2, -1) + "-"+
- llGetSubString ("0" + (string) hours, -2, -1) + "-" + llGetSubString ("0" + (string) mins, -2, -1)+ "-" + llGetSubString ("0" + (string) secs, -2, -1) );
- integer LastSunday = days - DayOfWeek;
- string PST_PDT = " PST"; // start by assuming Pacific Standard Time
- // Up to 2006, PDT is from the first Sunday in April to the last Sunday in October
- // After 2006, PDT is from the 2nd Sunday in March to the first Sunday in November
- if (years > 2006 && month == 3 && LastSunday > 7) PST_PDT = " PDT";
- if (month > 3) PST_PDT = " PDT";
- if (month > 10) PST_PDT = " PST";
- if (years < 2007 && month == 10 && LastSunday > 24) PST_PDT = " PST";
- return (str);
- }
- default
- {
- touch_start(integer total_number)
- {
- llOwnerSay("Date: " + (string)Unix2DateTime(llGetUnixTime())); // displays date as DD-MON-YYYY
- list nstring = llParseString2List(Unix2PST_PDT(llGetUnixTime()),["-"],[""]);
- integer nyear = (integer)llList2String(nstring,0);
- integer nmonth = (integer)llList2String(nstring,1);
- integer nday = (integer)llList2String(nstring,2);
- integer nhour = (integer)llList2String(nstring,3);
- integer nmin = (integer)llList2String(nstring,4);
- integer nsec = (integer)llList2String(nstring,5);
- nowUnix = DateTime2Unix(nyear,nmonth,nday,nhour,nmin,nsec);
- llOwnerSay((string)nyear + " " +(string)nmonth + " " +(string)nday + " " +(string)nhour + " " +(string)nmin + " " +(string)nsec);
- llOwnerSay("now unix is " + (string)nowUnix);
- thenUnix = DateTime2Unix(tyear,tmonth,tday,thour,tmin,tsec);
- integer elapsed = thenUnix - nowUnix;
- elapsed -=3660;
- integer ehours = elapsed/3660;
- elapsed = elapsed %=3600;
- integer emins = elapsed /60;
- elapsed = elapsed %= 60;
- integer esecs = elapsed;
- llOwnerSay("time elapsed is " + (string)ehours+ " hours "+ (string)emins + "mins " + (string)esecs + "secs");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement