Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

keep system

  • Loading branch information...
commit 789fd42a12081dc2cb11f246a9c7fa9a83d12eca 1 parent 62e8194
@schickling authored
Showing with 27 additions and 3 deletions.
  1. +1 −0  README.md
  2. +8 −2 src/timemachine.js
  3. +18 −1 test/timemachine.js
View
1  README.md
@@ -32,6 +32,7 @@ Parameter | Description | Default
`timestamp` | Date you want set as a timestamp | `0`
`difference` | Time offset in milliseconds | `0`
`tick` | Whether the system clock should continue ticking | `false`
+`keepTime` | Whether the system time should be kept | `false`
### Reset
```js
View
10 src/timemachine.js
@@ -21,11 +21,13 @@
timestamp: 0,
tick: false,
tickStartDate: null,
+ keepTime: false,
difference: 0,
config: function(options) {
this.timestamp = OriginalDate.parse(options.dateString) || options.timestamp || this.timestamp;
this.difference = options.difference || this.difference;
+ this.keepTime = options.keepTime || this.keepTime;
this.tick = options.tick || this.tick;
if (this.tick) {
this.tickStartDate = new OriginalDate();
@@ -37,6 +39,7 @@
this.timestamp = 0;
this.tick = false;
this.tickStartDate = null;
+ this.keepTime = false;
this.difference = 0;
Date = OriginalDate;
Date.prototype = OriginalDate.prototype;
@@ -47,7 +50,9 @@
Date = function() {
var date;
- if (arguments.length === 1) {
+ if (self.keepTime) {
+ date = new OriginalDate();
+ } else if (arguments.length === 1) {
date = new OriginalDate(arguments[0]);
} else if (arguments.length === 7) {
date = new OriginalDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
@@ -65,7 +70,8 @@
Date.prototype = OriginalDate.prototype;
Date.now = function() {
- return self.timestamp + self._getDifference();
+ var timestamp = self.keepTime ? OriginalDate.now() : self.timestamp;
+ return timestamp + self._getDifference();
};
Date.OriginalDate = OriginalDate;
View
19 test/timemachine.js
@@ -68,6 +68,14 @@ describe('check timemachine', function() {
expect(new Date().toUTCString()).toBe('Thu, 01 Jan 1970 00:00:01 GMT');
});
+ it('should keep system time', function() {
+ timemachine.config({
+ keepTime: true
+ });
+ var now = new Date.OriginalDate().toUTCString();
+ expect(new Date().toUTCString()).toBe(now);
+ });
+
});
describe('check Date.now()', function() {
@@ -77,8 +85,9 @@ describe('check timemachine', function() {
});
it('should reset', function() {
+ var now = Date.OriginalDate.now();
timemachine.reset();
- expect(Date.now()).not.toBe(0);
+ expect(Date.now()).toBe(now);
});
it('should add a time difference as milliseconds', function() {
@@ -103,6 +112,14 @@ describe('check timemachine', function() {
expect(Date.now()).toBe(1000);
});
+ it('should keep system time', function() {
+ timemachine.config({
+ keepTime: true
+ });
+ var now = Date.OriginalDate.now();
+ expect(Date.now()).toBe(now);
+ });
+
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.