Skip to content

Commit

Permalink
Mostly moving stuff around and adding additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Jan 22, 2023
1 parent 433548f commit cb331bb
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 97 deletions.
31 changes: 31 additions & 0 deletions src/Skybrud.Essentials/Time/TimeUtils.DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ public static DateTime GetEndOfMonth(DateTime time) {

#endregion

#region Get ... of quarter

/// <summary>
/// Returns the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">An instance of <see cref="DateTime"/> representing the timestamp.</param>
/// <returns>An <see cref="int"/> representing the quarter.</returns>
public static int GetQuarter(DateTime timestamp) {
return (int) Math.Ceiling(timestamp.Month / 3d);
}

/// <summary>
/// Returns a new <see cref="DateTime"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTime"/> representing the start of the quarter.</returns>
public static DateTime GetStartOfQuarter(DateTime timestamp) {
return GetStartOfQuarter(timestamp.Year, GetQuarter(timestamp)).DateTime;
}

/// <summary>
/// Returns a new <see cref="DateTime"/> representing the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTime GetEndOfQuarter(DateTime timestamp) {
return GetEndOfQuarter(timestamp.Year, GetQuarter(timestamp)).DateTime;
}

#endregion

#region Is...

/// <summary>
Expand Down
78 changes: 78 additions & 0 deletions src/Skybrud.Essentials/Time/TimeUtils.DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,84 @@ public static DateTimeOffset GetEndOfMonth(DateTimeOffset time, TimeZoneInfo? ti

#endregion

#region Get ... of quarter

/// <summary>
/// Returns the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">An instance of <see cref="DateTimeOffset"/> representing the timestamp.</param>
/// <returns>An <see cref="int"/> representing the quarter.</returns>
public static int GetQuarter(DateTimeOffset timestamp) {
return (int) Math.Ceiling(timestamp.Month / 3d);
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTimeOffset GetStartOfQuarter(DateTimeOffset timestamp) {
return new DateTimeOffset(timestamp.Year, 3 * GetQuarter(timestamp) - 2, 1, 0, 0, 0, timestamp.Offset);
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="timeZone">The time zone for which the reuslt should be adjusted.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTimeOffset GetStartOfQuarter(DateTimeOffset timestamp, TimeZoneInfo? timeZone) {

// Get the start of the quarter
var start = GetStartOfQuarter(timestamp);

// Adjust to the specified time zone
start = timeZone == null ? start : TimeZoneInfo.ConvertTime(start, timeZone);

// Adjust for dayligt savings
if (start.Hour == 23) start = start.AddHours(+1);
if (start.Hour == 01) start = start.AddHours(-1);

return start;

}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the end of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the end of the quarter.</returns>
public static DateTimeOffset GetEndOfQuarter(DateTimeOffset timestamp) {
return GetEndOfQuarter(timestamp.Year, GetQuarter(timestamp));
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the end of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="timeZone">The time zone for which the reuslt should be adjusted.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the end of the quarter.</returns>
public static DateTimeOffset GetEndOfQuarter(DateTimeOffset timestamp, TimeZoneInfo? timeZone) {

// Get the end of the quarter
var end = GetEndOfQuarter(timestamp);

// Adjust to the specified time zone
end = timeZone == null ? end : TimeZoneInfo.ConvertTime(end, timeZone);

// Adjust for dayligt savings
switch (end.Hour) {
case 22: end = end.AddHours(+1); break;
case 00: end = end.AddHours(-1); break;
case 01: end = end.AddHours(-2); break;
}

return end;

}

#endregion

#region Is...

/// <summary>
Expand Down
136 changes: 42 additions & 94 deletions src/Skybrud.Essentials/Time/TimeUtils.Quarters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,53 @@ namespace Skybrud.Essentials.Time {

public static partial class TimeUtils {

/// <summary>
/// Returns the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">An instance of <see cref="DateTime"/> representing the timestamp.</param>
/// <returns>An <see cref="int"/> representing the quarter.</returns>
public static int GetQuarter(DateTime timestamp) {
return (int) Math.Ceiling(timestamp.Month / 3d);
internal static int GetStartMonthOfQuarter(int quarter) {
return 3 * quarter - 2;
}

internal static int GetStartMonthOfQuarter(DateTime date) {
return GetStartMonthOfQuarter(GetQuarter(date));
}

internal static int GetStartMonthOfQuarter(DateTimeOffset date) {
return GetStartMonthOfQuarter(GetQuarter(date));
}

internal static int GetStartMonthOfQuarter(EssentialsDate date) {
return GetStartMonthOfQuarter(GetQuarter(date));
}

internal static int GetStartMonthOfQuarter(EssentialsTime date) {
return GetStartMonthOfQuarter(GetQuarter(date));
}

internal static int GetEndMonthOfQuarter(int quarter) {
return 3 * quarter + 1;
}

internal static int GetEndMonthOfQuarter(DateTime date) {
return GetEndMonthOfQuarter(GetQuarter(date));
}

internal static int GetEndMonthOfQuarter(DateTimeOffset date) {
return GetEndMonthOfQuarter(GetQuarter(date));
}

internal static int GetEndMonthOfQuarter(EssentialsDate date) {
return GetEndMonthOfQuarter(GetQuarter(date));
}

internal static int GetEndMonthOfQuarter(EssentialsTime date) {
return GetEndMonthOfQuarter(GetQuarter(date));
}

/// <summary>
/// Returns the quarter of the specified <paramref name="timestamp"/>.
/// Returns the quarter of the specified <paramref name="date"/>.
/// </summary>
/// <param name="timestamp">An instance of <see cref="DateTimeOffset"/> representing the timestamp.</param>
/// <param name="date">An instance of <see cref="EssentialsTime"/> representing the date.</param>
/// <returns>An <see cref="int"/> representing the quarter.</returns>
public static int GetQuarter(DateTimeOffset timestamp) {
return (int) Math.Ceiling(timestamp.Month / 3d);
public static int GetQuarter(EssentialsDate date) {
return (int) Math.Ceiling(date.Month / 3d);
}

/// <summary>
Expand Down Expand Up @@ -54,46 +85,6 @@ public static DateTimeOffset GetStartOfQuarter(int year, int quarter, TimeSpan o
return new DateTimeOffset(year, 3 * quarter - 2, 1, 0, 0, 0, offset);
}

/// <summary>
/// Returns a new <see cref="DateTime"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTime"/> representing the start of the quarter.</returns>
public static DateTime GetStartOfQuarter(DateTime timestamp) {
return GetStartOfQuarter(timestamp.Year, GetQuarter(timestamp)).DateTime;
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTimeOffset GetStartOfQuarter(DateTimeOffset timestamp) {
return new DateTimeOffset(timestamp.Year, 3 * GetQuarter(timestamp) - 2, 1, 0, 0, 0, timestamp.Offset);
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="timeZone">The time zone for which the reuslt should be adjusted.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTimeOffset GetStartOfQuarter(DateTimeOffset timestamp, TimeZoneInfo? timeZone) {

// Get the start of the quarter
var start = GetStartOfQuarter(timestamp);

// Adjust to the specified time zone
start = timeZone == null ? start : TimeZoneInfo.ConvertTime(start, timeZone);

// Adjust for dayligt savings
if (start.Hour == 23) start = start.AddHours(+1);
if (start.Hour == 01) start = start.AddHours(-1);

return start;

}

/// <summary>
/// Returns a new <see cref="EssentialsTime"/> representing the start of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
Expand Down Expand Up @@ -133,49 +124,6 @@ public static DateTimeOffset GetEndOfQuarter(int year, int quarter, TimeSpan off

}

/// <summary>
/// Returns a new <see cref="DateTime"/> representing the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the start of the quarter.</returns>
public static DateTime GetEndOfQuarter(DateTime timestamp) {
return GetEndOfQuarter(timestamp.Year, GetQuarter(timestamp)).DateTime;
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the end of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the end of the quarter.</returns>
public static DateTimeOffset GetEndOfQuarter(DateTimeOffset timestamp) {
return GetEndOfQuarter(timestamp.Year, GetQuarter(timestamp));
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> representing the end of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="timeZone">The time zone for which the reuslt should be adjusted.</param>
/// <returns>An instance of <see cref="DateTimeOffset"/> representing the end of the quarter.</returns>
public static DateTimeOffset GetEndOfQuarter(DateTimeOffset timestamp, TimeZoneInfo? timeZone) {

// Get the end of the quarter
var end = GetEndOfQuarter(timestamp);

// Adjust to the specified time zone
end = timeZone == null ? end : TimeZoneInfo.ConvertTime(end, timeZone);

// Adjust for dayligt savings
switch (end.Hour) {
case 22: end = end.AddHours(+1); break;
case 00: end = end.AddHours(-1); break;
case 01: end = end.AddHours(-2); break;
}

return end;

}

/// <summary>
/// Returns a new <see cref="EssentialsTime"/> representing the end of the quarter of the specified <paramref name="timestamp"/>.
/// </summary>
Expand Down
Loading

0 comments on commit cb331bb

Please sign in to comment.