Skip to content

Commit

Permalink
Merge pull request #807 from siwalikm/master
Browse files Browse the repository at this point in the history
Adding support for fake Time Method()
  • Loading branch information
Marak authored Jun 10, 2020
2 parents 36c0068 + 89edd15 commit a4e7842
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function Faker (opts) {
var _Date = require('./date');
self.date = new _Date(self);

var _Time = require('./time');
self.time = new _Time(self);

var Commerce = require('./commerce');
self.commerce = new Commerce(self);

Expand Down
37 changes: 37 additions & 0 deletions lib/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
*
* @namespace faker.time
*/
var _Time = function(faker) {
var self = this;

/**
* recent
*
* @method faker.time.recent
* @param {string} outputType - 'abbr' || 'wide' || 'unix' (default choice)
*/
self.recent = function(outputType) {
if (typeof outputType === "undefined") {
outputType = 'unix';
}

var date = new Date();
switch (outputType) {
case "abbr":
date = date.toLocaleTimeString();
break;
case "wide":
date = date.toTimeString();
break;
case "unix":
date = date.getTime();
break;
}
return date;
};

return self;
};

module["exports"] = _Time;
26 changes: 26 additions & 0 deletions test/time.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
}

describe("time.js", function () {
describe("recent()", function () {
it("returns the recent timestamp in Unix time format", function () {
var date = faker.time.recent();
assert.ok(typeof date === 'number');
assert.ok(date == new Date().getTime());
});

it("returns the recent timestamp in full time string format", function () {
var date = faker.time.recent('wide');
assert.ok(date == new Date().toTimeString());
});

it("returns the recent timestamp in abbreviated string format", function () {
var date = faker.time.recent('abbr');
assert.ok(date == new Date().toLocaleTimeString());
});
});

});

0 comments on commit a4e7842

Please sign in to comment.