Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10077: common_date_methods.diff

File common_date_methods.diff, 14.5 kB (added by joshua.clayton, 1 year ago)

Diff of changes including tests

  • test/unit/date.html

    old new  
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     2        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
     4<head> 
     5  <title>Prototype Unit test file</title> 
     6  <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
     7  <script src="../../dist/prototype.js" type="text/javascript"></script> 
     8  <script src="../lib/unittest.js" type="text/javascript"></script> 
     9  <link rel="stylesheet" href="../test.css" type="text/css" /> 
     10  <style type="text/css" media="screen"> 
     11  /* <![CDATA[ */ 
     12    #testcss1 { font-size:11px; color: #f00; } 
     13    #testcss2 { font-size:12px; color: #0f0; display: none; } 
     14  /* ]]> */ 
     15  </style> 
     16</head> 
     17<body> 
     18<h1>Prototype Unit test file</h1> 
     19<p> 
     20  Test of utility functions in date.js 
     21</p> 
     22 
     23<!-- Log output --> 
     24<div id="testlog"> </div> 
     25 
     26<!-- Tests follow --> 
     27<script type="text/javascript" language="javascript" charset="utf-8"> 
     28// <![CDATA[ 
     29        var Fixtures = { 
     30                leapYear:                                       new Date(2004, 1, 1), 
     31                nonLeapYear:                            new Date(2005, 1, 1), 
     32                looksLikeLeapYear:      new Date(1900, 1, 1), 
     33                dateAt31Days:                   new Date('10/31/2007'), 
     34                dateAt30Days:                   new Date('9/30/2007'), 
     35                dateAt5Days:                            new Date('1/5/2007'), 
     36                today:                                                  new Date() 
     37        } 
     38         
     39  new Test.Unit.Runner({ 
     40    testMonthArray: function() { with(this) { 
     41                        assertEqual('January', Date.months[0]); 
     42                        assertEqual('December', Date.months[11]); 
     43                        assertUndefined(Date.months[12]); 
     44                }}, 
     45                testDayOfWeekArray: function() { with(this) { 
     46                        assertEqual('Sunday', Date.weekdays[0]); 
     47                        assertEqual('Saturday', Date.weekdays[6]); 
     48                        assertUndefined(Date.weekdays[7]);                       
     49                }}, 
     50                testDaysInMonth: function() { with(this) { 
     51                        assertEqual(31, (new Date(2005, 0, 1).daysInMonth())); 
     52                        assertEqual(29, Fixtures.leapYear.daysInMonth()); 
     53                        assertEqual(28, Fixtures.nonLeapYear.daysInMonth()); 
     54                        assertEqual(28, Fixtures.looksLikeLeapYear.daysInMonth()); 
     55                        assertEqual(31, (new Date(2005, 2, 1).daysInMonth())); 
     56                        assertEqual(30, (new Date(2005, 3, 1).daysInMonth())); 
     57                        assertEqual(31, (new Date(2005, 4, 1).daysInMonth())); 
     58                        assertEqual(30, (new Date(2005, 5, 1).daysInMonth())); 
     59                        assertEqual(31, (new Date(2005, 6, 1).daysInMonth())); 
     60                        assertEqual(31, (new Date(2005, 7, 1).daysInMonth())); 
     61                        assertEqual(30, (new Date(2005, 8, 1).daysInMonth())); 
     62                        assertEqual(31, (new Date(2005, 9, 1).daysInMonth())); 
     63                        assertEqual(30, (new Date(2005, 10, 1).daysInMonth())); 
     64                        assertEqual(31, (new Date(2005, 11, 1).daysInMonth())); 
     65                        assertEqual(31, Date.daysInMonth({year: 2007, month: 0})); 
     66                        assertEqual(31, Date.daysInMonth(new Date(2007, 0, 1))); 
     67                }}, 
     68                testLeapYears: function() { with(this) { 
     69                        assert(Fixtures.leapYear.isLeapYear()); 
     70                        assert(Fixtures.nonLeapYear.isLeapYear() == false); 
     71                        assert(Fixtures.looksLikeLeapYear.isLeapYear() == false); 
     72                }}, 
     73                testChangingMonthsCorrectsDay: function() { with(this) { 
     74                        assertEqual(31, Fixtures.dateAt31Days.dayOfChangedMonth({month: 9, year: 2008})); 
     75                        assertEqual(29, Fixtures.dateAt31Days.dayOfChangedMonth({month: 1, year: 2008})); 
     76                        assertEqual(30, Fixtures.dateAt31Days.dayOfChangedMonth({month: 8, year: 2008})); 
     77                        assertEqual(30, Fixtures.dateAt30Days.dayOfChangedMonth({month: 9, year: 2008})); 
     78                        assertEqual(29, Fixtures.dateAt30Days.dayOfChangedMonth({month: 1, year: 2008})); 
     79                        assertEqual(30, Fixtures.dateAt30Days.dayOfChangedMonth({month: 8, year: 2008})); 
     80                        assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 9, year: 2008})); 
     81                        assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 1, year: 2008})); 
     82                        assertEqual(5, Fixtures.dateAt5Days.dayOfChangedMonth({month: 8, year: 2008})); 
     83                }}, 
     84                testAdvanceMonth: function() { with(this) { 
     85                        assertEqual(new Date('12/1/2008').beginningOfDay().getTime(), new Date('11/1/2007').beginningOfDay().advanceMonth(13).getTime()); 
     86                        assertEqual(new Date('10/1/2007').beginningOfDay().getTime(), new Date('11/1/2007').beginningOfDay().advanceMonth(-1).getTime()); 
     87                        assertEqual(new Date('9/30/2007').beginningOfDay().getTime(), new Date('10/31/2007').beginningOfDay().advanceMonth(-1).getTime()); 
     88                        assertEqual(new Date('2/29/2004').beginningOfDay().getTime(), new Date('3/31/2004').beginningOfDay().advanceMonth(-1).getTime()); 
     89                        assertEqual(new Date('2/28/2007').beginningOfDay().getTime(), new Date('1/29/2007').beginningOfDay().advanceMonth(1).getTime()); 
     90                }}, 
     91                testClone: function() { with(this) { 
     92                        assertEqual(Fixtures.dateAt5Days.getTime(), Fixtures.dateAt5Days.clone().getTime()); 
     93                        var newDate = Fixtures.dateAt5Days.clone(); 
     94                        newDate.setDate(newDate.getDate() + 5); 
     95                        assert(Fixtures.dateAt5Days.getTime() != newDate.getTime()); 
     96                }}, 
     97                testAdvance: function() { with(this) { 
     98                        assertEqual(new Date('12/1/2007').getTime(),    new Date('11/1/2007').advance({months: 1}).getTime()); 
     99                        assertEqual(new Date('11/1/2008').getTime(),    new Date('11/1/2007').advance({years: 1}).getTime()); 
     100                        assertEqual(new Date('11/10/2007').getTime(),   new Date('11/1/2007').advance({days: 9}).getTime()); 
     101                        assertEqual(new Date('12/1/2007').getTime(),    new Date('11/1/2007').advance({days: 30}).getTime()); 
     102                        assertEqual(new Date('10/31/2007').getTime(), new Date('11/1/2007').advance({days: -1}).getTime()); 
     103                        assertEqual(new Date('12/31/2007').getTime(), new Date('11/1/2007').advance({months: 1, days: 30}).getTime()); 
     104                        assertEqual(new Date('1/1/2008').getTime(),     new Date('11/1/2007').advance({months: 1, days: 31}).getTime()); 
     105                        assertEqual(new Date('12/6/2009').getTime(),    new Date('11/1/2007').advance({years: 2, months: 1, days: 5}).getTime()); 
     106                }}, 
     107                testStripTime: function() { with(this) { 
     108                        assertEqual(0, Fixtures.dateAt5Days.stripTime().getHours()); 
     109                        assertEqual(0, Fixtures.dateAt5Days.stripTime().getMinutes()); 
     110                        assertEqual(0, Fixtures.dateAt5Days.stripTime().getSeconds()); 
     111                        assertEqual(0, Fixtures.dateAt5Days.stripTime().getMilliseconds()); 
     112                }}, 
     113                testYearsAgo: function() { with(this) { 
     114                        assertEqual(new Date('1/1/2006').getTime(), new Date('1/1/2007').yearsAgo(1).getTime()); 
     115                        assertEqual(new Date('2/28/2001').getTime(), new Date('2/29/2004').yearsAgo(3).getTime()); 
     116                }}, 
     117                testMonthsAgo: function() { with(this) { 
     118                        assertEqual(new Date('1/1/2008').getTime(), new Date('3/1/2008').monthsAgo(2).getTime()); 
     119                        assertEqual(new Date('2/28/2007').getTime(), new Date('3/31/2008').monthsAgo(13).getTime()); 
     120                }}, 
     121                testDaysAgo: function() { with(this) { 
     122                        assertEqual(new Date('1/1/2008').getTime(),             new Date('1/15/2008').daysAgo(14).getTime()); 
     123                        assertEqual(new Date('2/29/2004').getTime(),    new Date('3/1/2004').daysAgo(1).getTime()); 
     124                        assertEqual(new Date('9/30/2007').getTime(),    new Date('10/31/2007').daysAgo(31).getTime()); 
     125                        assertEqual(new Date('12/31/2006').getTime(),   new Date('1/31/2007').daysAgo(31).getTime()); 
     126                }}, 
     127                testBeginningOfWeek: function() { with(this) { 
     128                        assertEqual(new Date('11/12/2007').getTime(), new Date('11/18/2007').beginningOfWeek().getTime()); 
     129                        assertEqual(new Date('11/12/2007').getTime(), new Date('11/12/2007').beginningOfWeek().getTime());                       
     130                }}, 
     131                testIsToday: function() { with(this) { 
     132                        assert(Fixtures.today.isToday()); 
     133                        assert(!Fixtures.today.daysAgo(1).isToday()); 
     134                }}, 
     135                testIsYesterday: function() { with(this) { 
     136                        assert(Fixtures.today.daysAgo(1).isYesterday()); 
     137                        assert(!Fixtures.today.isYesterday()); 
     138                }} 
     139  }, 'testlog'); 
     140// ]]> 
     141</script> 
     142</body> 
     143</html> 
  • src/prototype.js

    old new  
    3737 
    3838<%= include 'enumerable.js', 'array.js', 'number.js', 'hash.js', 'range.js' %> 
    3939 
    40 <%= include 'ajax.js', 'dom.js', 'selector.js', 'form.js', 'event.js', 'deprecated.js' %> 
     40<%= include 'ajax.js', 'dom.js', 'selector.js', 'form.js', 'event.js', 'deprecated.js', 'date.js' %> 
    4141 
    4242Element.addMethods(); 
  • src/base.js

    old new  
    206206 
    207207Function.prototype.defer = Function.prototype.delay.curry(0.01); 
    208208 
    209 Date.prototype.toJSON = function() { 
    210   return '"' + this.getUTCFullYear() + '-' + 
    211     (this.getUTCMonth() + 1).toPaddedString(2) + '-' + 
    212     this.getUTCDate().toPaddedString(2) + 'T' + 
    213     this.getUTCHours().toPaddedString(2) + ':' + 
    214     this.getUTCMinutes().toPaddedString(2) + ':' + 
    215     this.getUTCSeconds().toPaddedString(2) + 'Z"'; 
    216 }; 
    217  
    218209var Try = { 
    219210  these: function() { 
    220211    var returnValue; 
  • src/date.js

    old new  
     1Object.extend(Date, { 
     2  months: $w("January February March April May June July August September October November December"), 
     3  weekdays: $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday"), 
     4  daysInMonth: function(date) { 
     5    try { 
     6      date.getFullYear(); 
     7    } catch(e) { 
     8      var today = new Date(); 
     9      date = new Date(date.year || today.getFullYear(), date.month || date.month === 0 ? date.month : today.getMonth(), 1); 
     10    } 
     11     
     12    var month = date.getMonth(); 
     13    if (month === 1) { 
     14      return date.isLeapYear() ? 29 : 28; 
     15    } else if (month < 7) { 
     16      return (month + 1) % 2 === 0 ? 30 : 31; 
     17    } else { 
     18      return (month + 1) % 2 === 0 ? 31 : 30; 
     19    } 
     20  }, 
     21  isLeapYear: function(date) { 
     22    if ((typeof date == 'string') || (typeof date == 'number')) { date = new Date(date, 1, 1); } 
     23    return date.getFullYear() % 4 === 0 && (date.getFullYear() % 100 !== 0 || date.getFullYear() % 400 === 0); 
     24  } 
     25}); 
     26 
     27Object.extend(Date.prototype, { 
     28  toJSON: function() { 
     29    return '"' + this.getUTCFullYear() + '-' + 
     30      (this.getUTCMonth() + 1).toPaddedString(2) + '-' + 
     31      this.getUTCDate().toPaddedString(2) + 'T' + 
     32      this.getUTCHours().toPaddedString(2) + ':' + 
     33      this.getUTCMinutes().toPaddedString(2) + ':' + 
     34      this.getUTCSeconds().toPaddedString(2) + 'Z"'; 
     35  }, 
     36  dayOfChangedMonth: function(options) { 
     37    return this.getDate() > Date.daysInMonth({month: options.month, year: options.year || this.getFullYear()}) ? Date.daysInMonth({month: options.month, year: options.year || this.getFullYear()}) : this.getDate(); 
     38  }, 
     39  advanceMonth: function(months) { 
     40    var agnosticDay = new Date(this.getFullYear(), this.getMonth() + months, 1); 
     41    var currentDayInNewMonth = this.dayOfChangedMonth({year: agnosticDay.getFullYear(), month: agnosticDay.getMonth()}); 
     42    var date = this.clone(); 
     43    date.setDate(currentDayInNewMonth); 
     44    date.setMonth(this.getMonth() + months); 
     45    return date; 
     46  }, 
     47  change: function(options) { 
     48    var year          = options && options.year ? options.year : this.getFullYear(); 
     49    var month         = options && (options.month || options.month === 0) ? options.month : this.getMonth(); 
     50    var date          = options && options.date ? options.date : this.dayOfChangedMonth({year: year, month: month}); 
     51    var hour          = options && (options.hours || options.hours === 0) ? options.hours : this.getHours(); 
     52    var minutes       = options && (options.minutes || options.minutes === 0) ? options.minutes : (options.hours ? 0 : this.getMinutes()); 
     53    var seconds       = options && (options.seconds || options.seconds === 0) ? options.seconds : (options.hours || options.minutes ? 0 : this.getSeconds()); 
     54    var milliseconds  = options && (options.milliseconds || options.milliseconds === 0) ? options.milliseconds : (options.hours || options.minutes || options.seconds ? 0 : this.getMilliseconds()); 
     55    return new Date(year, month, date, hour, minutes, seconds, milliseconds); 
     56  }, 
     57  clone: function() { 
     58    return this.change({date: this.getDate()}); 
     59  }, 
     60  advance: function(options) { 
     61    var date = this.clone(); 
     62    date.setFullYear(this.getFullYear() + (options.years || 0)); 
     63    var lambdaMonth = options.months || 0; 
     64    date.setDate(1); 
     65    date = date.advanceMonth(lambdaMonth); 
     66    date.setDate(this.dayOfChangedMonth({year: date.getFullYear(), month: date.getMonth()}) + (options.days || 0)); 
     67    return this.change({year: date.getFullYear(), month: date.getMonth(), date: date.getDate()});     
     68  }, 
     69  daysInMonth: function(options){ return (options ? Date.daysInMonth(options) : Date.daysInMonth(this)); }, 
     70  stripTime:        function()      { return new Date(this.getFullYear(), this.getMonth(), this.getDate()); }, 
     71  yearsAgo:         function(count) { return this.change({year: this.getFullYear() - count}); }, 
     72  monthsAgo:        function(count) { return this.advanceMonth(-count); }, 
     73  daysAgo:          function(count) { var newDate = this.clone(); newDate.setDate(this.getDate() - count); return newDate; }, 
     74  yearsSince:       function(count) { return this.yearsAgo(-count); }, 
     75  monthsSince:      function(count) { return this.monthsAgo(-count); }, 
     76  daysSince:        function(count) { return this.daysAgo(-count); }, 
     77  lastYear:         function()      { return this.yearsAgo(1); }, 
     78  nextYear:         function()      { return this.yearsAgo(-1); }, 
     79  lastMonth:        function()      { return this.monthsAgo(1); }, 
     80  nextMonth:        function()      { return this.monthsAgo(-1); }, 
     81  midnight:         function()      { return this.stripTime(); }, 
     82  beginningOfDay:   function()      { return this.stripTime(); }, 
     83  beginningOfMonth: function()      { return this.change({date: 1}).midnight(); }, 
     84  endOfMonth:       function()      { return this.change({date: this.daysInMonth()}).midnight(); }, 
     85  beginningOfYear:  function()      { return this.change({date: 1, month: 0}).midnight(); }, 
     86  beginningOfWeek:  function() { 
     87    var daysToMonday = this.getDay() !== 0 ? this.getDay() - 1 : 6; 
     88    return this.advance({days: -daysToMonday}).midnight(); 
     89  }, 
     90  isToday: function() { 
     91    var today = new Date(); 
     92    return today.getFullYear() == this.getFullYear() && today.getMonth() == this.getMonth() && today.getDate() == this.getDate(); 
     93  }, 
     94  isYesterday: function() { 
     95    var yesterday = new Date().daysAgo(1); 
     96    return yesterday.getFullYear() == this.getFullYear() && yesterday.getMonth() == this.getMonth() && yesterday.getDate() == this.getDate();     
     97  }, 
     98  //used right now in order to be used in sites not running newest version of prototype 
     99  isLeapYear: function() { 
     100    return Date.isLeapYear(this); 
     101  } 
     102}); 
     103 
     104$w("isLeapYear").each(function(method) { 
     105  Date.prototype[method] = Date[method].methodize(); 
     106});