aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Perez Alvarez <[email protected]>2014-11-12 08:43:23 +0000
committerDaniel Perez Alvarez <[email protected]>2014-11-12 08:43:23 +0000
commitba482f10bed1a4e34cf1941bb5728d0a029822a4 (patch)
tree8ee41919fd2bad1348f95c64b2dd7a91e1617221
parentc109b4873aa794965d37bda10dbacc51e2073bc8 (diff)
downloadfaker-ba482f10bed1a4e34cf1941bb5728d0a029822a4.tar.xz
faker-ba482f10bed1a4e34cf1941bb5728d0a029822a4.zip
Have past, future and recent return random dates
Functions `past` and `future` only returned dates EXACTLY N YEARS before or after the reference date. Function `recent` only returned dates EXACTLY N DAYS before the reference date. For example, `faker.date.past(1)` would return either *Nov 12, 2014*, or *Nov 12, 2013*, but nothing in-between. Also, now you can call those functions without parameter, and N will take the default value of `1`.
-rw-r--r--lib/date.js20
-rw-r--r--test/date.unit.js10
2 files changed, 26 insertions, 4 deletions
diff --git a/lib/date.js b/lib/date.js
index 01d505eb..afe9fdc9 100644
--- a/lib/date.js
+++ b/lib/date.js
@@ -4,9 +4,13 @@ var date = {
past: function (years, refDate) {
var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
+ var range = {
+ min: 1000,
+ max: (years || 1) * 365 * 24 * 3600 * 1000
+ };
var past = date.getTime();
- past -= faker.random.number(years) * 365 * 24 * 3600 * 1000; // some time from now to N years ago, in milliseconds
+ past -= faker.random.number(range); // some time from now to N years ago, in milliseconds
date.setTime(past);
return date;
@@ -14,8 +18,13 @@ var date = {
future: function (years, refDate) {
var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
+ var range = {
+ min: 1000,
+ max: (years || 1) * 365 * 24 * 3600 * 1000
+ };
+
var future = date.getTime();
- future += faker.random.number(years) * 365 * 3600 * 1000 + 1000; // some time from now to N years later, in milliseconds
+ future += faker.random.number(range); // some time from now to N years later, in milliseconds
date.setTime(future);
return date;
@@ -32,8 +41,13 @@ var date = {
recent: function (days) {
var date = new Date();
+ var range = {
+ min: 1000,
+ max: (days || 1) * 24 * 3600 * 1000
+ };
+
var future = date.getTime();
- future -= faker.random.number(days) * 24 * 60 * 60 * 1000; // some time from now to N days ago, in milliseconds
+ future -= faker.random.number(range); // some time from now to N days ago, in milliseconds
date.setTime(future);
return date;
diff --git a/test/date.unit.js b/test/date.unit.js
index b15e2043..cc0cddc7 100644
--- a/test/date.unit.js
+++ b/test/date.unit.js
@@ -12,6 +12,14 @@ describe("date.js", function () {
assert.ok(date < new Date());
});
+ it("returns a past date when N = 0", function () {
+
+ var refDate = new Date();
+ var date = faker.date.past(0, refDate.toJSON());
+
+ assert.ok(date < refDate); // date should be before the date given
+ });
+
it("returns a date N years before the date given", function () {
var refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
@@ -36,7 +44,7 @@ describe("date.js", function () {
var refDate = new Date();
var date = faker.date.future(0, refDate.toJSON());
- assert.ok(date > refDate); // date should be after the date given, but before the current time
+ assert.ok(date > refDate); // date should be after the date given
});
it("returns a date N years after the date given", function () {