2013年12月9日 星期一

Javascript show Server UTC time code

/*
 * A javascript extension to allow formatting of Date object like in Ruby's strftime.
Documentation http://www.ruby-doc.org/core/classes/Time.html#M000392
It's simple:
    new Date().format("%H:%M on %d/%m") returns "08:20 on 26/04"

Format directives:
Date (Year, Month, Day):
  %Y - Year with century (can be negative, 4 digits at least)
          -0001, 0000, 1995, 2009, 14292, etc.
  %C - year / 100 (rounded down such as 20 in 2009)
  %y - year % 100 (00..99)

  %m - Month of the year, zero-padded (01..12)
          %_m  blank-padded ( 1..12)
          %-m  no-padded (1..12)
  %B - The full month name (``January'')
          %^B  uppercased (``JANUARY'')
  %b - The abbreviated month name (``Jan'')
          %^b  uppercased (``JAN'')
  %h - Equivalent to %b

  %d - Day of the month, zero-padded (01..31)
          %-d  no-padded (1..31)
  %e - Day of the month, blank-padded ( 1..31)

  %j - Day of the year (001..366)

Time (Hour, Minute, Second, Subsecond):
  %H - Hour of the day, 24-hour clock, zero-padded (00..23)
  %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
  %I - Hour of the day, 12-hour clock, zero-padded (01..12)
  %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
  %P - Meridian indicator, lowercase (``am'' or ``pm'')
  %p - Meridian indicator, uppercase (``AM'' or ``PM'')

  %M - Minute of the hour (00..59)

  %S - Second of the minute (00..60)

  %L - Millisecond of the second (000..999)
  %N - Fractional seconds digits, default is 9 digits (nanosecond)
          %3N  milli second (3 digits)
          %6N  micro second (6 digits)
          %9N  nano second (9 digits)
          %12N pico second (12 digits)
          %15N femto second (15 digits)
          %18N atto second (18 digits)
          %21N zepto second (21 digits)
          %24N yocto second (24 digits)

Time zone:
  %z - Time zone as hour and minute offset from UTC (e.g. +0900)
          %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
          %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
  %Z - Abbreviated time zone name or similar information.

Weekday:
  %A - The full weekday name (``Sunday'')
          %^A  uppercased (``SUNDAY'')
  %a - The abbreviated name (``Sun'')
          %^a  uppercased (``SUN'')
  %u - Day of the week (Monday is 1, 1..7)
  %w - Day of the week (Sunday is 0, 0..6)

ISO 8601 week-based year and week number:
The first week of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

Week number:
The first week of YYYY that starts with a Sunday or Monday (according to %U
or %W). The days in the year before the first week are in week 0.
  %U - Week number of the year. The week starts with Sunday. (00..53)
  %W - Week number of the year. The week starts with Monday. (00..53)

Seconds since the Epoch:
  %s - Number of seconds since 1970-01-01 00:00:00 UTC.

Literal string:
  %n - Newline character (\n)
  %t - Tab character (\t)
  %% - Literal ``%'' character

Combination:
  %c - date and time (%a %b %e %T %Y)
  %D - Date (%m/%d/%y)
  %F - The ISO 8601 date format (%Y-%m-%d)
  %v - VMS date (%e-%^b-%4Y)
  %x - Same as %D
  %X - Same as %T
  %r - 12-hour time (%I:%M:%S %p)
  %R - 24-hour time (%H:%M)
  %T - 24-hour time (%H:%M:%S)
 */
(function () {
    var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
    
    var re = new RegExp(/%(a|A|b|B|c|C|d|D|e|F|h|H|I|j|k|l|L|m|M|n|p|P|r|R|s|S|t|T|u|U|v|V|W|w|x|X|y|Y|z)/g);
    var abbreviatedWeekdays = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
    var fullWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var abbreviatedMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var fullMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    
    function padNumber(num, count, padCharacter) {
      if (typeof padCharacter == "undefined") {
        padCharacter = "0";
      }
      var lenDiff = count - String(num).length;
      var padding = "";
    
      if (lenDiff > 0)
        while (lenDiff--)
          padding += padCharacter;
          
      return padding + num;
    }
    
    function dayOfYearUTC(d) {
      var oneJan = new Date(d.getUTCFullYear(), 0, 1);
      return Math.ceil((d - oneJan) / 86400000);
    }
    
    function weekOfYearUTC(d) {
      var oneJan = new Date(d.getUTCFullYear(), 0, 1);
      return Math.ceil((((d - oneJan) / 86400000) + oneJan.getUTCDay() + 1) / 7);
    }
    
    function isoWeekOfYearUTC(d) {
      var target  = new Date(d.valueOf());  
      var dayNr   = (d.getUTCDay() + 6) % 7;  
      target.setDate(target.getUTCDate() - dayNr + 3);  
      var jan4    = new Date(target.getUTCFullYear(), 0, 4);  
      var dayDiff = (target - jan4) / 86400000;
      
      return 1 + Math.ceil(dayDiff / 7);
    }
    
    function tweleveHourUTC(d) {
      return d.getUTCHours() > 12 ? d.getUTCHours() - 12 : d.getUTCHours();
    }
    
    function timeZoneOffset(d) {
      var hoursDiff = (-d.getTimezoneOffset() / 60);
      var result = padNumber(Math.abs(hoursDiff), 4);
      return (hoursDiff > 0 ? "+" : "-") + result;
    }
    
    Date.prototype.formatUTC = function (formatString) {
      return formatString.replace(re, __bind(function (m, p) {
        switch (p) {
          case "a": return abbreviatedWeekdays[this.getUTCDay()];
          case "A": return fullWeekdays[this.getUTCDay()];
          case "b": return abbreviatedMonths[this.getUTCMonth()];
          case "B": return fullMonths[this.getUTCMonth()];
          case "c": return this.toLocaleString();
          case "C": return Math.round(this.getUTCFullYear() / 100);
          case "d": return padNumber(this.getUTCDate(), 2);
          case "D": return this.format("%m/%d/%y");
          case "e": return padNumber(this.getUTCDate(), 2, " ");
          case "F": return this.format("%Y-%m-%d");
          case "h": return this.format("%b");
          case "H": return padNumber(this.getUTCHours(), 2);
          case "I": return padNumber(tweleveHourUTC(this), 2);
          case "j": return padNumber(dayOfYearUTC(this), 3);
          case "k": return padNumber(this.getUTCHours(), 2, " ");
          case "l": return padNumber(tweleveHourUTC(this), 2, " ");
          case "L": return padNumber(this.getUTCMilliseconds(), 3);
          case "m": return padNumber(this.getUTCMonth() + 1, 2);
          case "M": return padNumber(this.getUTCMinutes(), 2);
          case "n": return "\n";
          case "p": return this.getUTCHours() > 11 ? "PM" : "AM";
          case "P": return this.format("%p").toLowerCase();
          case "r": return this.format("%I:%M:%S %p");
          case "R": return this.format("%H:%M");
          case "s": return this.getTime() / 1000;
          case "S": return padNumber(this.getUTCSeconds(), 2);
          case "t": return "\t";
          case "T": return this.format("%H:%M:%S");
          case "u": return this.getUTCDay() == 0 ? 7 : this.getUTCDay();
          case "U": return padNumber(weekOfYearUTC(this), 2); //either this or W is wrong (or both)
          case "v": return this.format("%e-%b-%Y");
          case "V": return padNumber(isoWeekOfYearUTC(this), 2);
          case "W": return padNumber(weekOfYearUTC(this), 2); //either this or U is wrong (or both)
          case "w": return padNumber(this.getUTCDay(), 2);
          case "x": return this.toLocaleDateString();
          case "X": return this.toLocaleTimeString();
          case "y": return String(this.getUTCFullYear()).substring(2);
          case "Y": return this.getUTCFullYear();
          case "z": return timeZoneOffset(this);
          default: return match;
        }
      }, this));
    };

}).call(this);

沒有留言:

wibiya widget