Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date time generator #15

Merged
merged 2 commits into from Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/faker.dart
Expand Up @@ -20,4 +20,7 @@ main() {

// Generate random decimal.
print(random.decimal());

// Generate random DateTime, between the years 2000 and 2020
print(faker.date.dateTime(minYear: 2000, maxYear: 2020));
}
34 changes: 34 additions & 0 deletions lib/src/data/date_time/time_zones.dart
@@ -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',
];
122 changes: 122 additions & 0 deletions lib/src/date.dart
@@ -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}';
}
}
3 changes: 3 additions & 0 deletions lib/src/faker.dart
@@ -1,3 +1,4 @@
import 'package:faker/src/date.dart';
import 'package:faker/src/lorem.dart';

import 'address.dart';
Expand Down Expand Up @@ -26,6 +27,7 @@ class Faker {
final Lorem lorem;
final Person person;
final Sport sport;
final Date date;
final RandomGenerator randomGenerator;

const Faker()
Expand All @@ -40,5 +42,6 @@ class Faker {
lorem = const Lorem(),
person = const Person(),
sport = const Sport(),
date = const Date(),
randomGenerator = const RandomGenerator();
}
2 changes: 2 additions & 0 deletions test/runner.dart
Expand Up @@ -9,6 +9,7 @@ import 'specs/job.dart' as job;
import 'specs/lorem.dart' as lorem;
import 'specs/person.dart' as person;
import 'specs/sport.dart' as sport;
import 'specs/date.dart' as date;
import 'specs/random_generator.dart' as random;

main() {
Expand All @@ -23,5 +24,6 @@ main() {
lorem.main();
person.main();
sport.main();
date.main();
random.main();
}
28 changes: 28 additions & 0 deletions test/specs/date.dart
@@ -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}))*$")));
});
});
}