Skip to content

Commit

Permalink
Merge 97369a3 into 6a5a1dd
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstebo authored Oct 11, 2018
2 parents 6a5a1dd + 97369a3 commit fcb8573
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Contents
- [Faker.Name](doc/name.md)
- [Faker.Number](doc/number.md)
- [Faker.Random](doc/random.md)
- [Faker.Time](doc/time.md)

## Installing

Expand Down
2 changes: 1 addition & 1 deletion doc/boolean.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Faker.Boolean.Boolean() //=> true
Faker.Boolean.Boolean(0.2) //=> false
```
```
2 changes: 1 addition & 1 deletion doc/number.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Faker.Number.Negative() //=> -4480.042585669558
Faker.Number.NonZeroDigit() //=> "8"
Faker.Number.Digit() //=> "1"
```
```
21 changes: 21 additions & 0 deletions doc/time.md
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"
```
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public static class Faker
public static INameFaker Name { get; } = Container.Name;
public static INumberFaker Number { get; } = Container.Number;
public static IRandomFaker Random { get; } = Container.Random;
public static ITimeFaker Time { get; } = Container.Time;
}
}
3 changes: 3 additions & 0 deletions src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal interface IFakerContainer
INameFaker Name { get; }
INumberFaker Number { get; }
IRandomFaker Random { get; }
ITimeFaker Time { get; }
}

internal class FakerContainer : IFakerContainer
Expand All @@ -26,6 +27,7 @@ public FakerContainer()
Name = new NameFaker(this);
Number = new NumberFaker();
Random = new RandomFaker();
Time = new TimeFaker();
}

public IAppFaker App { get; }
Expand All @@ -36,5 +38,6 @@ public FakerContainer()
public INameFaker Name { get; }
public INumberFaker Number { get; }
public IRandomFaker Random { get; }
public ITimeFaker Time { get; }
}
}
94 changes: 94 additions & 0 deletions src/FakerDotNet/Fakers/TimeFaker.cs
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);
}
}
}
}
14 changes: 14 additions & 0 deletions src/FakerDotNet/TimePeriod.cs
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
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ public void Random_returns_IRandomFaker()
{
Assert.IsInstanceOf<IRandomFaker>(_fakerContainer.Random);
}

[Test]
public void Time_returns_ITimeFaker()
{
Assert.IsInstanceOf<ITimeFaker>(_fakerContainer.Time);
}
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ public void Random_returns_IRandomFaker()
{
Assert.IsInstanceOf<IRandomFaker>(Faker.Random);
}

[Test]
public void Time_returns_ITimeFaker()
{
Assert.IsInstanceOf<ITimeFaker>(Faker.Time);
}
}
}
Loading

0 comments on commit fcb8573

Please sign in to comment.