-
Notifications
You must be signed in to change notification settings - Fork 13
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
11 changed files
with
375 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 |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
Faker.Boolean.Boolean() //=> true | ||
Faker.Boolean.Boolean(0.2) //=> false | ||
``` | ||
``` |
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,21 @@ | ||
# Faker.Time | ||
|
||
```cs | ||
// Random date between dates | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30)) //=> "2014-09-18 12:30:59 -0700" | ||
// Random date between dates (within specified part of the day) | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.All) //=> "2014-09-19 07:03:30 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Day) //=> "2014-09-18 16:28:13 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Night) //=> "2014-09-20 19:39:38 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Morning) //=> "2014-09-19 08:07:52 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Afternoon) //=> "2014-09-18 12:10:34 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Evening) //=> "2014-09-19 20:21:03 -0700" | ||
Faker.Time.Between(new DateTime(2014, 9, 1), new DateTime(2014, 9, 30), TimePeriod.Midnight) //=> "2014-09-20 00:40:14 -0700" | ||
// Random time in the future (up to maximum of N days) | ||
Faker.Time.Forward(23, TimePeriod.Morning) //=> "2014-09-26 06:54:47 -0700" | ||
// Random time in the past (up to maximum of N days) | ||
Faker.Time.Backward(14, TimePeriod.Evening) //=> "2014-09-17 19:56:33 -0700" | ||
``` |
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,94 @@ | ||
using System; | ||
using System.Data; | ||
using FakerDotNet.Wrappers; | ||
|
||
namespace FakerDotNet.Fakers | ||
{ | ||
public interface ITimeFaker | ||
{ | ||
DateTime Between(string from, string to, TimePeriod timePeriod = TimePeriod.All); | ||
DateTime Between(DateTime from, DateTime to, TimePeriod timePeriod = TimePeriod.All); | ||
DateTime Forward(int days = 365, TimePeriod timePeriod = TimePeriod.All); | ||
DateTime Backward(int days = 365, TimePeriod timePeriod = TimePeriod.All); | ||
} | ||
|
||
internal class TimeFaker : ITimeFaker | ||
{ | ||
private readonly IRandomWrapper _randomWrapper; | ||
|
||
public TimeFaker() | ||
: this(new RandomWrapper()) | ||
{ | ||
} | ||
|
||
internal TimeFaker(IRandomWrapper randomWrapper) | ||
{ | ||
_randomWrapper = randomWrapper; | ||
} | ||
|
||
public DateTime Between(string from, string to, TimePeriod timePeriod = TimePeriod.All) | ||
{ | ||
return Between(DateTime.Parse(from), DateTime.Parse(to), timePeriod); | ||
} | ||
|
||
public DateTime Between(DateTime from, DateTime to, TimePeriod timePeriod = TimePeriod.All) | ||
{ | ||
var date = from.AddDays(_randomWrapper.Next(0, to.Subtract(from).Days)); | ||
var range = RangeFor(timePeriod); | ||
|
||
return timePeriod == TimePeriod.Between | ||
? date.Date | ||
: new DateTime( | ||
date.Year, | ||
date.Month, | ||
date.Day, | ||
_randomWrapper.Next(range.Minimum, range.Maximum), | ||
_randomWrapper.Next(0, 59), | ||
_randomWrapper.Next(0, 59) | ||
); | ||
} | ||
|
||
public DateTime Forward(int days = 365, TimePeriod timePeriod = TimePeriod.All) | ||
{ | ||
var from = DateTime.UtcNow.Date.AddDays(1); | ||
var to = DateTime.UtcNow.Date.AddDays(days); | ||
|
||
return Between(from, to, timePeriod); | ||
} | ||
|
||
public DateTime Backward(int days = 365, TimePeriod timePeriod = TimePeriod.All) | ||
{ | ||
var from = DateTime.UtcNow.Date.AddDays(-days); | ||
var to = DateTime.UtcNow.Date.AddDays(-1); | ||
|
||
return Between(from, to, timePeriod); | ||
} | ||
|
||
private static Range<int> RangeFor(TimePeriod timePeriod) | ||
{ | ||
switch (timePeriod) | ||
{ | ||
case TimePeriod.Day: | ||
return new Range<int>(9, 17); | ||
|
||
case TimePeriod.Night: | ||
return new Range<int>(18, 23); | ||
|
||
case TimePeriod.Morning: | ||
return new Range<int>(6, 11); | ||
|
||
case TimePeriod.Afternoon: | ||
return new Range<int>(12, 17); | ||
|
||
case TimePeriod.Evening: | ||
return new Range<int>(17, 21); | ||
|
||
case TimePeriod.Midnight: | ||
return new Range<int>(0, 4); | ||
|
||
default: | ||
return new Range<int>(0, 23); | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
namespace FakerDotNet | ||
{ | ||
public enum TimePeriod | ||
{ | ||
All, | ||
Day, | ||
Night, | ||
Morning, | ||
Afternoon, | ||
Evening, | ||
Midnight, | ||
Between | ||
} | ||
} |
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.