|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | + |
| 4 | +namespace Exceptionless.DateTimeExtensions |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A class defining a business day. |
| 8 | + /// </summary> |
| 9 | + [DebuggerDisplay("DayOfWeek={DayOfWeek}, StartTime={StartTime}, EndTime={EndTime}")] |
| 10 | + public class BusinessDay |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Initializes a new instance of the <see cref="BusinessDay"/> class. |
| 14 | + /// </summary> |
| 15 | + /// <param name="dayOfWeek">The day of week this business day represents.</param> |
| 16 | + public BusinessDay(DayOfWeek dayOfWeek) |
| 17 | + { |
| 18 | + StartTime = TimeSpan.FromHours(9); // 9am |
| 19 | + EndTime = TimeSpan.FromHours(17); // 5pm |
| 20 | + DayOfWeek = dayOfWeek; |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="BusinessDay"/> class. |
| 25 | + /// </summary> |
| 26 | + /// <param name="dayOfWeek">The day of week this business day represents.</param> |
| 27 | + /// <param name="startTime">The start time of the business day.</param> |
| 28 | + /// <param name="endTime">The end time of the business day.</param> |
| 29 | + public BusinessDay(DayOfWeek dayOfWeek, TimeSpan startTime, TimeSpan endTime) |
| 30 | + { |
| 31 | + if (startTime.TotalDays >= 1) |
| 32 | + throw new ArgumentOutOfRangeException("startTime", startTime, "The startTime argument must be less then one day."); |
| 33 | + |
| 34 | + if (endTime.TotalDays > 1) |
| 35 | + throw new ArgumentOutOfRangeException("endTime", endTime, "The endTime argument must be less then one day."); |
| 36 | + |
| 37 | + if (endTime <= startTime) |
| 38 | + throw new ArgumentOutOfRangeException("endTime", endTime, "The endTime argument must be greater then startTime."); |
| 39 | + |
| 40 | + DayOfWeek = dayOfWeek; |
| 41 | + StartTime = startTime; |
| 42 | + EndTime = endTime; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets the day of week this business day represents.. |
| 47 | + /// </summary> |
| 48 | + /// <value>The day of week.</value> |
| 49 | + public DayOfWeek DayOfWeek { get; private set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the start time of the business day. |
| 53 | + /// </summary> |
| 54 | + /// <value>The start time of the business day.</value> |
| 55 | + public TimeSpan StartTime { get; private set; } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets the end time of the business day. |
| 59 | + /// </summary> |
| 60 | + /// <value>The end time of the business day.</value> |
| 61 | + public TimeSpan EndTime { get; private set; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Determines whether the specified date falls in the business day. |
| 65 | + /// </summary> |
| 66 | + /// <param name="date">The date to check.</param> |
| 67 | + /// <returns> |
| 68 | + /// <c>true</c> if the specified date falls in the business day; otherwise, <c>false</c>. |
| 69 | + /// </returns> |
| 70 | + public bool IsBusinessDay(DateTime date) |
| 71 | + { |
| 72 | + if (date.DayOfWeek != DayOfWeek) |
| 73 | + return false; |
| 74 | + |
| 75 | + if (date.TimeOfDay < StartTime) |
| 76 | + return false; |
| 77 | + |
| 78 | + if (date.TimeOfDay > EndTime) |
| 79 | + return false; |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments