Skip to content

Commit ed742d3

Browse files
committed
Logging + Cleanup
1 parent a93eed4 commit ed742d3

File tree

10 files changed

+51
-81
lines changed

10 files changed

+51
-81
lines changed

src/Exceptionless.DateTimeExtensions/BusinessWeek.cs

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5-
namespace Exceptionless.DateTimeExtensions
6-
{
5+
namespace Exceptionless.DateTimeExtensions {
76
/// <summary>
87
/// A class representing a business week.
98
/// </summary>
10-
public class BusinessWeek
11-
{
9+
public class BusinessWeek {
1210
/// <summary>
1311
/// Initializes a new instance of the <see cref="BusinessWeek"/> class.
1412
/// </summary>
15-
public BusinessWeek()
16-
{
13+
public BusinessWeek() {
1714
BusinessDays = new List<BusinessDay>();
1815
}
1916

@@ -30,8 +27,7 @@ public BusinessWeek()
3027
/// <returns>
3128
/// <c>true</c> if the specified date falls on a business day; otherwise, <c>false</c>.
3229
/// </returns>
33-
public bool IsBusinessDay(DateTime date)
34-
{
30+
public bool IsBusinessDay(DateTime date) {
3531
return BusinessDays.Any(day => day.IsBusinessDay(date));
3632
}
3733

@@ -47,20 +43,16 @@ public bool IsBusinessDay(DateTime date)
4743
/// Business time is calculated by adding only the time that falls inside the business day range.
4844
/// If all the time between the start and end date fall outside the business day, the time will be zero.
4945
/// </remarks>
50-
public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate)
51-
{
46+
public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate) {
5247
Validate(true);
5348

5449
var businessTime = TimeSpan.Zero;
5550
var workingDate = startDate;
5651

57-
while (workingDate < endDate)
58-
{
59-
DateTime businessStart;
60-
BusinessDay businessDay;
52+
while (workingDate < endDate) {
6153

6254
// get start date
63-
if (!NextBusinessDay(workingDate, out businessStart, out businessDay))
55+
if (!NextBusinessDay(workingDate, out var businessStart, out var businessDay))
6456
break;
6557

6658
// business start after end date
@@ -70,11 +62,10 @@ public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate)
7062
if (businessDay == null)
7163
break;
7264

73-
TimeSpan timeToEndOfDay = businessDay.EndTime.Subtract(businessStart.TimeOfDay);
74-
DateTime businessEnd = businessStart.SafeAdd(timeToEndOfDay);
65+
var timeToEndOfDay = businessDay.EndTime.Subtract(businessStart.TimeOfDay);
66+
var businessEnd = businessStart.SafeAdd(timeToEndOfDay);
7567

76-
if (endDate <= businessEnd)
77-
{
68+
if (endDate <= businessEnd) {
7869
timeToEndOfDay = endDate.TimeOfDay.Subtract(businessStart.TimeOfDay);
7970
businessTime = businessTime.Add(timeToEndOfDay);
8071
return businessTime;
@@ -94,23 +85,19 @@ public TimeSpan GetBusinessTime(DateTime startDate, DateTime endDate)
9485
/// <param name="startDate">The start date.</param>
9586
/// <param name="businessTime">The business time.</param>
9687
/// <returns></returns>
97-
public DateTime GetBusinessEndDate(DateTime startDate, TimeSpan businessTime)
98-
{
88+
public DateTime GetBusinessEndDate(DateTime startDate, TimeSpan businessTime) {
9989
Validate(true);
10090

10191
var endDate = startDate;
10292
var remainingTime = businessTime;
10393

104-
while (remainingTime > TimeSpan.Zero)
105-
{
106-
DateTime businessStart;
107-
BusinessDay businessDay;
94+
while (remainingTime > TimeSpan.Zero) {
10895

10996
// get start date
110-
if (!NextBusinessDay(endDate, out businessStart, out businessDay))
97+
if (!NextBusinessDay(endDate, out var businessStart, out var businessDay))
11198
break;
11299

113-
TimeSpan timeForDay = businessDay.EndTime.Subtract(businessStart.TimeOfDay);
100+
var timeForDay = businessDay.EndTime.Subtract(businessStart.TimeOfDay);
114101
if (remainingTime <= timeForDay)
115102
return businessStart.SafeAdd(remainingTime);
116103

@@ -127,8 +114,7 @@ public DateTime GetBusinessEndDate(DateTime startDate, TimeSpan businessTime)
127114
/// </summary>
128115
/// <param name="throwExcption">if set to <c>true</c> throw excption if invalid.</param>
129116
/// <returns><c>true</c> if valid; otherwise <c>false</c>.</returns>
130-
protected virtual bool Validate(bool throwExcption)
131-
{
117+
protected virtual bool Validate(bool throwExcption) {
132118
if (BusinessDays.Any())
133119
return true;
134120

@@ -138,38 +124,33 @@ protected virtual bool Validate(bool throwExcption)
138124
return false;
139125
}
140126

141-
internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out BusinessDay businessDay)
142-
{
127+
internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out BusinessDay businessDay) {
143128
nextDate = startDate;
144129
businessDay = null;
145130

146131
var tree = GetDayTree();
147132

148133
// loop no more then 7 times
149-
for (int x = 0; x < 7; x++)
150-
{
151-
DayOfWeek dayOfWeek = nextDate.DayOfWeek;
134+
for (int x = 0; x < 7; x++) {
135+
var dayOfWeek = nextDate.DayOfWeek;
152136

153-
if (!tree.ContainsKey(dayOfWeek))
154-
{
137+
if (!tree.ContainsKey(dayOfWeek)) {
155138
// no business days on this day of the week
156139
nextDate = nextDate.AddDays(1).Date;
157140
continue;
158141
}
159142

160-
IList<BusinessDay> businessDays = tree[dayOfWeek];
143+
var businessDays = tree[dayOfWeek];
161144
if (businessDays == null)
162145
continue;
163146

164-
foreach (BusinessDay day in businessDays)
165-
{
147+
foreach (var day in businessDays) {
166148
if (day == null)
167149
continue;
168150

169-
TimeSpan timeOfDay = nextDate.TimeOfDay;
151+
var timeOfDay = nextDate.TimeOfDay;
170152

171-
if (timeOfDay >= day.StartTime && timeOfDay < day.EndTime)
172-
{
153+
if (timeOfDay >= day.StartTime && timeOfDay < day.EndTime) {
173154
// working date in range
174155
businessDay = day;
175156
return true;
@@ -196,19 +177,14 @@ internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out Bus
196177

197178
private Dictionary<DayOfWeek, IList<BusinessDay>> _dayTree;
198179

199-
private Dictionary<DayOfWeek, IList<BusinessDay>> GetDayTree()
200-
{
180+
private Dictionary<DayOfWeek, IList<BusinessDay>> GetDayTree() {
201181
if (_dayTree != null)
202182
return _dayTree;
203183

204184
_dayTree = new Dictionary<DayOfWeek, IList<BusinessDay>>();
205-
var days = BusinessDays
206-
.OrderBy(b => b.DayOfWeek)
207-
.ThenBy(b => b.StartTime)
208-
.ToList();
185+
var days = BusinessDays.OrderBy(b => b.DayOfWeek).ThenBy(b => b.StartTime).ToList();
209186

210-
foreach (var day in days)
211-
{
187+
foreach (var day in days) {
212188
if (!_dayTree.ContainsKey(day.DayOfWeek))
213189
_dayTree.Add(day.DayOfWeek, new List<BusinessDay>());
214190

@@ -226,16 +202,14 @@ private Dictionary<DayOfWeek, IList<BusinessDay>> GetDayTree()
226202
/// <summary>
227203
/// Nested class to lazy-load singleton.
228204
/// </summary>
229-
private class Nested
230-
{
205+
private class Nested {
231206
/// <summary>
232207
/// Initializes the Nested class.
233208
/// </summary>
234209
/// <remarks>
235210
/// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit.
236211
/// </remarks>
237-
static Nested()
238-
{
212+
static Nested() {
239213
Current = new BusinessWeek();
240214
Current.BusinessDays.Add(new BusinessDay(DayOfWeek.Monday));
241215
Current.BusinessDays.Add(new BusinessDay(DayOfWeek.Tuesday));
@@ -247,7 +221,7 @@ static Nested()
247221
/// <summary>
248222
/// Current singleton instance.
249223
/// </summary>
250-
internal readonly static BusinessWeek Current;
224+
internal static readonly BusinessWeek Current;
251225
}
252226
}
253227
}

src/Exceptionless.DateTimeExtensions/DateTimeExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static DateTime SafeSubtract(this DateTime date, TimeSpan value) {
3939
}
4040

4141
public static string ToApproximateAgeString(this DateTime fromDate) {
42-
var isFuture = fromDate > DateTime.Now;
42+
bool isFuture = fromDate > DateTime.Now;
4343
var age = isFuture ? GetAge(DateTime.Now, fromDate) : GetAge(fromDate);
4444
if (age.TotalMinutes <= 1d)
4545
return age.TotalSeconds > 0 ? "Just now" : "Right now";
@@ -78,7 +78,7 @@ public static AgeSpan GetAge(this DateTime fromDate, DateTime toDate) {
7878
}
7979

8080
public static int ToEpoch(this DateTime fromDate) {
81-
var utc = (fromDate.ToUniversalTime().Ticks - EPOCH_TICKS) / TimeSpan.TicksPerSecond;
81+
long utc = (fromDate.ToUniversalTime().Ticks - EPOCH_TICKS) / TimeSpan.TicksPerSecond;
8282
return Convert.ToInt32(utc);
8383
}
8484

@@ -265,7 +265,7 @@ public static DateTime Ceiling(this DateTime date, TimeSpan interval) {
265265
}
266266

267267
public static DateTime Round(this DateTime date, TimeSpan roundingInterval) {
268-
var halfIntervalTicks = ((roundingInterval.Ticks + 1) >> 1);
268+
long halfIntervalTicks = ((roundingInterval.Ticks + 1) >> 1);
269269
return date.AddTicks(halfIntervalTicks - ((date.Ticks + halfIntervalTicks) % roundingInterval.Ticks));
270270
}
271271

src/Exceptionless.DateTimeExtensions/DateTimeOffsetExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static DateTimeOffset SafeSubtract(this DateTimeOffset date, TimeSpan val
3939
}
4040

4141
public static string ToApproximateAgeString(this DateTimeOffset fromDate) {
42-
var isFuture = fromDate > DateTimeOffset.Now;
42+
bool isFuture = fromDate > DateTimeOffset.Now;
4343
var age = isFuture ? GetAge(DateTimeOffset.Now, fromDate) : GetAge(fromDate);
4444
if (age.TotalMinutes <= 1d)
4545
return age.TotalSeconds > 0 ? "Just now" : "Right now";
@@ -78,7 +78,7 @@ public static AgeSpan GetAge(this DateTimeOffset fromDate, DateTimeOffset toDate
7878
}
7979

8080
public static int ToEpoch(this DateTimeOffset fromDate) {
81-
var utc = (fromDate.ToUniversalTime().Ticks - EPOCH_TICKS) / TimeSpan.TicksPerSecond;
81+
long utc = (fromDate.ToUniversalTime().Ticks - EPOCH_TICKS) / TimeSpan.TicksPerSecond;
8282
return Convert.ToInt32(utc);
8383
}
8484

@@ -272,7 +272,7 @@ public static DateTimeOffset Ceiling(this DateTimeOffset date, TimeSpan interval
272272
}
273273

274274
public static DateTimeOffset Round(this DateTimeOffset date, TimeSpan roundingInterval) {
275-
var halfIntervalTicks = ((roundingInterval.Ticks + 1) >> 1);
275+
long halfIntervalTicks = ((roundingInterval.Ticks + 1) >> 1);
276276
return date.AddTicks(halfIntervalTicks - ((date.Ticks + halfIntervalTicks) % roundingInterval.Ticks));
277277
}
278278

src/Exceptionless.DateTimeExtensions/DateTimeRange.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public bool Intersects(DateTimeRange other) {
101101
}
102102

103103
public DateTimeRange Intersection(DateTimeRange other) {
104-
DateTime greatestStart = Start > other.Start ? Start : other.Start;
105-
DateTime smallestEnd = End < other.End ? End : other.End;
104+
var greatestStart = Start > other.Start ? Start : other.Start;
105+
var smallestEnd = End < other.End ? End : other.End;
106106

107107
if (greatestStart > smallestEnd)
108108
return null;

src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/ExplicitDateFormatParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public DateTimeRange Parse(string content, DateTimeOffset relativeBaseTime) {
1818
if (value.Length == 16)
1919
value += ":00";
2020

21-
DateTimeOffset date;
22-
if (!DateTimeOffset.TryParse(value, out date))
21+
if (!DateTimeOffset.TryParse(value, out var date))
2322
return null;
2423

2524
date = date.ChangeOffset(relativeBaseTime.Offset);

test/Exceptionless.DateTimeExtensions.Tests/BusinessDayTests.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void TotalBusinessHours() {
3232
var endDate = new DateTime(2010, 1, 2);
3333
var businessWeek = BusinessWeek.DefaultWeek;
3434

35-
TimeSpan time = businessWeek.GetBusinessTime(startDate, endDate);
35+
var time = businessWeek.GetBusinessTime(startDate, endDate);
3636
// workday friday
3737
Assert.Equal(8, time.TotalHours);
3838

@@ -66,10 +66,7 @@ public void TotalBusinessHours() {
6666
public void NextBusinessDay() {
6767
var businessWeek = BusinessWeek.DefaultWeek;
6868

69-
BusinessDay businessDay;
70-
DateTime resultDate;
71-
72-
var result = businessWeek.NextBusinessDay(new DateTime(2010, 1, 1, 2, 0, 0), out resultDate, out businessDay);
69+
bool result = businessWeek.NextBusinessDay(new DateTime(2010, 1, 1, 2, 0, 0), out var resultDate, out var businessDay);
7370
Assert.True(result);
7471
Assert.Equal(new DateTime(2010, 1, 1, 9, 0, 0), resultDate);
7572
Assert.NotNull(businessDay);
@@ -95,8 +92,8 @@ public void GetBusinessTime() {
9592
var endDate = new DateTime(2010, 1, 6, 13, 14, 16);
9693
var businessWeek = BusinessWeek.DefaultWeek;
9794

98-
Stopwatch watch = Stopwatch.StartNew();
99-
TimeSpan time = businessWeek.GetBusinessTime(startDate, endDate);
95+
var watch = Stopwatch.StartNew();
96+
var time = businessWeek.GetBusinessTime(startDate, endDate);
10097
watch.Stop();
10198

10299
Console.WriteLine("Business Time: {0}", time);
@@ -134,7 +131,7 @@ public void GetBusinessDate() {
134131
var endDate = new DateTime(2010, 1, 4, 11, 0, 0);
135132
var businessWeek = BusinessWeek.DefaultWeek;
136133

137-
Stopwatch watch = Stopwatch.StartNew();
134+
var watch = Stopwatch.StartNew();
138135
var resultDate = businessWeek.GetBusinessEndDate(startDate, TimeSpan.FromHours(10));
139136
watch.Stop();
140137
Console.WriteLine("Business Date: {0}", resultDate);
@@ -155,7 +152,7 @@ public void GetBusinessDate() {
155152

156153
[Fact]
157154
public void ThridShift() {
158-
BusinessWeek businessWeek = new BusinessWeek();
155+
var businessWeek = new BusinessWeek();
159156
//day 1
160157
businessWeek.BusinessDays.Add(new BusinessDay(DayOfWeek.Sunday, TimeSpan.FromHours(22), TimeSpan.FromHours(24)));
161158
businessWeek.BusinessDays.Add(new BusinessDay(DayOfWeek.Monday, TimeSpan.Zero, TimeSpan.FromHours(6)));
@@ -176,7 +173,7 @@ public void ThridShift() {
176173
var startDate = new DateTime(2010, 1, 3, 22, 0, 0);
177174
var endDate = new DateTime(2010, 1, 4, 6, 0, 0);
178175

179-
TimeSpan time = businessWeek.GetBusinessTime(startDate, endDate);
176+
var time = businessWeek.GetBusinessTime(startDate, endDate);
180177

181178
Assert.Equal(8, time.TotalHours);
182179

test/Exceptionless.DateTimeExtensions.Tests/DateTimeRangeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void CanAddAndSubtract() {
3434
[Fact]
3535
public void CanParseIntoLocalTime() {
3636
const string time = "2016-12-28T05:00:00-2016-12-28T05:30:00";
37-
TimeSpan utcOffset = TimeSpan.FromHours(-1);
37+
var utcOffset = TimeSpan.FromHours(-1);
3838
var localRange = DateTimeRange.Parse(time, DateTimeOffset.UtcNow.ChangeOffset(utcOffset));
3939
Assert.Equal(new DateTime(2016, 12, 28, 5, 0, 0, DateTimeKind.Unspecified), localRange.Start);
4040
Assert.Equal(new DateTime(2016, 12, 28, 5, 30, 0, DateTimeKind.Unspecified), localRange.End);

test/Exceptionless.DateTimeExtensions.Tests/Exceptionless.DateTimeExtensions.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ProjectReference Include="..\..\src\Exceptionless.DateTimeExtensions\Exceptionless.DateTimeExtensions.csproj" />
1313
</ItemGroup>
1414
<ItemGroup>
15-
<PackageReference Include="Foundatio.Logging.Xunit" Version="5.1.1562" />
15+
<PackageReference Include="Foundatio.Logging.Xunit" Version="6.0.1566-pre" />
1616
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
1717
<PackageReference Include="xunit" Version="2.3.1" />
1818
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

test/Exceptionless.DateTimeExtensions.Tests/FormatParsers/FormatParserTestsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Exceptionless.DateTimeExtensions.FormatParsers;
3-
using Foundatio.Logging;
43
using Foundatio.Logging.Xunit;
4+
using Microsoft.Extensions.Logging;
55
using Xunit;
66
using Xunit.Abstractions;
77

@@ -16,7 +16,7 @@ static FormatParserTestsBase() {
1616
}
1717

1818
public void ValidateInput(IFormatParser parser, string input, DateTime? start, DateTime? end) {
19-
_logger.Info(String.Format("Input: {0}, Now: {1}, Start: {2}, End: {3}", input, _now, start, end));
19+
_logger.LogInformation("Input: {Input}, Now: {Now}, Start: {Start}, End: {End}", input, _now, start, end);
2020
var range = parser.Parse(input, _now);
2121
if (range == null) {
2222
Assert.Null(start);

test/Exceptionless.DateTimeExtensions.Tests/FormatParsers/PartParsers/PartParserTestsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Exceptionless.DateTimeExtensions.FormatParsers.PartParsers;
3-
using Foundatio.Logging;
43
using Foundatio.Logging.Xunit;
4+
using Microsoft.Extensions.Logging;
55
using Xunit;
66
using Xunit.Abstractions;
77

@@ -16,7 +16,7 @@ static PartParserTestsBase() {
1616
}
1717

1818
public void ValidateInput(IPartParser parser, string input, bool isUpperLimit, DateTimeOffset? expected) {
19-
_logger.Info(String.Format("Input: {0}, Now: {1}, IsUpperLimit: {2}, Expected: {3}", input, _now, isUpperLimit, expected));
19+
_logger.LogInformation("Input: {Input}, Now: {Now}, IsUpperLimit: {IsUpperLimit}, Expected: {Expected}", input, _now, isUpperLimit, expected);
2020
var match = parser.Regex.Match(input);
2121
if (!match.Success) {
2222
Assert.Null(expected);

0 commit comments

Comments
 (0)