MySQLTime.prototype.initializeFromTimeString = function()

in database-jones/Adapter/common/MySQLTime.js [50:119]


MySQLTime.prototype.initializeFromTimeString = function(jsValue) {
  var numValue = "";
  var decimalPart = "";
  var decimalLength = 0;
  var pos = 0;
  var colons = 0;
  var c, cc;
  
  /* Initial + or - */
  c = jsValue.charAt(pos);
  if(c == '+' || c == '-') {
    numValue += c;
    pos++;
  }

  /* Copy numbers but skip separators */
  while(pos < jsValue.length) {
    c = jsValue.charAt(pos);
    cc = jsValue.charCodeAt(pos);
    if(cc > 47 && cc < 58) {  // i.e. isdigit()
      numValue += jsValue.charAt(pos);
    }
    else if(c === '.' && ! decimalPart) { 
      decimalPart = jsValue.slice(pos+1);
      pos = jsValue.length;
    }
    else if(c === ':') {
      colons++;
    } else if((cc > 64 && cc < 91) || (cc > 96 && cc < 123)) {
      /* MySQL truncates a time string where it sees a letter */
      pos = jsValue.length;
    }
    pos++;
  }

  /* Convert fixed and decimal parts to numbers */
  numValue = parseInt(numValue, 10);

  if(numValue < 0) {
    this.sign = -1;
    numValue = -numValue;  
  }

  this.hour = Math.floor(numValue / 10000); 
  this.minute = (Math.floor(numValue / 100)) % 100;
  this.second = numValue % 100;

  /* Expand decimal part out to microseconds */
  if(decimalPart > 0) {
    decimalLength = decimalPart.length;
    this.microsec = parseInt(decimalPart, 10);
    while(decimalLength > 6) {  
      this.microsec = Math.floor(this.microsec / 10);
      decimalLength--;
    }
    while(decimalLength < 6) {
      this.microsec *= 10;
      decimalLength++;
    }
  }

  /* Special case HH:MM */
  if(this.hour == 0 && colons == 1) {
    this.hour = this.minute;
    this.minute = this.second;
    this.second = 0; 
  }

  return this;
};