Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created June 2, 2020 19:11
Show Gist options
  • Save calexandrepcjr/aa181c6f30a371e2b7d6d47f0e6d07fc to your computer and use it in GitHub Desktop.
Save calexandrepcjr/aa181c6f30a371e2b7d6d47f0e6d07fc to your computer and use it in GitHub Desktop.
Helps testing some routines regarding time, freezing dates
export class DateFreeze {
private readonly originalNow: () => number = Date.now.bind(Date);
public constructor(private timestamp: number) {}
public freeze(timestamp?: number): this {
if (timestamp !== undefined) {
this.timestamp = timestamp;
}
Date.now = () => this.timestamp;
return this;
}
public unfreeze(): this {
Date.now = this.originalNow;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment