Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from TNorbury/dateTimeGenerator
Date time generator
- Loading branch information
Showing
6 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const timeZones = [ | ||
'GMT', | ||
'UTC', | ||
'ECT', | ||
'EET', | ||
'ART', | ||
'EAT', | ||
'MET', | ||
'NET', | ||
'PLT', | ||
'IST', | ||
'BST', | ||
'VST', | ||
'CTT', | ||
'JST', | ||
'ACT', | ||
'AET', | ||
'SST', | ||
'NST', | ||
'MIT', | ||
'HST', | ||
'AST', | ||
'PST', | ||
'PNT', | ||
'MST', | ||
'CST', | ||
'EST', | ||
'IET', | ||
'PRT', | ||
'CNT', | ||
'AGT', | ||
'BET', | ||
'CAT', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import 'data/date_time/time_zones.dart'; | ||
import 'random_generator.dart'; | ||
|
||
class Date { | ||
const Date(); | ||
|
||
static final _yearSuffixes = [ | ||
"BC", | ||
"AD", | ||
]; | ||
|
||
static final _timeSuffixes = [ | ||
"AM", | ||
"PM", | ||
]; | ||
|
||
static final _months = [ | ||
"January", | ||
"February", | ||
"March", | ||
"April", | ||
"May", | ||
"June", | ||
"July", | ||
"August", | ||
"September", | ||
"October", | ||
"November", | ||
"December" | ||
]; | ||
|
||
/// Generates a random [DateTime] | ||
/// By default the range of the years will be between 0 and 10000. But that can | ||
/// be adjusted with the minYear and maxYear args | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// faker.date.dateTime(); | ||
/// faker.date.dateTime(minYear: 2000, maxYear: 2020); | ||
/// ``` | ||
DateTime dateTime({int minYear = 0, int maxYear = 10000}) { | ||
return DateTime( | ||
random.integer(maxYear, min: minYear), // year | ||
random.integer(13, min: 1), // month | ||
random.integer(32, min: 1), // day | ||
random.integer(24), // hour | ||
random.integer(60), // minute | ||
random.integer(60), // second | ||
random.integer(1000), // millisecond | ||
random.integer(1000), // microsecond | ||
); | ||
} | ||
|
||
/// Generates a random month | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// faker.date.month() | ||
/// ``` | ||
String month() { | ||
return '${random.element(_months)}'; | ||
} | ||
|
||
/// Generates a random year. | ||
/// By default the range of the years will be between 0 and 10000. But that can | ||
/// be adjusted with the minYear and maxYear args | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// faker.date.year(); | ||
/// ``` | ||
String year({int minYear = 0, int maxYear = 10000}) { | ||
DateTime date = dateTime(minYear: minYear, maxYear: maxYear); | ||
|
||
// Just year | ||
if (random.boolean()) { | ||
return '${date.year}'; | ||
} | ||
// Year with suffix | ||
else { | ||
return '${date.year} ${random.element(_yearSuffixes)}'; | ||
} | ||
} | ||
|
||
/// Generates a random time | ||
/// Such as 23:52, 3:42 PM, 6:00 AM PST | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// faker.date.time() | ||
/// ``` | ||
String time() { | ||
DateTime date = dateTime(); | ||
|
||
String timeSuffix = ""; | ||
if (random.boolean()) { | ||
timeSuffix = random.element(_timeSuffixes) + " "; | ||
} | ||
|
||
String timeZone = ""; | ||
if (random.boolean()) { | ||
timeZone = random.element(timeZones); | ||
} | ||
|
||
// If we have a time suffix, convert the hour to a 12-hour clock | ||
String hour; | ||
if (timeSuffix.isNotEmpty) { | ||
hour = (date.hour % 13).toString(); | ||
} else { | ||
hour = | ||
(date.hour < 10) ? '0' + date.hour.toString() : date.hour.toString(); | ||
} | ||
|
||
// If the minute is a single digit (i.e. less than 10) | ||
// We want to prepend a 0 to it. | ||
String minute = (date.minute < 10) | ||
? '0' + date.minute.toString() | ||
: date.minute.toString(); | ||
|
||
return '${hour}:${minute} ${timeSuffix}${timeZone}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:faker/faker.dart'; | ||
|
||
main() { | ||
var faker = Faker(); | ||
|
||
group("date", () { | ||
test("should be able to generate a DateTime", () { | ||
expect(true, faker.date.dateTime() is DateTime); | ||
}); | ||
|
||
test("should be able to generate a month", () { | ||
expect(faker.date.month(), matches(new RegExp(r"^([\w\s\-])+$"))); | ||
}); | ||
|
||
test("should be able to generate a year", () { | ||
expect( | ||
faker.date.year(), matches(new RegExp(r"^([0-9]){1,5}( (BC|AD))?$"))); | ||
}); | ||
|
||
test('should be able to generate a time', () { | ||
expect( | ||
faker.date.time(), | ||
matches(new RegExp( | ||
r"^([0-9]){1,}:([0-9]){2} ?( (AM|PM|((\w){3})))?( )?(((\w){3}))*$"))); | ||
}); | ||
}); | ||
} |