Przeglądaj źródła

fix bug where setting FCMoment time with a Moment

Adam Shaw 11 lat temu
rodzic
commit
29cbeb55e4
2 zmienionych plików z 77 dodań i 1 usunięć
  1. 10 1
      src/moment-ext.js
  2. 67 0
      tests/automated/moment-misc.js

+ 10 - 1
src/moment-ext.js

@@ -151,7 +151,16 @@ FCMoment.prototype.time = function(time) {
 			time = moment.duration(time);
 		}
 
-		return this.hours(time.hours() + time.days() * 24) // day value will cause overflow (so 24 hours becomes 00:00:00 of next day)
+		// The day value should cause overflow (so 24 hours becomes 00:00:00 of next day).
+		// Only for Duration times, not Moment times.
+		var dayHours = 0;
+		if (moment.isDuration(time)) {
+			dayHours = Math.floor(time.asDays()) * 24;
+		}
+
+		// We need to set the individual fields.
+		// Can't use startOf('day') then add duration. In case of DST at start of day.
+		return this.hours(dayHours + time.hours())
 			.minutes(time.minutes())
 			.seconds(time.seconds())
 			.milliseconds(time.milliseconds());

+ 67 - 0
tests/automated/moment-misc.js

@@ -0,0 +1,67 @@
+describe('FCMoment::time', function() {
+
+	describe('getter', function() {
+
+		// the scenario where an ambiguously-timed moment's 00:00 time is checked
+		// is taken care of in moment-ambig.js
+
+		it('should return 00:00 for a moment with 00:00 time', function() {
+			var mom = $.fullCalendar.moment.utc('2014-06-08T00:00:00');
+			var time = mom.time();
+			expect(time).toEqualDuration('00:00');
+		});
+
+		it('should return the time of a moment with a time', function() {
+			var mom = $.fullCalendar.moment.utc('2014-06-08T07:30:00');
+			var time = mom.time();
+			expect(time).toEqualDuration('07:30');
+		});
+	});
+
+	describe('setter', function() {
+
+		// the scenario where an ambiguously-timed moment is given a time via the setter
+		// is taken care of in moment-ambig.js
+
+		describe('when setting with a Duration', function() {
+
+			it('should give a moment with 00:00 a time', function() {
+				var mom = $.fullCalendar.moment.utc('2014-06-08T00:00:00');
+				var dur = moment.duration('13:25');
+				mom.time(dur);
+				expect(mom).toEqualMoment('2014-06-08T13:25:00+00:00');
+			});
+
+			it('should overwrite the time of a moment with a time', function() {
+				var mom = $.fullCalendar.moment.utc('2014-06-08T05:00:00');
+				var dur = moment.duration('13:25');
+				mom.time(dur);
+				expect(mom).toEqualMoment('2014-06-08T13:25:00+00:00');
+			});
+
+			it('should move to next day if greater than 24 hours', function() {
+				var mom = $.fullCalendar.moment.utc('2014-06-08T00:00:00');
+				var dur = moment.duration('1.01:00:00'); // 1 day, 1 hour
+				mom.time(dur);
+				expect(mom).toEqualMoment('2014-06-09T01:00:00+00:00');
+			});
+		});
+
+		describe('when setting with another Moment', function() {
+
+			it('should give a moment with 00:00 a time', function() {
+				var mom1 = $.fullCalendar.moment.utc('2014-06-09T00:00:00');
+				var mom2 = $.fullCalendar.moment.utc('2014-07-22T05:30:00'); // a Tues, so .days() -> 2
+				mom1.time(mom2);
+				expect(mom1).toEqualMoment('2014-06-09T05:30:00+00:00');
+			});
+
+			it('should overwrite the time of a moment with a time', function() {
+				var mom1 = $.fullCalendar.moment.utc('2014-06-09T04:15:00');
+				var mom2 = $.fullCalendar.moment.utc('2014-07-22T05:30:00'); // a Tues, so .days() -> 2
+				mom1.time(mom2);
+				expect(mom1).toEqualMoment('2014-06-09T05:30:00+00:00');
+			});
+		});
+	});
+});