Skip to content

Commit

Permalink
style: namespace without brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikostiha committed Oct 17, 2024
1 parent 64c02a4 commit 7bc1e81
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 165 deletions.
229 changes: 114 additions & 115 deletions src/Atin/AtinParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,135 @@
using System;
using System.Globalization;

namespace Atin
namespace Atin;

/// <summary>
/// A static class that provides methods to parse time span input strings.
/// </summary>
public static class AtinParser
{
/// <summary>
/// A static class that provides methods to parse time span input strings.
/// Regular expression pattern used to match time units (W, D, H, M, S) and corresponding quantities.
/// </summary>
private const string _regexPattern = @"([WwDdHhMmSs])(\d+)";

/// <summary>
/// Compiled regular expression based on the <see cref="_regexPattern"/>.
/// </summary>
private static readonly Regex _regex = new(_regexPattern, RegexOptions.Compiled);

/// <summary>
/// Parses a time string and returns the equivalent <see cref="TimeSpan"/> object.
/// </summary>
public static class AtinParser
/// <param name="input">A string representing time intervals (e.g., "2W3D4H").</param>
/// <returns>A <see cref="TimeSpan"/> representing the total time.</returns>
/// <exception cref="ArgumentNullException">Thrown when the input is null.</exception>
/// <exception cref="ArgumentException">Thrown when the input format is invalid or the last character is not a digit.</exception>
public static TimeSpan Parse(string input)
{
/// <summary>
/// Regular expression pattern used to match time units (W, D, H, M, S) and corresponding quantities.
/// </summary>
private const string _regexPattern = @"([WwDdHhMmSs])(\d+)";

/// <summary>
/// Compiled regular expression based on the <see cref="_regexPattern"/>.
/// </summary>
private static readonly Regex _regex = new(_regexPattern, RegexOptions.Compiled);

/// <summary>
/// Parses a time string and returns the equivalent <see cref="TimeSpan"/> object.
/// </summary>
/// <param name="input">A string representing time intervals (e.g., "2W3D4H").</param>
/// <returns>A <see cref="TimeSpan"/> representing the total time.</returns>
/// <exception cref="ArgumentNullException">Thrown when the input is null.</exception>
/// <exception cref="ArgumentException">Thrown when the input format is invalid or the last character is not a digit.</exception>
public static TimeSpan Parse(string input)
if (input is null)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
throw new ArgumentNullException(nameof(input));
}

if (input == string.Empty)
{
return TimeSpan.Zero;
}
if (input == string.Empty)
{
return TimeSpan.Zero;
}

if (!char.IsDigit(input[input.Length - 1]))
{
throw new ArgumentException("The last character of the input must be a digit.");
}

var matches = _regex.Matches(input);

if (matches.Count == 0)
{
throw new ArgumentException("Input format is invalid.");
}

int totalSeconds = 0;

if (!char.IsDigit(input[input.Length - 1]))
foreach (Match match in matches)
{
string unit = match.Groups[1].Value;
int quantity = int.Parse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);

switch (unit)
{
throw new ArgumentException("The last character of the input must be a digit.");
case "W":
totalSeconds += quantity * 604800; // Convert weeks to seconds (7 days)
break;
case "D":
totalSeconds += quantity * 86400; // Convert days to seconds (24 hours)
break;
case "H":
totalSeconds += quantity * 3600; // Convert hours to seconds (60 minutes)
break;
case "M":
totalSeconds += quantity * 60; // Convert minutes to seconds
break;
case "S":
totalSeconds += quantity; // Seconds
break;
default:
throw new ArgumentException($"Invalid time unit: {unit}");
}
}

return TimeSpan.FromSeconds(totalSeconds);
}

/// <summary>
/// Tries to parse the time string and returns a boolean indicating success or failure.
/// </summary>
/// <param name="input">A string representing time intervals (e.g., "2W3D4H").</param>
/// <param name="result">The output <see cref="TimeSpan"/> if parsing succeeds.</param>
/// <returns><c>true</c> if the input string was successfully parsed; otherwise, <c>false</c>.</returns>
public static bool TryParse(string input, out TimeSpan result)
{
if (input is null)
{
result = TimeSpan.Zero;
return false;
}

if (input == string.Empty)
{
result = TimeSpan.Zero;
return true;
}

if (!char.IsDigit(input[input.Length - 1]))
{
result = TimeSpan.Zero;
return false;
}

result = TimeSpan.Zero;

try
{
var matches = _regex.Matches(input);

if (matches.Count == 0)
{
throw new ArgumentException("Input format is invalid.");
return false;
}

int totalSeconds = 0;

foreach (Match match in matches)
{
string unit = match.Groups[1].Value;
int quantity = int.Parse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
int quantity;

if (!int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out quantity))
{
result = TimeSpan.Zero;
return false;
}

switch (unit)
{
Expand All @@ -75,94 +150,18 @@ public static TimeSpan Parse(string input)
totalSeconds += quantity; // Seconds
break;
default:
throw new ArgumentException($"Invalid time unit: {unit}");
result = TimeSpan.Zero;
return false; // Invalid time unit
}
}

return TimeSpan.FromSeconds(totalSeconds);
result = TimeSpan.FromSeconds(totalSeconds);
return true;
}

/// <summary>
/// Tries to parse the time string and returns a boolean indicating success or failure.
/// </summary>
/// <param name="input">A string representing time intervals (e.g., "2W3D4H").</param>
/// <param name="result">The output <see cref="TimeSpan"/> if parsing succeeds.</param>
/// <returns><c>true</c> if the input string was successfully parsed; otherwise, <c>false</c>.</returns>
public static bool TryParse(string input, out TimeSpan result)
catch
{
if (input is null)
{
result = TimeSpan.Zero;
return false;
}

if (input == string.Empty)
{
result = TimeSpan.Zero;
return true;
}

if (!char.IsDigit(input[input.Length - 1]))
{
result = TimeSpan.Zero;
return false;
}

result = TimeSpan.Zero;

try
{
var matches = _regex.Matches(input);

if (matches.Count == 0)
{
return false;
}

int totalSeconds = 0;

foreach (Match match in matches)
{
string unit = match.Groups[1].Value;
int quantity;

if (!int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out quantity))
{
result = TimeSpan.Zero;
return false;
}

switch (unit)
{
case "W":
totalSeconds += quantity * 604800; // Convert weeks to seconds (7 days)
break;
case "D":
totalSeconds += quantity * 86400; // Convert days to seconds (24 hours)
break;
case "H":
totalSeconds += quantity * 3600; // Convert hours to seconds (60 minutes)
break;
case "M":
totalSeconds += quantity * 60; // Convert minutes to seconds
break;
case "S":
totalSeconds += quantity; // Seconds
break;
default:
result = TimeSpan.Zero;
return false; // Invalid time unit
}
}

result = TimeSpan.FromSeconds(totalSeconds);
return true;
}
catch
{
result = TimeSpan.Zero;
return false;
}
return false;
}
}
}
99 changes: 49 additions & 50 deletions src/Atin/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,69 @@
using System;
using System.Text;

namespace Atin
namespace Atin;

/// <summary>
/// Provides extension methods for the <see cref="TimeSpan"/> class.
/// </summary>
public static class TimeSpanExtensions
{
/// <summary>
/// Provides extension methods for the <see cref="TimeSpan"/> class.
/// Converts a <see cref="TimeSpan"/> object to a custom formatted string in the "Atin" format.
/// The format includes weeks (W), days (D), hours (H), minutes (M), and seconds (S).
/// </summary>
public static class TimeSpanExtensions
/// <param name="timeSpan">The <see cref="TimeSpan"/> instance to convert.</param>
/// <returns>A string representing the time span in the "Atin" format (e.g., "W2D3H4M5S").</returns>
public static string ToAtin(this TimeSpan timeSpan)
{
/// <summary>
/// Converts a <see cref="TimeSpan"/> object to a custom formatted string in the "Atin" format.
/// The format includes weeks (W), days (D), hours (H), minutes (M), and seconds (S).
/// </summary>
/// <param name="timeSpan">The <see cref="TimeSpan"/> instance to convert.</param>
/// <returns>A string representing the time span in the "Atin" format (e.g., "W2D3H4M5S").</returns>
public static string ToAtin(this TimeSpan timeSpan)
{
int totalSeconds = (int)timeSpan.TotalSeconds;
int totalSeconds = (int)timeSpan.TotalSeconds;

// Calculate weeks, days, hours, minutes, and seconds from the total seconds.
int weeks = totalSeconds / 604800; // 604800 seconds in a week
totalSeconds %= 604800;
// Calculate weeks, days, hours, minutes, and seconds from the total seconds.
int weeks = totalSeconds / 604800; // 604800 seconds in a week
totalSeconds %= 604800;

int days = totalSeconds / 86400; // 86400 seconds in a day
totalSeconds %= 86400;
int days = totalSeconds / 86400; // 86400 seconds in a day
totalSeconds %= 86400;

int hours = totalSeconds / 3600; // 3600 seconds in an hour
totalSeconds %= 3600;
int hours = totalSeconds / 3600; // 3600 seconds in an hour
totalSeconds %= 3600;

int minutes = totalSeconds / 60; // 60 seconds in a minute
int seconds = totalSeconds % 60; // remaining seconds
int minutes = totalSeconds / 60; // 60 seconds in a minute
int seconds = totalSeconds % 60; // remaining seconds

// Use StringBuilder to construct the output string.
var result = new StringBuilder();
// Use StringBuilder to construct the output string.
var result = new StringBuilder();

// Append weeks, if any.
if (weeks > 0)
{
result.Append($"W{weeks}");
}

// Append days, if any.
if (days > 0)
{
result.Append($"D{days}");
}
// Append weeks, if any.
if (weeks > 0)
{
result.Append($"W{weeks}");
}

// Append hours, if any.
if (hours > 0)
{
result.Append($"H{hours}");
}
// Append days, if any.
if (days > 0)
{
result.Append($"D{days}");
}

// Append minutes, if any.
if (minutes > 0)
{
result.Append($"M{minutes}");
}
// Append hours, if any.
if (hours > 0)
{
result.Append($"H{hours}");
}

// Append seconds or default to seconds if no other units were added.
if (seconds > 0 || result.Length == 0) // Always include seconds if no other units were added.
{
result.Append($"S{seconds}");
}
// Append minutes, if any.
if (minutes > 0)
{
result.Append($"M{minutes}");
}

return result.ToString();
// Append seconds or default to seconds if no other units were added.
if (seconds > 0 || result.Length == 0) // Always include seconds if no other units were added.
{
result.Append($"S{seconds}");
}

return result.ToString();
}
}

0 comments on commit 7bc1e81

Please sign in to comment.