Mozilla BuildBot Trending/Snippets

From CDOT Wiki
Revision as of 02:05, 13 February 2009 by John64 (talk | contribs) (New page: ==Date Format Parser== This js code parses a terminal date string into a real date and time. <pre> // This file is a parser for terminal "date" command // format strings. Likely, you wil...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Date Format Parser

This js code parses a terminal date string into a real date and time.

// This file is a parser for terminal "date" command
// format strings.  Likely, you will want to use only
// timeParse(date, fmtstring)
// Copyright 2009 John Ford
// MPL-GPL-LGPL

/* This function pads a signle digit to a leading 0
   as a string */
function pad(number){
  if(number <= 9){
    return "0" + "" + number;
  } else {
    return number;
  }
}

/* Convert a specifer letter into a value based on date */
function specifier(date, letter){
  switch(letter){
    case 'Y':
      return "" + (1900 + date.getYear());
    case 'm':
      //Whole numbering used for month
      return pad(date.getMonth() + 1);
    case 'd':
      return pad(date.getDate());
    case 'H':
      return pad(date.getHours());
    case 'M':
      return pad(date.getMinutes());
    case 'S':
      return pad(date.getSeconds());
   case 's':
      return date.valueOf();
    default:
      return letter;
  }
}

/* This will parse date format strings to give valid
   output pased on POSIX date command when given a format
   and a date object. Returns string */
function timeParse(date, fmt){
  var output ="";
  var input = "" + fmt;
  input = input.replace(/\+/, "");
  input = input.replace(/%*/g, "");
  for each (letter in input){
    if (letter == '%'){
      continue;
    } else {
      output += specifier(date, "" + letter);
    }
  }
  return output;
}