]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Switched to yyyy-MM-DD format for original proprietary format
authorTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 02:36:24 +0000 (18:36 -0800)
committerTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 02:36:24 +0000 (18:36 -0800)
lib/underscore.date.js
test/date.js

index 9143d4f7830da51cb6515bfad0b474d63a193ca6..15f8d54c72e63290622c2fe74a877fd3be3a1bfd 100644 (file)
                wordsTimeAgo = {
                        future: "in %s",
                        past: "%s ago",
-                       S: "less than a minute",
+                       s: "less than a minute",
                        m: "about a minute",
-                       M: "%d minutes",
+                       mm: "%d minutes",
                        h: "about an hour",
-                       H: "about %d hours",
+                       hh: "about %d hours",
                        d: "a day",
-                       D: "%d days",
-                       l: "about a month",
-                       L: "%d months",
+                       dd: "%d days",
+                       M: "about a month",
+                       MM: "%d months",
                        y: "about a year",
-                       Y: "%d years"
+                       yy: "%d years"
                };
        
        // add ordinal to number
                return output;
        }
        
-       // add zero fill and ordinal to number
-       function zeroFillAndOrdinal(number, zerofill, ordinal) {
-               var output = zerofill ? leftZeroFill(number, zerofill) : number;
-               return ordinal ? output + createOrdinal(number) : output;
-       }
-       
        // Date.prototype.humanize
        function humanize(inputString) {
                // shortcuts to this and getting time functions
                        currentHours = self.getHours(),
                        currentMinutes = self.getMinutes(),
                        currentSeconds = self.getSeconds(),
-                       charactersToReplace = /[a-z][0-9]?/gi,
+                       charactersToReplace = /Mo|MM?M?M?|Do|DDDo|DD?D?D?|do|dd?d?d?|w[o|w]?|YYYY|YY|[aA]|hh?|HH?|mm?|ss?/g,
                        formatFunctions = {
-                               // MONTH 
-                               // number 
-                               L : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentMonth + 1, zerofill ? 2 : 0, ordinal);
-                               },
-                               // string 
-                               l : function(shorthand) {
-                                       var output = wordsMonths[currentMonth];
-                                       return shorthand ? output.slice(0, 3) : output;
+                               // MONTH
+                               M : function() {
+                                       return currentMonth + 1;
+                               },
+                               Mo : function() {
+                                       return (currentMonth + 1) + createOrdinal(currentMonth + 1);
+                               },
+                               MM : function() {
+                                       return leftZeroFill(currentMonth + 1, 2);
+                               },
+                               MMM : function() {
+                                       return wordsMonths[currentMonth].slice(0, 3);
+                                       
+                               },
+                               MMMM : function() {
+                                       return wordsMonths[currentMonth];
                                        
                                },
                                
                                // DAY OF MONTH
-                               // number
-                               D : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentDate, zerofill ? 2 : 0, ordinal);
+                               D : function() {
+                                       return currentDate;
+                               },
+                               Do : function() {
+                                       return currentDate + createOrdinal(currentDate);
+                               },
+                               DD : function() {
+                                       return leftZeroFill(currentDate, 2);
                                },
                                
                                // DAY OF YEAR
-                               // number
-                               d : function(ordinal, zerofill) {
+                               DDD : function() {
                                        var a = new Date(currentYear, currentMonth, currentDate),
                                                b = new Date(currentYear, 0, 1);
-                                       return zeroFillAndOrdinal(((a - b) / 864e5) + 1.5 | 0, zerofill ? 3 : 0, ordinal);
+                                       return ((a - b) / 864e5) + 1.5 | 0;
+                               },
+                               DDDo : function() {
+                                       var DDD = formatFunctions.DDD();
+                                       return DDD + createOrdinal(DDD);
+                               },
+                               DDDD : function() {
+                                       return leftZeroFill(formatFunctions.DDD(), 3);
                                },
                                
                                // WEEKDAY
-                               // number
-                               W : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentDay, zerofill ? 2 : 0, ordinal);
+                               d : function() {
+                                       return currentDay;
                                },
-                               // string
-                               w : function(shorthand) {
-                                       var output = wordsWeekdays[currentDay];
-                                       return shorthand ? output.slice(0, 3) : output;
+                               do : function() {
+                                       return currentDay + createOrdinal(currentDay);
+                               },
+                               dd : function() {
+                                       return leftZeroFill(currentDay, 2);
+                               },
+                               ddd : function() {
+                                       return wordsWeekdays[currentDay].slice(0, 3);
+                               },
+                               dddd : function() {
+                                       return wordsWeekdays[currentDay];
                                },
                                
                                // WEEK OF YEAR
-                               K : function(ordinal, zerofill) {
+                               w : function() {
                                        var a = new Date(currentYear, currentMonth, currentDate - currentDay + 5),
                                                b = new Date(a.getFullYear(), 0, 4);
-                                       return zeroFillAndOrdinal((a - b) / 864e5 / 7 + 1.5 | 0, zerofill ? 2 : 0, ordinal);
+                                       return (a - b) / 864e5 / 7 + 1.5 | 0;
+                               },
+                               wo : function() {
+                                       var w = formatFunctions.w();
+                                       return w + createOrdinal(w);
+                               },
+                               ww : function() {
+                                       return leftZeroFill(formatFunctions.w(), 2);
                                },
                                
                                // YEAR
-                               Y : function(shorthand) {
-                                       return shorthand ? (currentYear + '').slice(-2) : currentYear;
+                               YY : function() {
+                                       return (currentYear + '').slice(-2);
+                               },
+                               YYYY : function(shorthand) {
+                                       return currentYear;
                                },
                                
                                // AM / PM
                                        return currentHours > 11 ? 'PM' : 'AM';
                                },
                                
-                               // HOUR 
-                               // 24
-                               H : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentHours, zerofill ? 2 : 0, ordinal);
+                               // 24 HOUR 
+                               H : function() {
+                                       return currentHours;
                                },
-                               // 12
-                               h : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentHours % 12 || 12, zerofill ? 2 : 0, ordinal);
+                               HH : function() {
+                                       return leftZeroFill(currentHours, 2);
+                               },
+                               
+                               // 12 HOUR 
+                               h : function() {
+                                       return currentHours % 12 || 12;
+                               },
+                               hh : function() {
+                                       return leftZeroFill(currentHours % 12 || 12, 2);
                                },
                                
                                // MINUTE
-                               m : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentMinutes, zerofill ? 2 : 0, ordinal);
+                               m : function() {
+                                       return currentMinutes;
+                               },
+                               mm : function() {
+                                       return leftZeroFill(currentMinutes, 2);
                                },
                                
                                // SECOND
-                               s : function(ordinal, zerofill) {
-                                       return zeroFillAndOrdinal(currentSeconds, zerofill ? 2 : 0, ordinal);
+                               s : function() {
+                                       return currentSeconds;
+                               },
+                               ss : function() {
+                                       return leftZeroFill(currentSeconds, 2);
                                }
                        };
                
                // check if the character is a format
                // return formatted string or non string.
                function replaceFunction(input) {
-                       var character = input.charAt(0),
-                               parameter = input.charAt(1) || 0;
-                       return formatFunctions[character] ? formatFunctions[character](parameter & 1, parameter >> 1) : input;
+                       return formatFunctions[input] ? formatFunctions[input]() : input;
                }
                
                return inputString.replace(charactersToReplace, replaceFunction);
                                hours = minutes / 60,
                                days = hours / 24,
                                years = days / 365;
-                       return seconds < 45 && substituteTimeAgo('S', seconds | 0) ||
+                       return seconds < 45 && substituteTimeAgo('s', seconds | 0) ||
                                seconds < 90 && substituteTimeAgo('m') ||
-                               minutes < 45 && substituteTimeAgo('M', minutes | 0) ||
+                               minutes < 45 && substituteTimeAgo('mm', minutes | 0) ||
                                minutes < 90 && substituteTimeAgo('h') ||
-                               hours < 24 && substituteTimeAgo('H', hours | 0) ||
+                               hours < 24 && substituteTimeAgo('hh', hours | 0) ||
                                hours < 48 && substituteTimeAgo('d') ||
-                               days < 30 && substituteTimeAgo('D', days | 0) ||
-                               days < 60 && substituteTimeAgo('l') ||
-                               days < 350 && substituteTimeAgo('L', days / 30 | 0) ||
+                               days < 30 && substituteTimeAgo('dd', days | 0) ||
+                               days < 60 && substituteTimeAgo('M') ||
+                               days < 350 && substituteTimeAgo('MM', days / 30 | 0) ||
                                years < 2 && substituteTimeAgo('y') ||
-                               substituteTimeAgo('Y', years | 0);
+                               substituteTimeAgo('yy', years | 0);
                },
                msapart : function(time, now) {
                        return makeInputMilliseconds(time) - makeInputMilliseconds(now);
index a52543b61d70806092e9db56c25c1ed8c09f0c68..beb714cf37f8d0b87a1d5fbfceb4011805bd88db 100644 (file)
@@ -5,18 +5,18 @@ $(document).ready(function() {
   test("dateFormat", function() {
        var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125);
     expect(13);
-       equal(dateTest.humanize("w, l D1 Y, h:m2:s2 a"), "Sunday, February 14th 2010, 3:25:50 pm");
-       equal(dateTest.humanize("w1, hA"), "Sun, 3PM");
-       equal(dateTest.humanize("L L1 L2 L3 l l1"), "2 2nd 02 02nd February Feb");
-       equal(dateTest.humanize("Y Y1"), "2010 10");
-       equal(dateTest.humanize("D D1 D2 D3"), "14 14th 14 14th");
-       equal(dateTest.humanize("W W1 W2 W3 w w1"), "0 0th 00 00th Sunday Sun");
-       equal(dateTest.humanize("d d1 d2 d3"), "45 45th 045 045th");
-       equal(dateTest.humanize("K K1 K2 K3"), "8 8th 08 08th");
-       equal(dateTest.humanize("h h1 h2 h3"), "3 3rd 03 03rd");
-       equal(dateTest.humanize("H H1 H2 H3"), "15 15th 15 15th");
-       equal(dateTest.humanize("m m1 m2 m3"), "25 25th 25 25th");
-       equal(dateTest.humanize("s s1 s2 s3"), "50 50th 50 50th");
+       equal(dateTest.humanize("dddd, MMMM Do YYYY, h:mm:ss a"), "Sunday, February 14th 2010, 3:25:50 pm");
+       equal(dateTest.humanize("ddd, hA"), "Sun, 3PM");
+       equal(dateTest.humanize("M Mo MM MMMM MMM"), "2 2nd 02 February Feb");
+       equal(dateTest.humanize("YYYY YY"), "2010 10");
+       equal(dateTest.humanize("D Do DD"), "14 14th 14");
+       equal(dateTest.humanize("d do dd dddd ddd"), "0 0th 00 Sunday Sun");
+       equal(dateTest.humanize("DDD DDDo DDDD"), "45 45th 045");
+       equal(dateTest.humanize("w wo ww"), "8 8th 08");
+       equal(dateTest.humanize("h hh"), "3 03");
+       equal(dateTest.humanize("H HH"), "15 15");
+       equal(dateTest.humanize("m mm"), "25 25");
+       equal(dateTest.humanize("s ss"), "50 50");
        equal(dateTest.humanize("a A"), "pm PM");
   });