-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
386 additions
and
2 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Faker::Book | ||
# Faker.Book | ||
|
||
```cs | ||
Faker.Book.Title() //=> "The Odd Sister" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Faker::Boolean | ||
# Faker.Boolean | ||
|
||
```cs | ||
Faker.Boolean.Boolean() //=> true | ||
|
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,20 @@ | ||
# Faker.Date | ||
|
||
```cs | ||
// Random date between dates | ||
Faker.Date.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30)) //=> "Wed, 24 Sep 2014" | ||
Faker.Date.Between("2014-09-01", "2014-09-30") //=> "Wed, 24 Sep 2014" | ||
// Random date between dates except for certain date | ||
Faker.Date.BetweenExcept(new DateTime(2014, 9, 1), new DateTime(2015, 9, 1), DateTime.UtcNow) //=> "Wed, 24 Sep 2014" | ||
Faker.Date.BetweenExcept("2014-01-01", "2015-01-30") | ||
|
||
// Random date in the future (up to maximum of N days) | ||
Faker.Date.Forward(23) //=> "Fri, 03 Oct 2014" | ||
// Random date in the past (up to maximum of N days) | ||
Faker.Date.Backward(14) //=> "Fri, 19 Sep 2014" | ||
// Random birthday date (maximum age between 18 and 65) | ||
Faker.Date.Birthday(18, 65) //=> "Fri, 28 Mar 1986" | ||
``` |
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,86 @@ | ||
using System; | ||
using FakerDotNet.Wrappers; | ||
|
||
namespace FakerDotNet.Fakers | ||
{ | ||
public interface IDateFaker | ||
{ | ||
DateTime Between(string from, string to); | ||
DateTime Between(DateTime from, DateTime to); | ||
DateTime BetweenExcept(string from, string to, string except); | ||
DateTime BetweenExcept(DateTime from, DateTime to, DateTime except); | ||
DateTime Forward(int days = 365); | ||
DateTime Backward(int days = 365); | ||
DateTime Birthday(int minAge = 18, int maxAge = 65); | ||
} | ||
|
||
internal class DateFaker : IDateFaker | ||
{ | ||
private readonly IRandomWrapper _randomWrapper; | ||
|
||
public DateFaker() | ||
: this(new RandomWrapper()) | ||
{ | ||
} | ||
|
||
internal DateFaker(IRandomWrapper randomWrapper) | ||
{ | ||
_randomWrapper = randomWrapper; | ||
} | ||
|
||
public DateTime Between(string from, string to) | ||
{ | ||
return Between(DateTime.Parse(from), DateTime.Parse(to)); | ||
} | ||
|
||
public DateTime Between(DateTime from, DateTime to) | ||
{ | ||
return from.AddDays(_randomWrapper.Next(0, to.Subtract(from).Days)); | ||
} | ||
|
||
public DateTime BetweenExcept(string from, string to, string except) | ||
{ | ||
return BetweenExcept(DateTime.Parse(from), DateTime.Parse(to), DateTime.Parse(except)); | ||
} | ||
|
||
public DateTime BetweenExcept(DateTime from, DateTime to, DateTime except) | ||
{ | ||
if (from.Equals(to) && to.Equals(except)) | ||
throw new ArgumentException("From date, to date and excepted date must not be the same"); | ||
|
||
DateTime result; | ||
|
||
do | ||
{ | ||
result = from.Date.AddDays(_randomWrapper.Next(0, to.Subtract(from).Days)); | ||
} while (result.Equals(except.Date)); | ||
|
||
return result; | ||
} | ||
|
||
public DateTime Forward(int days = 365) | ||
{ | ||
var from = DateTime.UtcNow.Date.AddDays(1); | ||
var to = DateTime.UtcNow.Date.AddDays(days); | ||
|
||
return Between(from, to); | ||
} | ||
|
||
public DateTime Backward(int days = 365) | ||
{ | ||
var from = DateTime.UtcNow.Date.AddDays(-days); | ||
var to = DateTime.UtcNow.Date.AddDays(-1); | ||
|
||
return Between(from, to); | ||
} | ||
|
||
public DateTime Birthday(int minAge = 18, int maxAge = 65) | ||
{ | ||
var now = DateTime.UtcNow; | ||
var from = new DateTime(now.Year - maxAge); | ||
var to = new DateTime(now.Year - minAge); | ||
|
||
return Between(from, to); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.