Advertisement
Alhadis

Time.js

May 18th, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Converts a well-formed string representing a time of day into a JavaScript date object. */
  2. function Time(input){
  3.     var match, date, hours, minutes, seconds, milliseconds;
  4.  
  5.  
  6.     /**
  7.      * Override the returned Date object's toString method to return a numeric value, unless an argument is explicitly passed
  8.      * requesting the date's representation as a human-readable string. To invoke the standard behaviour of Date.toString,
  9.      * consider using the instance's toLocaleString or toUTCString methods instead.
  10.      *
  11.      * @param {bool} Returns a string formatted to 12 or 24-hour time if set to FALSE or TRUE, respectively.
  12.      * @return {int|string} The number of milliseconds of the generated date if in24hr is undefined; a string otherwise.
  13.     */
  14.     var toString    =   function(in24hr){
  15.         if(undefined === in24hr) return this.getTime();
  16.  
  17.         var h   =   this.getUTCHours(),
  18.             m   =   this.getUTCMinutes(),
  19.             s   =   this.getUTCSeconds() || "",
  20.             ms  =   this.getUTCMilliseconds() || "";
  21.  
  22.         if(ms){
  23.             if(ms < 10)         ms  =   "00" + ms;
  24.             else if(ms < 100)   ms  =    "0" + ms;
  25.         }
  26.  
  27.         return  (in24hr ? ((h < 10 ? "0" : "")+h) : (!h ? 12 : (h > 12 ? h-12 : h))) + ":"
  28.                 +   (m < 10 ? "0" : "") + m
  29.                 +   (s? ":"+(s+(ms? "."+ms:"")) : "")
  30.                 +   (in24hr ? "" : (h < 12 ? " am" : " pm"));
  31.     };
  32.  
  33.  
  34.     /** Allow function to be called without an argument; will default to current time. */
  35.     if(undefined === input) input   =   "now";
  36.  
  37.  
  38.  
  39.     if((match = input.match(/^\s*(\d+)(?::(\d{2})\s*(?::(\d{2})(\.\d+)?)?)?\s*((?:a|p)\.?m\.?)?\s*$/i))){
  40.         hours           =   parseInt(match[1]);
  41.         minutes         =   parseInt(match[2]);
  42.         seconds         =   parseInt(match[3]);
  43.         milliseconds    =   match[4] ? Math.round((parseFloat(match[4]) * 1000)) : 0;
  44.  
  45.         var meridiem    =   !match[5] ? undefined : (match[5][0].toLowerCase() === "p" ? "PM" : "AM");
  46.         if(meridiem === "PM" && hours < 12)
  47.             hours   +=  12;
  48.  
  49.  
  50.         /** Don't be fooled: 12pm is technically kinda 0pm. */
  51.         else if(meridiem === "AM" && hours == 12)
  52.             hours   =   0;
  53.  
  54.  
  55.         /** Generate our returned value. */
  56.         date                =   new Date(0);
  57.         date.toString       =   toString;
  58.         date.inputString    =   input;
  59.         date.setUTCHours(hours);
  60.         date.setUTCMinutes(minutes || 0);
  61.         date.setUTCSeconds(seconds || 0);
  62.         date.setUTCMilliseconds(milliseconds || 0);
  63.         return date;
  64.     }
  65.    
  66.     /** Allow some common shorthand values to be passed in instead. */
  67.     switch(input.toLowerCase()){
  68.         case "now":{
  69.             date                =   new Date();
  70.             date.toString       =   toString;
  71.             date.inputString    =   input;
  72.             date.setUTCFullYear(1970);
  73.             date.setUTCMonth(0);
  74.             date.setUTCDate(1);
  75.             return date;
  76.             break;
  77.         }
  78.  
  79.         case "midnight":{
  80.             date                =   new Date(0);
  81.             date.toString       =   toString;
  82.             date.inputString    =   input;
  83.             return date;
  84.             break;
  85.         }
  86.  
  87.         case "midday":
  88.         case "noon":{
  89.             date                =   new Date(43200000);
  90.             date.toString       =   toString;
  91.             date.inputString    =   input;
  92.             return date;
  93.             break;
  94.         }
  95.     }
  96.  
  97.     /** Otherwise, we weren't supplied a value that we could read. Return NULL. */
  98.     return null;
  99. };
  100.  
  101.  
  102. /**
  103.  * Measures the number of milliseconds between a set of times.
  104.  *
  105.  * @param {string} input A comma-separated sequence of hyphen-delimited time ranges ("7:30am - 9:30am, 11:30am - 12:15pm")
  106.  * @return {int} The total number of milliseconds between each of the supplied times.
  107.  */
  108. Time.between    =   function(input){
  109.     var rSplit  =   /\s*-\s*/g,
  110.         ranges  =   input.split(/,/g),
  111.         output  =   0;
  112.  
  113.     var rHour       =   /^\s*\d+/,
  114.         rMeridiem   =   /((?:a|p)\.?m\.?)\s*$/i;
  115.  
  116.     var range, i, from, to, fromHour, toHour, match;
  117.     for(i = 0; i < ranges.length; ++i){
  118.         range   =   ranges[i].split(rSplit);
  119.  
  120.         from    =   range[0];
  121.         to      =   range[range.length-1];
  122.  
  123.         /** Only the first time was supplied an AM/PM suffix. */
  124.         if(!rMeridiem.test(from) && (match = to.match(rMeridiem))){
  125.  
  126.             fromHour    =   parseInt(from.match(rHour)[0]);
  127.             toHour      =   parseInt(to.match(rHour)[0]);
  128.  
  129.             /** Beware of that bloody 12:00 AM/PM crossover. */
  130.             if(fromHour !== 12 && toHour === 12 && match[1])
  131.                 from    +=  "a" === match[1][0].toLowerCase() ? " pm" : " am";
  132.  
  133.             else
  134.                 from    +=  match[1];
  135.         }
  136.  
  137.         from    =   Time(from);
  138.         to      =   Time(to);
  139.  
  140.         /** Time ranges spanning midnight need to be handled carefully. */
  141.         if(+from >= 43200000 && to < 43200000)
  142.             output  +=  (86400000 - from) + to;
  143.  
  144.         /** Otherwise, just add the time distance normally. */
  145.         else output +=  Math.abs(to - from);
  146.     }
  147.  
  148.     return output;
  149. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement