Convert back GetDateTimeString()

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am
Location: Italy

Convert back GetDateTimeString()

Post by xeon »

Someone has a function to convert back to seconds from 1970 a string returned by GetDateTimeString(), es. '2011-02-17 23:34:30' ?
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am
Location: Italy

Re: Convert back GetDateTimeString()

Post by xeon »

Nevermind, I just did it.

Code: Select all

// Return the number of seconds from epoch respectfully to the input string.
// Input typically is from GetDateTimeString(), es: '2011-02-17 23:34:30'
// The epoch is January 1, 1970, 00:00:00 GMT.
// This function assure you this:
// var nSystime := polcore().systime + 60*SHARD_TIME_ZONE;
// var sGDTS := GetDateTimeString();
// var nBack := DecodeDateTimeString(sGDTS);
// nBack == nSystime
function DecodeDateTimeString(sString)

  var arString := SplitWords(sString);
  var arDate   := SplitWords(arString[1], "-");
  var arTime   := SplitWords(arString[2], ":");

  var time := 0;
  time :=            CInt(arTime[3]) 
         +      60 * CInt(arTime[2])
         + 60 * 60 * CInt(arTime[1]);

  var nYears := CInt(arDate[1]) - 1970;
  var c, nSecsOfYear := 0;
  for (c := 1970; c < CInt(arDate[1]); c := c + 1)
    nSecsOfYear := nSecsOfYear + DaysInYear(c) * SECONDS_IN_DAY;
  endfor
  var nSecsOfMonth := GetDaysFromStartYearToMonth(CInt(arDate[2])) * SECONDS_IN_DAY;
  // Subtracting 1 because today time is given by the other part of the string.
  var nSecsOfDay   := (CInt(arDate[3])-1) * SECONDS_IN_DAY;
  time := time + nSecsOfYear + nSecsOfMonth + nSecsOfDay;
  
  return time;
  
endfunction
Post Reply