Skip to content

Commit

Permalink
IsValidPersianDate added to DateTimeUtility and IsDigitOnly added to …
Browse files Browse the repository at this point in the history
…StringUtility
  • Loading branch information
vahid committed Sep 12, 2023
1 parent be7861c commit eec76fd
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
31 changes: 30 additions & 1 deletion 01-Core/Jinget.Core/Utilities/DateTimeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,40 @@ public static string ToSolarDate(DateTime gregorianDate)
/// <summary>
/// Converts given Solar date to its Gregorian equalivant
/// </summary>
public static DateTime ToGregorianDate(string persianDate) => DateTime.Parse(persianDate, new CultureInfo("fa-IR"));
public static DateTime ToGregorianDate(string persianDate)
{
if (persianDate.Length == 8 && !persianDate.Contains("/"))
persianDate = $"{persianDate[..4]}/{persianDate.Substring(4, 2)}/{persianDate.Substring(6, 2)}";
return DateTime.Parse(persianDate, new CultureInfo("fa-IR"));
}

/// <summary>
/// convert minute to hour and minute representation. for example 150 minutes is equal to 2 hours and 30 minutes
/// </summary>
public static TimeSpan ParseToTime(int minute) => TimeSpan.FromMinutes(minute);

/// <summary>
/// check if given persian date is a valid date or not.
/// <paramref name="persianDate"/> should have exactly 8 numerical characters
/// and it should be also greater than the zero
/// You can check the date validity using minimum acceptable date nad maximum accptable date ranges
/// </summary>
public static bool IsValidPersianDate(string persianDate, string minAcceptableDate = "", string maxAcceptableDate = "")
{
if (!StringUtility.IsDigitOnly(persianDate) || Convert.ToInt32(persianDate) < 0 || persianDate.Length != 8)
return false;

DateTime givenDate = ToGregorianDate(persianDate);
DateTime? minDate = null;
DateTime? maxDate = null;
if (minAcceptableDate != "")
minDate = ToGregorianDate(minAcceptableDate);
if (maxAcceptableDate != "")
maxDate = ToGregorianDate(maxAcceptableDate);
if ((minDate == null || givenDate >= minDate) && (maxDate == null || givenDate <= maxDate))
return true;

else return false;
}
}
}
14 changes: 14 additions & 0 deletions 01-Core/Jinget.Core/Utilities/StringUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@ public static string GetRandomString(int length, IEnumerable<char> characterSet)
}
return new string(result);
}

/// <summary>
/// check if given string contains only numeric characters
/// </summary>
public static bool IsDigitOnly(string input)
{
foreach (char c in input)
{
if (c < '0' || c > '9')
return false;
}

return true;
}
}
}
56 changes: 55 additions & 1 deletion Tests/Jinget.Core.Tests/Utilities/DateTimeUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jinget.Core.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Jinget.Core.Utilities.Tests
Expand Down Expand Up @@ -41,5 +42,58 @@ public void should_return_timespan()

Assert.AreEqual(expectedResult, result);
}

[TestMethod()]
public void should_return_true_for_valid_persian_date()
{
string input = "14010101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input));

input = "14100101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input));

input = "00010101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input));
}

[TestMethod()]
public void should_return_true_for_valid_persian_date_in_given_range()
{
string input = "14010101";
string minDate = "13500101";
string maxDate = "14030101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input, minDate, maxDate));

input = "14010101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input, minDate));

input = "14010101";
Assert.IsTrue(DateTimeUtility.IsValidPersianDate(input, maxAcceptableDate: maxDate));
}

[TestMethod()]
public void should_return_false_for_invalid_persian_date()
{
string input = "1401011";
Assert.IsFalse(DateTimeUtility.IsValidPersianDate(input));

input = "-14020101";
Assert.IsFalse(DateTimeUtility.IsValidPersianDate(input));
}

[TestMethod()]
public void should_return_false_for_persian_date_outside_given_range()
{
string input = "13820101";
string minDate = "13900101";
string maxDate = "14030101";
Assert.IsFalse(DateTimeUtility.IsValidPersianDate(input, minDate, maxDate));

input = "13891201";
Assert.IsFalse(DateTimeUtility.IsValidPersianDate(input, minDate));

input = "14040101";
Assert.IsFalse(DateTimeUtility.IsValidPersianDate(input, maxAcceptableDate: maxDate));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Jinget.Core.Tests/Utilities/StringUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,19 @@ public void should_create_random_string_using_given_characterset()
Assert.IsTrue(result.Length == length);
Assert.IsTrue(result.ToCharArray().All(x => charset.Contains(x)));
}

[TestMethod()]
public void should_return_true_for_numeric_string()
{
string input = "1234567890";
Assert.IsTrue(StringUtility.IsDigitOnly(input));
}

[TestMethod()]
public void should_return_false_for_non_numeric_string()
{
string input = "vahid123";
Assert.IsFalse(StringUtility.IsDigitOnly(input));
}
}
}

0 comments on commit eec76fd

Please sign in to comment.