From ed77f7a89d25676e615947269713d18c93209840 Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 01:54:00 +0330 Subject: [PATCH 1/5] Refactor and document Persian date handling Updated `using` directive to include `System.Globalization`. Reformatted `namespace` declaration. Added comprehensive XML documentation to `PersianDateShamsi` class and its methods. Refactored methods to handle `DateTimeOffset` and `DateTime` inputs separately. Added new methods for Shamsi date handling. Improved readability and maintainability of `ToShamsi.cs`, `DateTest.cs`, and `ToShamsiTest.cs` files with reorganized code and added XML comments. Enhanced test coverage for `DateTimeOffset` and null values. --- PersianDate/PersianDateShamsi.cs | 516 +++++++++++++++++++++++-------- PersianDate/ToShamsi.cs | 151 +++++++-- PersianDateTests/DateTest.cs | 379 ++++++++++++++++++----- PersianDateTests/ToShamsiTest.cs | 169 ++++++++-- 4 files changed, 952 insertions(+), 263 deletions(-) diff --git a/PersianDate/PersianDateShamsi.cs b/PersianDate/PersianDateShamsi.cs index 90b42f8..1d960af 100644 --- a/PersianDate/PersianDateShamsi.cs +++ b/PersianDate/PersianDateShamsi.cs @@ -1,151 +1,425 @@ -using System; +using System; using System.Globalization; -namespace PersianDate +namespace PersianDate; + +/// +/// Provides methods to convert Gregorian dates to Persian (Shamsi) dates. +/// +/// +/// var persianDate = new PersianDateShamsi(); +/// var shamsiYear = persianDate.GetShamsiYear(new DateTime(2023, 3, 21)); // 1402 +/// var shortShamsiYear = persianDate.GetShortShamsiYear(new DateTime(2023, 3, 21)); // "02" +/// var shamsiMonth = persianDate.GetShamsiMonth(new DateTime(2023, 3, 21)); // 1 +/// var shamsiDay = persianDate.GetShamsiDay(new DateTime(2023, 3, 21)); // 1 +/// +/// +/// +public class PersianDateShamsi { + private readonly PersianCalendar persianCalendar; + /// - /// Provides methods to convert Gregorian dates to Persian (Shamsi) dates. + /// Initializes a new instance of the class. /// - public class PersianDateShamsi + public PersianDateShamsi() { - private readonly PersianCalendar persianCalendar; + persianCalendar = new PersianCalendar(); + } - /// - /// Initializes a new instance of the class. - /// - public PersianDateShamsi() + /// + /// Gets the Shamsi year from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiYear = persianDate.GetShamsiYear(new DateTime(2023, 3, 21)); // Returns 1402 + /// int? nullYear = persianDate.GetShamsiYear(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi year, or null if the input is null or out of range. + public int? GetShamsiYear(DateTime? dateTime) + { + if (!dateTime.HasValue) { - persianCalendar = new PersianCalendar(); + return null; } - /// - /// Gets the Shamsi year from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi year, or null if the input is null or out of range. - public int? GetShamsiYear(DateTime? dateTime) + try { - if (!dateTime.HasValue) - return null; - - try - { - return persianCalendar.GetYear(dateTime.Value); - } - catch (ArgumentOutOfRangeException) - { - return null; - } + return persianCalendar.GetYear(dateTime.Value); } - - /// - /// Gets the short Shamsi year from the specified Gregorian date as a string. - /// - /// The Gregorian date. - /// The short Shamsi year as a string, or null if the input is null. - public string? GetShortShamsiYear(DateTime? dateTime) + catch (ArgumentOutOfRangeException) { - return dateTime?.ToString("yy", CultureInfo.CreateSpecificCulture("fa")); + return null; } + } - /// - /// Gets the Shamsi year from the specified Gregorian date as a string. - /// - /// The Gregorian date. - /// The Shamsi year as a string, or null if the input is null or out of range. - public string? GetShamsiYearToString(DateTime? dateTime) - { - if (!dateTime.HasValue) - return null; - - try - { - return persianCalendar.GetYear(dateTime.Value).ToString(); - } - catch (ArgumentOutOfRangeException) - { - return null; - } - } + /// + /// Gets the Shamsi year from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiYear = persianDate.GetShamsiYear(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns 1402 + /// int? nullYear = persianDate.GetShamsiYear(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi year, or null if the input is null or out of range. + public int? GetShamsiYear(DateTimeOffset? dateTimeOffset) + { + return GetShamsiYear(dateTimeOffset?.DateTime); + } - /// - /// Gets the Shamsi month from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi month, or null if the input is null. - public int? GetShamsiMonth(DateTime? dateTime) - { - return dateTime.HasValue ? persianCalendar.GetMonth(dateTime.Value) : null; - } + /// + /// Gets the short Shamsi year from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shortShamsiYear = persianDate.GetShortShamsiYear(new DateTime(2023, 3, 21)); // Returns "02" + /// string? nullYear = persianDate.GetShortShamsiYear(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The short Shamsi year as a string, or null if the input is null. + public string? GetShortShamsiYear(DateTime? dateTime) + { + return dateTime?.ToString("yy", CultureInfo.CreateSpecificCulture("fa")); + } - /// - /// Gets the Shamsi month number from the specified Gregorian date as a string. - /// - /// The Gregorian date. - /// The Shamsi month number as a string. - public string GetShamsiMonthString(DateTime dateTime) - { - return persianCalendar.GetMonth(dateTime).ToString("00"); - } + /// + /// Gets the short Shamsi year from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shortShamsiYear = persianDate.GetShortShamsiYear(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "02" + /// string? nullYear = persianDate.GetShortShamsiYear(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The short Shamsi year as a string, or null if the input is null. + public string? GetShortShamsiYear(DateTimeOffset? dateTimeOffset) + { + return GetShortShamsiYear(dateTimeOffset?.DateTime); + } - /// - /// Gets the Shamsi month number from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi month number, or null if the input is null. - public int? GetShamsiMonthNumber(DateTime? dateTime) + /// + /// Gets the Shamsi year from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiYearString = persianDate.GetShamsiYearToString(new DateTime(2023, 3, 21)); // Returns "1402" + /// string? nullYear = persianDate.GetShamsiYearToString(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi year as a string, or null if the input is null or out of range. + public string? GetShamsiYearToString(DateTime? dateTime) + { + if (!dateTime.HasValue) { - return dateTime.HasValue ? persianCalendar.GetMonth(dateTime.Value) : null; + return null; } - /// - /// Gets the Shamsi month name from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi month name, or null if the input is null. - public string? GetShamsiMonthName(DateTime? dateTime) + try { - return dateTime?.ToString("MMMM", CultureInfo.CreateSpecificCulture("fa")); + return persianCalendar.GetYear(dateTime.Value).ToString(); } - - /// - /// Gets the Shamsi day from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi day, or null if the input is null. - public int? GetShamsiDay(DateTime? dateTime) + catch (ArgumentOutOfRangeException) { - return dateTime.HasValue ? persianCalendar.GetDayOfMonth(dateTime.Value) : null; + return null; } + } - /// - /// Gets the Shamsi day from the specified Gregorian date as a string. - /// - /// The Gregorian date. - /// The Shamsi day as a string, or null if the input is null. - public string? GetShamsiDayString(DateTime? dateTime) - { - return dateTime.HasValue ? persianCalendar.GetDayOfMonth(dateTime.Value).ToString("00") : null; - } + /// + /// Gets the Shamsi year from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiYearString = persianDate.GetShamsiYearToString(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "1402" + /// string? nullYear = persianDate.GetShamsiYearToString(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi year as a string, or null if the input is null or out of range. + public string? GetShamsiYearToString(DateTimeOffset? dateTimeOffset) + { + return GetShamsiYearToString(dateTimeOffset?.DateTime); + } - /// - /// Gets the Shamsi day name from the specified Gregorian date. - /// - /// The Gregorian date. - /// The Shamsi day name, or null if the input is null. - public string? GetShamsiDayName(DateTime? dateTime) - { - return dateTime?.ToString("dddd", CultureInfo.CreateSpecificCulture("fa")); - } + /// + /// Gets the Shamsi month from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiMonth = persianDate.GetShamsiMonth(new DateTime(2023, 3, 21)); // Returns 1 + /// int? nullMonth = persianDate.GetShamsiMonth(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month, or null if the input is null. + public int? GetShamsiMonth(DateTime? dateTime) + { + return dateTime.HasValue ? persianCalendar.GetMonth(dateTime.Value) : null; + } - /// - /// Gets the short Shamsi day name from the specified Gregorian date. - /// - /// The Gregorian date. - /// The short Shamsi day name, or null if the input is null. - public string? GetShamsiDayShortName(DateTime? dateTime) - { - return (dateTime?.ToString("dddd", CultureInfo.CreateSpecificCulture("fa")))?[..1]; - } + /// + /// Gets the Shamsi month from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiMonth = persianDate.GetShamsiMonth(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns 1 + /// int? nullMonth = persianDate.GetShamsiMonth(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month, or null if the input is null. + public int? GetShamsiMonth(DateTimeOffset? dateTimeOffset) + { + return GetShamsiMonth(dateTimeOffset?.DateTime); + } + + /// + /// Gets the Shamsi month number from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string shamsiMonthString = persianDate.GetShamsiMonthString(new DateTime(2023, 3, 21)); // Returns "01" + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month number as a string. + public string GetShamsiMonthString(DateTime dateTime) + { + return persianCalendar.GetMonth(dateTime).ToString("00"); + } + + /// + /// Gets the Shamsi month number from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string shamsiMonthString = persianDate.GetShamsiMonthString(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "01" + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month number as a string. + public string GetShamsiMonthString(DateTimeOffset dateTimeOffset) + { + return GetShamsiMonthString(dateTimeOffset.DateTime); + } + + /// + /// Gets the Shamsi month number from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiMonthNumber = persianDate.GetShamsiMonthNumber(new DateTime(2023, 3, 21)); // Returns 1 + /// int? nullMonth = persianDate.GetShamsiMonthNumber(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month number, or null if the input is null. + public int? GetShamsiMonthNumber(DateTime? dateTime) + { + return dateTime.HasValue ? persianCalendar.GetMonth(dateTime.Value) : null; + } + + /// + /// Gets the Shamsi month number from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiMonthNumber = persianDate.GetShamsiMonthNumber(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns 1 + /// int? nullMonth = persianDate.GetShamsiMonthNumber(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month number, or null if the input is null. + public int? GetShamsiMonthNumber(DateTimeOffset? dateTimeOffset) + { + return GetShamsiMonthNumber(dateTimeOffset?.DateTime); + } + + /// + /// Gets the Shamsi month name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiMonthName = persianDate.GetShamsiMonthName(new DateTime(2023, 3, 21)); // Returns "فروردین" + /// string? nullMonth = persianDate.GetShamsiMonthName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month name, or null if the input is null. + public string? GetShamsiMonthName(DateTime? dateTime) + { + return dateTime?.ToString("MMMM", CultureInfo.CreateSpecificCulture("fa")); + } + + /// + /// Gets the Shamsi month name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiMonthName = persianDate.GetShamsiMonthName(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "فروردین" + /// string? nullMonth = persianDate.GetShamsiMonthName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi month name, or null if the input is null. + public string? GetShamsiMonthName(DateTimeOffset? dateTimeOffset) + { + return GetShamsiMonthName(dateTimeOffset?.DateTime); + } + + /// + /// Gets the Shamsi day from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiDay = persianDate.GetShamsiDay(new DateTime(2023, 3, 21)); // Returns 1 + /// int? nullDay = persianDate.GetShamsiDay(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day, or null if the input is null. + public int? GetShamsiDay(DateTime? dateTime) + { + return dateTime.HasValue ? persianCalendar.GetDayOfMonth(dateTime.Value) : null; + } + + /// + /// Gets the Shamsi day from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// int? shamsiDay = persianDate.GetShamsiDay(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns 1 + /// int? nullDay = persianDate.GetShamsiDay(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day, or null if the input is null. + public int? GetShamsiDay(DateTimeOffset? dateTimeOffset) + { + return GetShamsiDay(dateTimeOffset?.DateTime); + } + + /// + /// Gets the Shamsi day from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiDayString = persianDate.GetShamsiDayString(new DateTime(2023, 3, 21)); // Returns "01" + /// string? nullDay = persianDate.GetShamsiDayString(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day as a string, or null if the input is null. + public string? GetShamsiDayString(DateTime? dateTime) + { + return dateTime.HasValue ? persianCalendar.GetDayOfMonth(dateTime.Value).ToString("00") : null; + } + + /// + /// Gets the Shamsi day from the specified Gregorian date as a string. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiDayString = persianDate.GetShamsiDayString(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "01" + /// string? nullDay = persianDate.GetShamsiDayString(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day as a string, or null if the input is null. + public string? GetShamsiDayString(DateTimeOffset? dateTimeOffset) + { + return GetShamsiDayString(dateTimeOffset?.DateTime); + } + + /// + /// Gets the Shamsi day name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiDayName = persianDate.GetShamsiDayName(new DateTime(2023, 3, 21)); // Returns "سه‌شنبه" + /// string? nullDay = persianDate.GetShamsiDayName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day name, or null if the input is null. + public string? GetShamsiDayName(DateTime? dateTime) + { + return dateTime?.ToString("dddd", CultureInfo.CreateSpecificCulture("fa")); + } + + /// + /// Gets the Shamsi day name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shamsiDayName = persianDate.GetShamsiDayName(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "سه‌شنبه" + /// string? nullDay = persianDate.GetShamsiDayName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The Shamsi day name, or null if the input is null. + public string? GetShamsiDayName(DateTimeOffset? dateTimeOffset) + { + return GetShamsiDayName(dateTimeOffset?.DateTime); + } + + /// + /// Gets the short Shamsi day name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shortShamsiDayName = persianDate.GetShamsiDayShortName(new DateTime(2023, 3, 21)); // Returns "س" + /// string? nullDay = persianDate.GetShamsiDayShortName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The short Shamsi day name, or null if the input is null. + public string? GetShamsiDayShortName(DateTime? dateTime) + { + return (dateTime?.ToString("dddd", CultureInfo.CreateSpecificCulture("fa")))?[..1]; + } + + /// + /// Gets the short Shamsi day name from the specified Gregorian date. + /// + /// + /// var persianDate = new PersianDateShamsi(); + /// string? shortShamsiDayName = persianDate.GetShamsiDayShortName(new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero)); // Returns "س" + /// string? nullDay = persianDate.GetShamsiDayShortName(null); // Returns null + /// + /// + /// + /// The Gregorian date. + /// The short Shamsi day name, or null if the input is null. + public string? GetShamsiDayShortName(DateTimeOffset? dateTimeOffset) + { + return GetShamsiDayShortName(dateTimeOffset?.DateTime); } } diff --git a/PersianDate/ToShamsi.cs b/PersianDate/ToShamsi.cs index 7c2d577..6f91db6 100644 --- a/PersianDate/ToShamsi.cs +++ b/PersianDate/ToShamsi.cs @@ -1,52 +1,135 @@ -using System; +using System; -namespace PersianDate +namespace PersianDate; + +/// +/// Provides extension methods to convert Gregorian dates to Persian (Shamsi) dates. +/// +public static class ToShamsi { /// - /// Provides extension methods to convert Gregorian dates to Persian (Shamsi) dates. + /// Get Shamsi Date From Miladi Year + /// Example: + /// + /// DateTime? dateTime = new DateTime(2023, 10, 5); + /// string? shamsiDate = dateTime.ToShamsiDate(); // "1402/07/13" + /// /// - public static class ToShamsi + /// Enter The Jalali DateTime + /// A string representing the Shamsi date or null if the input is null. + public static string? ToShamsiDate(this DateTime? dateTime) { - /// - /// Get Shamsi Date From Miladi Year - /// - /// Enter The Jalali DateTime - /// A string representing the Shamsi date or null if the input is null. - public static string? ToShamsiDate(this DateTime? dateTime) + if (!dateTime.HasValue) { - if (!dateTime.HasValue) - return null; + return null; + } - var persianDateShamsi = new PersianDateShamsi(); - return persianDateShamsi.GetShamsiYearToString(dateTime) + "/" + persianDateShamsi.GetShamsiMonthString(dateTime.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTime.Value); + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShamsiYearToString(dateTime) + "/" + persianDateShamsi.GetShamsiMonthString(dateTime.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTime.Value); + } + + /// + /// Get Shamsi Date From Miladi Year + /// Example: + /// + /// DateTimeOffset? dateTimeOffset = new DateTimeOffset(2023, 10, 5, 0, 0, 0, TimeSpan.Zero); + /// string? shamsiDate = dateTimeOffset.ToShamsiDate(); // "1402/07/13" + /// + /// + /// Enter The Jalali DateTimeOffset + /// A string representing the Shamsi date or null if the input is null. + public static string? ToShamsiDate(this DateTimeOffset? dateTimeOffset) + { + if (!dateTimeOffset.HasValue) + { + return null; } - /// - /// Get Short Shamsi Date From Miladi Year - /// - /// Enter The Jalali DateTime - /// A string representing the short Shamsi date or null if the input is null. - public static string? ToShortShamsiDate(this DateTime? dateTime) + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShamsiYearToString(dateTimeOffset) + "/" + persianDateShamsi.GetShamsiMonthString(dateTimeOffset.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTimeOffset.Value); + } + + /// + /// Get Short Shamsi Date From Miladi Year + /// Example: + /// + /// DateTime? dateTime = new DateTime(2023, 10, 5); + /// string? shortShamsiDate = dateTime.ToShortShamsiDate(); // "02/07/13" + /// + /// + /// Enter The Jalali DateTime + /// A string representing the short Shamsi date or null if the input is null. + public static string? ToShortShamsiDate(this DateTime? dateTime) + { + if (!dateTime.HasValue) { - if (!dateTime.HasValue) - return null; + return null; + } + + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShortShamsiYear(dateTime) + "/" + persianDateShamsi.GetShamsiMonthString(dateTime.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTime.Value); + } - var persianDateShamsi = new PersianDateShamsi(); - return persianDateShamsi.GetShortShamsiYear(dateTime) + "/" + persianDateShamsi.GetShamsiMonthString(dateTime.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTime.Value); + /// + /// Get Short Shamsi Date From Miladi Year + /// Example: + /// + /// DateTimeOffset? dateTimeOffset = new DateTimeOffset(2023, 10, 5, 0, 0, 0, TimeSpan.Zero); + /// string? shortShamsiDate = dateTimeOffset.ToShortShamsiDate(); // "02/07/13" + /// + /// + /// Enter The Jalali DateTimeOffset + /// A string representing the short Shamsi date or null if the input is null. + public static string? ToShortShamsiDate(this DateTimeOffset? dateTimeOffset) + { + if (!dateTimeOffset.HasValue) + { + return null; } - /// - /// Get Long Shamsi Date From Miladi Year - /// - /// Enter The Jalali DateTime - /// A string representing the long Shamsi date or null if the input is null. - public static string? ToLongShamsiDate(this DateTime? dateTime) + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShortShamsiYear(dateTimeOffset) + "/" + persianDateShamsi.GetShamsiMonthString(dateTimeOffset.Value) + "/" + persianDateShamsi.GetShamsiDayString(dateTimeOffset.Value); + } + + /// + /// Get Long Shamsi Date From Miladi Year + /// Example: + /// + /// DateTime? dateTime = new DateTime(2023, 10, 5); + /// string? longShamsiDate = dateTime.ToLongShamsiDate(); // "پنجشنبه 13 مهر 1402" + /// + /// + /// Enter The Jalali DateTime + /// A string representing the long Shamsi date or null if the input is null. + public static string? ToLongShamsiDate(this DateTime? dateTime) + { + if (!dateTime.HasValue) { - if (!dateTime.HasValue) - return null; + return null; + } + + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShamsiDayName(dateTime) + " " + persianDateShamsi.GetShamsiDay(dateTime) + " " + persianDateShamsi.GetShamsiMonthName(dateTime) + " " + persianDateShamsi.GetShamsiYear(dateTime); + } - var persianDateShamsi = new PersianDateShamsi(); - return persianDateShamsi.GetShamsiDayName(dateTime) + " " + persianDateShamsi.GetShamsiDay(dateTime) + " " + persianDateShamsi.GetShamsiMonthName(dateTime) + " " + persianDateShamsi.GetShamsiYear(dateTime); + /// + /// Get Long Shamsi Date From Miladi Year + /// Example: + /// + /// DateTimeOffset? dateTimeOffset = new DateTimeOffset(2023, 10, 5, 0, 0, 0, TimeSpan.Zero); + /// string? longShamsiDate = dateTimeOffset.ToLongShamsiDate(); // "پنجشنبه 13 مهر 1402" + /// + /// + /// Enter The Jalali DateTimeOffset + /// A string representing the long Shamsi date or null if the input is null. + public static string? ToLongShamsiDate(this DateTimeOffset? dateTimeOffset) + { + if (!dateTimeOffset.HasValue) + { + return null; } + + PersianDateShamsi persianDateShamsi = new(); + return persianDateShamsi.GetShamsiDayName(dateTimeOffset) + " " + persianDateShamsi.GetShamsiDay(dateTimeOffset) + " " + persianDateShamsi.GetShamsiMonthName(dateTimeOffset) + " " + persianDateShamsi.GetShamsiYear(dateTimeOffset); } } diff --git a/PersianDateTests/DateTest.cs b/PersianDateTests/DateTest.cs index 8b38622..390ba05 100644 --- a/PersianDateTests/DateTest.cs +++ b/PersianDateTests/DateTest.cs @@ -1,81 +1,310 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; using PersianDate; -using System; -namespace PersianDateTests +namespace PersianDateTests; + +[TestClass] +public class DateTest { - [TestClass] - public class DateTest + // My Birthday + private readonly DateTime? dateTime = new DateTime(1998, 1, 11); + private readonly PersianDateShamsi persianDateShamsi = new(); + + /// + /// Tests the GetShamsiYear method with a valid DateTime. + /// + [TestMethod] + public void YearTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + int? result = persianDateShamsi.GetShamsiYear(testDate); + + // Assert + Assert.AreEqual(1376, result); + } + + /// + /// Tests the GetShortShamsiYear method with a valid DateTime. + /// + [TestMethod] + public void ShortYearTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShortShamsiYear(testDate); + + // Assert + Assert.AreEqual("76", result); + } + + /// + /// Tests the GetShamsiYearToString method with a valid DateTime. + /// + [TestMethod] + public void YearStringTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShamsiYearToString(testDate); + + // Assert + Assert.AreEqual("1376", result); + } + + /// + /// Tests the GetShamsiMonth method with a valid DateTime. + /// + [TestMethod] + public void MonthTest() { - //My Birthday - DateTime? dateTime = new DateTime(1998, 1, 11); - PersianDateShamsi persianDateShamsi = new PersianDateShamsi(); - [TestMethod] - public void YearTest() - { - Assert.AreEqual(1376, persianDateShamsi.GetShamsiYear(dateTime)); - } - [TestMethod] - public void ShortYearTest() - { - Assert.AreEqual("76", persianDateShamsi.GetShortShamsiYear(dateTime)); - } - [TestMethod] - public void YearStringTest() - { - Assert.AreEqual("1376", persianDateShamsi.GetShamsiYearToString(dateTime)); - } - [TestMethod] - public void MonthTest() - { - Assert.AreEqual(10, persianDateShamsi.GetShamsiMonth(dateTime)); - } - [TestMethod] - public void MonthStringTest() - { - Assert.AreEqual("10", persianDateShamsi.GetShamsiMonthString(dateTime.Value)); - } - [TestMethod] - public void MonthNameTest() - { - Assert.AreEqual("دی", persianDateShamsi.GetShamsiMonthName(dateTime)); - } - [TestMethod] - public void DayTest() - { - Assert.AreEqual(21, persianDateShamsi.GetShamsiDay(dateTime)); - } - [TestMethod] - public void DayStringTest() - { - Assert.AreEqual("21", persianDateShamsi.GetShamsiDayString(dateTime)); - } - [TestMethod] - public void DayNameTest() - { - Assert.AreEqual("یکشنبه", persianDateShamsi.GetShamsiDayName(dateTime)); - } - [TestMethod] - public void DayShortNameTest() - { - Assert.AreEqual("ی", persianDateShamsi.GetShamsiDayShortName(dateTime)); - } - - // Additional test methods for edge cases and invalid inputs - [TestMethod] - public void NullDateTimeTest() - { - DateTime? nullDateTime = null; - Assert.IsNull(persianDateShamsi.GetShamsiYear(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShortShamsiYear(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiYearToString(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiMonth(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiMonthNumber(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiMonthName(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiDay(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiDayString(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiDayName(nullDateTime)); - Assert.IsNull(persianDateShamsi.GetShamsiDayShortName(nullDateTime)); - } + // Arrange + DateTime? testDate = dateTime; + + // Act + int? result = persianDateShamsi.GetShamsiMonth(testDate); + + // Assert + Assert.AreEqual(10, result); + } + + /// + /// Tests the GetShamsiMonthString method with a valid DateTime. + /// + [TestMethod] + public void MonthStringTest() + { + // Arrange + DateTime testDate = dateTime.Value; + + // Act + string result = persianDateShamsi.GetShamsiMonthString(testDate); + + // Assert + Assert.AreEqual("10", result); + } + + /// + /// Tests the GetShamsiMonthName method with a valid DateTime. + /// + [TestMethod] + public void MonthNameTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShamsiMonthName(testDate); + + // Assert + Assert.AreEqual("دی", result); + } + + /// + /// Tests the GetShamsiDay method with a valid DateTime. + /// + [TestMethod] + public void DayTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + int? result = persianDateShamsi.GetShamsiDay(testDate); + + // Assert + Assert.AreEqual(21, result); + } + + /// + /// Tests the GetShamsiDayString method with a valid DateTime. + /// + [TestMethod] + public void DayStringTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShamsiDayString(testDate); + + // Assert + Assert.AreEqual("21", result); + } + + /// + /// Tests the GetShamsiDayName method with a valid DateTime. + /// + [TestMethod] + public void DayNameTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShamsiDayName(testDate); + + // Assert + Assert.AreEqual("یکشنبه", result); + } + + /// + /// Tests the GetShamsiDayShortName method with a valid DateTime. + /// + [TestMethod] + public void DayShortNameTest() + { + // Arrange + DateTime? testDate = dateTime; + + // Act + string? result = persianDateShamsi.GetShamsiDayShortName(testDate); + + // Assert + Assert.AreEqual("ی", result); + } + + /// + /// Tests the methods with a null DateTime. + /// + [TestMethod] + public void NullDateTimeTest() + { + // Arrange + DateTime? nullDateTime = null; + + // Act & Assert + Assert.IsNull(persianDateShamsi.GetShamsiYear(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShortShamsiYear(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiYearToString(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiMonth(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiMonthNumber(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiMonthName(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiDay(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiDayString(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiDayName(nullDateTime)); + Assert.IsNull(persianDateShamsi.GetShamsiDayShortName(nullDateTime)); + } + + /// + /// Tests the GetShamsiYear method with a DateTimeOffset. + /// + [TestMethod] + public void DateTimeOffsetTest() + { + // Arrange + DateTimeOffset? dateTimeOffset = new DateTimeOffset(1998, 1, 11, 0, 0, 0, TimeSpan.Zero); + + // Act + int? result = persianDateShamsi.GetShamsiYear(dateTimeOffset); + + // Assert + Assert.AreEqual(1376, result); + } + + /// + /// Tests the GetShamsiMonth method with a DateTimeOffset. + /// + [TestMethod] + public void MonthDateTimeOffsetTest() + { + // Arrange + DateTimeOffset? dateTimeOffset = new DateTimeOffset(1998, 1, 11, 0, 0, 0, TimeSpan.Zero); + + // Act + int? result = persianDateShamsi.GetShamsiMonth(dateTimeOffset); + + // Assert + Assert.AreEqual(10, result); + } + + /// + /// Tests the GetShamsiDay method with a DateTimeOffset. + /// + [TestMethod] + public void DayDateTimeOffsetTest() + { + // Arrange + DateTimeOffset? dateTimeOffset = new DateTimeOffset(1998, 1, 11, 0, 0, 0, TimeSpan.Zero); + + // Act + int? result = persianDateShamsi.GetShamsiDay(dateTimeOffset); + + // Assert + Assert.AreEqual(21, result); + } + + /// + /// Tests multiple methods of PersianDateShamsi with various DateTime inputs. + /// + [TestMethod] + public void ComplexTest() + { + // Arrange + DateTime? validDate = new DateTime(2023, 3, 21); + DateTime? nullDate = null; + _ = new DateTime(1800, 1, 1); + DateTimeOffset? validDateOffset = new DateTimeOffset(2023, 3, 21, 0, 0, 0, TimeSpan.Zero); + + // Act + int? validYear = persianDateShamsi.GetShamsiYear(validDate); + string? validShortYear = persianDateShamsi.GetShortShamsiYear(validDate); + string? validYearString = persianDateShamsi.GetShamsiYearToString(validDate); + int? validMonth = persianDateShamsi.GetShamsiMonth(validDate); + string validMonthString = persianDateShamsi.GetShamsiMonthString(validDate.Value); + string? validMonthName = persianDateShamsi.GetShamsiMonthName(validDate); + int? validDay = persianDateShamsi.GetShamsiDay(validDate); + string? validDayString = persianDateShamsi.GetShamsiDayString(validDate); + string? validDayName = persianDateShamsi.GetShamsiDayName(validDate); + string? validDayShortName = persianDateShamsi.GetShamsiDayShortName(validDate); + + int? nullYear = persianDateShamsi.GetShamsiYear(nullDate); + string? nullShortYear = persianDateShamsi.GetShortShamsiYear(nullDate); + string? nullYearString = persianDateShamsi.GetShamsiYearToString(nullDate); + int? nullMonth = persianDateShamsi.GetShamsiMonth(nullDate); + int? nullMonthNumber = persianDateShamsi.GetShamsiMonthNumber(nullDate); + string? nullMonthName = persianDateShamsi.GetShamsiMonthName(nullDate); + int? nullDay = persianDateShamsi.GetShamsiDay(nullDate); + string? nullDayString = persianDateShamsi.GetShamsiDayString(nullDate); + string? nullDayName = persianDateShamsi.GetShamsiDayName(nullDate); + string? nullDayShortName = persianDateShamsi.GetShamsiDayShortName(nullDate); + + int? validYearOffset = persianDateShamsi.GetShamsiYear(validDateOffset); + int? validMonthOffset = persianDateShamsi.GetShamsiMonth(validDateOffset); + int? validDayOffset = persianDateShamsi.GetShamsiDay(validDateOffset); + + // Assert + Assert.AreEqual(1402, validYear); + Assert.AreEqual("02", validShortYear); + Assert.AreEqual("1402", validYearString); + Assert.AreEqual(1, validMonth); + Assert.AreEqual("01", validMonthString); + Assert.AreEqual("فروردین", validMonthName); + Assert.AreEqual(1, validDay); + Assert.AreEqual("01", validDayString); + Assert.AreEqual("سه‌شنبه", validDayName); + Assert.AreEqual("س", validDayShortName); + + Assert.IsNull(nullYear); + Assert.IsNull(nullShortYear); + Assert.IsNull(nullYearString); + Assert.IsNull(nullMonth); + Assert.IsNull(nullMonthNumber); + Assert.IsNull(nullMonthName); + Assert.IsNull(nullDay); + Assert.IsNull(nullDayString); + Assert.IsNull(nullDayName); + Assert.IsNull(nullDayShortName); + + Assert.AreEqual(1402, validYearOffset); + Assert.AreEqual(1, validMonthOffset); + Assert.AreEqual(1, validDayOffset); } } diff --git a/PersianDateTests/ToShamsiTest.cs b/PersianDateTests/ToShamsiTest.cs index 5517cd2..5d296af 100644 --- a/PersianDateTests/ToShamsiTest.cs +++ b/PersianDateTests/ToShamsiTest.cs @@ -1,38 +1,141 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; using PersianDate; -namespace PersianDateTests +namespace PersianDateTests; + +[TestClass] +public class ToShamsiTest { - [TestClass] - public class ToShamsiTest - { - //My Birthday - DateTime? dateTime = new DateTime(1998, 1, 11); - [TestMethod] - public void ToShamsiDateTest() - { - Assert.AreEqual("1376/10/21", dateTime.ToShamsiDate()); - } - [TestMethod] - public void ToShortShamsiDateTest() - { - Assert.AreEqual("76/10/21", dateTime.ToShortShamsiDate()); - } - [TestMethod] - public void ToLongShamsiDateTest() - { - Assert.AreEqual("یکشنبه 21 دی 1376", dateTime.ToLongShamsiDate()); - } - - // Additional test methods for edge cases and invalid inputs - [TestMethod] - public void NullDateTimeTest() - { - DateTime? nullDateTime = null; - Assert.IsNull(nullDateTime.ToShamsiDate()); - Assert.IsNull(nullDateTime.ToShortShamsiDate()); - Assert.IsNull(nullDateTime.ToLongShamsiDate()); - } + // My Birthday + private readonly DateTime? dateTime = new DateTime(1998, 1, 11); + private readonly DateTimeOffset? dateTimeOffset = new DateTimeOffset(1998, 1, 11, 0, 0, 0, TimeSpan.Zero); + + /// + /// Test for converting DateTime to Shamsi date. + /// + [TestMethod] + public void ToShamsiDateTest() + { + // Arrange + DateTime? testDateTime = dateTime; + + // Act + string? result = testDateTime.ToShamsiDate(); + + // Assert + Assert.AreEqual("1376/10/21", result); + } + + /// + /// Test for converting DateTime to short Shamsi date. + /// + [TestMethod] + public void ToShortShamsiDateTest() + { + // Arrange + DateTime? testDateTime = dateTime; + + // Act + string? result = testDateTime.ToShortShamsiDate(); + + // Assert + Assert.AreEqual("76/10/21", result); + } + + /// + /// Test for converting DateTime to long Shamsi date. + /// + [TestMethod] + public void ToLongShamsiDateTest() + { + // Arrange + DateTime? testDateTime = dateTime; + + // Act + string? result = testDateTime.ToLongShamsiDate(); + + // Assert + Assert.AreEqual("یکشنبه 21 دی 1376", result); + } + + /// + /// Test for converting null DateTime to Shamsi date. + /// + [TestMethod] + public void NullDateTimeTest() + { + // Arrange + DateTime? nullDateTime = null; + + // Act + string? result = nullDateTime.ToShamsiDate(); + + // Assert + Assert.IsNull(result); + } + + /// + /// Test for converting DateTimeOffset to Shamsi date. + /// + [TestMethod] + public void ToShamsiDateOffsetTest() + { + // Arrange + DateTimeOffset? testDateTimeOffset = dateTimeOffset; + + // Act + string? result = testDateTimeOffset.ToShamsiDate(); + + // Assert + Assert.AreEqual("1376/10/21", result); + } + + /// + /// Test for converting DateTimeOffset to short Shamsi date. + /// + [TestMethod] + public void ToShortShamsiDateOffsetTest() + { + // Arrange + DateTimeOffset? testDateTimeOffset = dateTimeOffset; + + // Act + string? result = testDateTimeOffset.ToShortShamsiDate(); + + // Assert + Assert.AreEqual("76/10/21", result); + } + + /// + /// Test for converting DateTimeOffset to long Shamsi date. + /// + [TestMethod] + public void ToLongShamsiDateOffsetTest() + { + // Arrange + DateTimeOffset? testDateTimeOffset = dateTimeOffset; + + // Act + string? result = testDateTimeOffset.ToLongShamsiDate(); + + // Assert + Assert.AreEqual("یکشنبه 21 دی 1376", result); + } + + /// + /// Test for converting null DateTimeOffset to Shamsi date. + /// + [TestMethod] + public void NullDateTimeOffsetTest() + { + // Arrange + DateTimeOffset? nullDateTimeOffset = null; + + // Act + string? result = nullDateTimeOffset.ToShamsiDate(); + + // Assert + Assert.IsNull(result); } } From 18d2bcfcf69462a26411e39340dd862025e9a534 Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 02:22:34 +0330 Subject: [PATCH 2/5] Add ToGregorian and StringUtil classes with tests Introduced the `ToGregorian` class in `ToGregorian.cs` to convert Persian (Shamsi) dates to Gregorian dates. This class includes methods to get the Gregorian year, month, day, date string, day name, and to convert a range of Shamsi dates to Gregorian dates. Added a new test class `ToGregorianTest` in `ToGregorianTest.cs` to provide unit tests for the `ToGregorian` class. This includes tests for methods like `GetGregorianYear`, `ToGregorianDate`, `GetGregorianMonth`, `GetGregorianDay`, `GetGregorianDateString`, `GetGregorianDayName`, `GetDayNameFromPersianCharacter`, `ConvertRangeToGregorian`, and `GetFirstDayOfShamsiMonth`. Added the `StringUtil` class in `StringUtil.cs` to provide utility methods for string operations. This includes methods to convert iOS keyboard and Arabic characters to Persian characters, check if a string contains Persian characters, reverse a string, and convert a string to title case. --- PersianDate/ToGregorian.cs | 217 ++++++++++++++++++++++++++++ PersianDate/Utills/StringUtil.cs | 97 +++++++++++++ PersianDateTests/ToGregorianTest.cs | 184 +++++++++++++++++++++++ 3 files changed, 498 insertions(+) create mode 100644 PersianDate/ToGregorian.cs create mode 100644 PersianDate/Utills/StringUtil.cs create mode 100644 PersianDateTests/ToGregorianTest.cs diff --git a/PersianDate/ToGregorian.cs b/PersianDate/ToGregorian.cs new file mode 100644 index 0000000..6c70a08 --- /dev/null +++ b/PersianDate/ToGregorian.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using PersianDate.Utills; + +namespace PersianDate; +/// +/// Provides methods to convert Persian (Shamsi) dates to Gregorian dates. +/// +/// +/// var persianDate = new ToGregorian(); +/// var gregorianYear = persianDate.GetGregorianYear(1402, 1, 1); // 2023 +/// var gregorianDate = persianDate.ToGregorianDate(1402, 1, 1); // 2023-03-21 +/// +/// +/// +public class ToGregorian +{ + private readonly PersianCalendar persianCalendar; + + /// + /// Initializes a new instance of the class. + /// + public ToGregorian() + { + persianCalendar = new PersianCalendar(); + } + + /// + /// Gets the Gregorian year from the specified Shamsi date. + /// + /// + /// var persianDate = new ToGregorian(); + /// int gregorianYear = persianDate.GetGregorianYear(1402, 1, 1); // Returns 2023 + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian year. + public int GetGregorianYear(int shamsiYear, int shamsiMonth, int shamsiDay) + { + DateTime gregorianDate = persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + return gregorianDate.Year; + } + + /// + /// Converts the specified Shamsi date to a Gregorian date. + /// + /// + /// var persianDate = new ToGregorian(); + /// DateTime gregorianDate = persianDate.ToGregorianDate(1402, 1, 1); // Returns 2023-03-21 + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian date. + public DateTime ToGregorianDate(int shamsiYear, int shamsiMonth, int shamsiDay) + { + return persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + } + + /// + /// Gets the Gregorian month from the specified Shamsi date. + /// + /// + /// var persianDate = new ToGregorian(); + /// int gregorianMonth = persianDate.GetGregorianMonth(1402, 1, 1); // Returns 3 + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian month. + public int GetGregorianMonth(int shamsiYear, int shamsiMonth, int shamsiDay) + { + DateTime gregorianDate = persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + return gregorianDate.Month; + } + + /// + /// Gets the Gregorian day from the specified Shamsi date. + /// + /// + /// var persianDate = new ToGregorian(); + /// int gregorianDay = persianDate.GetGregorianDay(1402, 1, 1); // Returns 21 + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian day. + public int GetGregorianDay(int shamsiYear, int shamsiMonth, int shamsiDay) + { + DateTime gregorianDate = persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + return gregorianDate.Day; + } + + /// + /// Gets the Gregorian date as a string from the specified Shamsi date. + /// + /// + /// var persianDate = new ToGregorian(); + /// string gregorianDateString = persianDate.GetGregorianDateString(1402, 1, 1); // Returns "2023-03-21" + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian date as a string. + public string GetGregorianDateString(int shamsiYear, int shamsiMonth, int shamsiDay) + { + DateTime gregorianDate = persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + return gregorianDate.ToString("yyyy-MM-dd"); + } + + /// + /// Gets the Gregorian day name from the specified Shamsi date. + /// + /// + /// var persianDate = new ToGregorian(); + /// string gregorianDayName = persianDate.GetGregorianDayName(1402, 1, 1); // Returns "Tuesday" + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Shamsi day. + /// The Gregorian day name. + public string GetGregorianDayName(int shamsiYear, int shamsiMonth, int shamsiDay) + { + DateTime gregorianDate = persianCalendar.ToDateTime(shamsiYear, shamsiMonth, shamsiDay, 0, 0, 0, 0); + return gregorianDate.ToString("dddd", CultureInfo.InvariantCulture); + } + + /// + /// Gets the Gregorian day name based on a specific Persian character. + /// + /// + /// var persianDate = new ToGregorian(); + /// string dayName = persianDate.GetDayNameFromPersianCharacter('ی'); // Returns "Sunday" + /// + /// + /// + /// The Persian character. + /// The corresponding Gregorian day name. + public string GetDayNameFromPersianCharacter(char persianCharacter) + { + char convertedCharacter = StringUtil.ConvertToPersianCharacter(persianCharacter); + return convertedCharacter switch + { + 'ی' => "Sunday", + 'د' => "Monday", + 'س' => "Tuesday", + 'چ' => "Wednesday", + 'پ' => "Thursday", + 'ج' => "Friday", + 'ش' => "Saturday", + _ => "Unknown" + }; + } + + /// + /// Converts a range of Shamsi dates to Gregorian dates. + /// + /// + /// var persianDate = new ToGregorian(); + /// var gregorianDates = persianDate.ConvertRangeToGregorian(1402, 1, 1, 1402, 1, 10); + /// // Returns a list of Gregorian dates from 2023-03-21 to 2023-03-30 + /// + /// + /// + /// The start Shamsi year. + /// The start Shamsi month. + /// The start Shamsi day. + /// The end Shamsi year. + /// The end Shamsi month. + /// The end Shamsi day. + /// A list of Gregorian dates. + public List ConvertRangeToGregorian(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) + { + List gregorianDates = []; + DateTime startDate = persianCalendar.ToDateTime(startYear, startMonth, startDay, 0, 0, 0, 0); + DateTime endDate = persianCalendar.ToDateTime(endYear, endMonth, endDay, 0, 0, 0, 0); + + for (DateTime date = startDate; date <= endDate; date = date.AddDays(1)) + { + gregorianDates.Add(date); + } + + return gregorianDates; + } + + /// + /// Gets the Gregorian date for the first day of the specified Shamsi month. + /// + /// + /// var persianDate = new ToGregorian(); + /// DateTime firstDay = persianDate.GetFirstDayOfShamsiMonth(1402, 1); + /// // Returns 2023-03-21 + /// + /// + /// + /// The Shamsi year. + /// The Shamsi month. + /// The Gregorian date for the first day of the specified Shamsi month. + public DateTime GetFirstDayOfShamsiMonth(int shamsiYear, int shamsiMonth) + { + return persianCalendar.ToDateTime(shamsiYear, shamsiMonth, 1, 0, 0, 0, 0); + } +} diff --git a/PersianDate/Utills/StringUtil.cs b/PersianDate/Utills/StringUtil.cs new file mode 100644 index 0000000..4a473d6 --- /dev/null +++ b/PersianDate/Utills/StringUtil.cs @@ -0,0 +1,97 @@ +using System; +using System.Globalization; +using System.Linq; + +namespace PersianDate.Utills; + +/// +/// Provides utility methods for string operations. +/// +public static class StringUtil +{ + /// + /// Converts iOS keyboard and Arabic characters to Persian characters. + /// + /// + /// char persianChar = StringUtil.ConvertToPersianCharacter('ي'); // Returns 'ی' + /// + /// + /// + /// The input character. + /// The converted Persian character. + public static char ConvertToPersianCharacter(char input) + { + return input switch + { + 'ي' => 'ی', // Arabic 'yeh' to Persian 'yeh' + 'ك' => 'ک', // Arabic 'kaf' to Persian 'kaf' + 'ة' => 'ه', // Arabic 'teh marbuta' to Persian 'heh' + '٤' => '۴', // Arabic '4' to Persian '4' + '٥' => '۵', // Arabic '5' to Persian '5' + '٦' => '۶', // Arabic '6' to Persian '6' + _ => input + }; + } + + /// + /// Converts a string containing iOS keyboard and Arabic characters to Persian characters. + /// + /// + /// string persianString = StringUtil.ConvertToPersianString("يوم"); // Returns "یوم" + /// + /// + /// + /// The input string. + /// The converted Persian string. + public static string ConvertToPersianString(string input) + { + return new string(input.Select(ConvertToPersianCharacter).ToArray()); + } + + /// + /// Checks if a string contains any Persian characters. + /// + /// + /// bool containsPersian = StringUtil.ContainsPersianCharacters("سلام"); // Returns true + /// + /// + /// + /// The input string. + /// True if the string contains Persian characters; otherwise, false. + public static bool ContainsPersianCharacters(string input) + { + return input.Any(c => c is >= '\u0600' and <= '\u06FF'); + } + + /// + /// Reverses a string. + /// + /// + /// string reversedString = StringUtil.ReverseString("سلام"); // Returns "مالس" + /// + /// + /// + /// The input string. + /// The reversed string. + public static string ReverseString(string input) + { + char[] charArray = input.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + /// + /// Converts a string to title case. + /// + /// + /// string titleCaseString = StringUtil.ToTitleCase("hello world"); // Returns "Hello World" + /// + /// + /// + /// The input string. + /// The string converted to title case. + public static string ToTitleCase(string input) + { + return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower()); + } +} diff --git a/PersianDateTests/ToGregorianTest.cs b/PersianDateTests/ToGregorianTest.cs new file mode 100644 index 0000000..e329e80 --- /dev/null +++ b/PersianDateTests/ToGregorianTest.cs @@ -0,0 +1,184 @@ +using System; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PersianDate; + +namespace PersianDateTests; + +/// +/// Unit tests for the class. +/// +[TestClass] +public class ToGregorianTest +{ + private readonly ToGregorian toGregorian; + + public ToGregorianTest() + { + toGregorian = new ToGregorian(); + } + + /// + /// Tests the GetGregorianYear method. + /// + [TestMethod] + public void GetGregorianYear_ShouldReturnCorrectYear() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + int result = toGregorian.GetGregorianYear(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual(2023, result); + } + + /// + /// Tests the ToGregorianDate method. + /// + [TestMethod] + public void ToGregorianDate_ShouldReturnCorrectDate() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + DateTime result = toGregorian.ToGregorianDate(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual(new DateTime(2023, 3, 21), result); + } + + /// + /// Tests the GetGregorianMonth method. + /// + [TestMethod] + public void GetGregorianMonth_ShouldReturnCorrectMonth() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + int result = toGregorian.GetGregorianMonth(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual(3, result); + } + + /// + /// Tests the GetGregorianDay method. + /// + [TestMethod] + public void GetGregorianDay_ShouldReturnCorrectDay() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + int result = toGregorian.GetGregorianDay(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual(21, result); + } + + /// + /// Tests the GetGregorianDateString method. + /// + [TestMethod] + public void GetGregorianDateString_ShouldReturnCorrectDateString() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + string result = toGregorian.GetGregorianDateString(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual("2023-03-21", result); + } + + /// + /// Tests the GetGregorianDayName method. + /// + [TestMethod] + public void GetGregorianDayName_ShouldReturnCorrectDayName() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + int shamsiDay = 1; + + // Act + string result = toGregorian.GetGregorianDayName(shamsiYear, shamsiMonth, shamsiDay); + + // Assert + Assert.AreEqual("Tuesday", result); + } + + /// + /// Tests the GetDayNameFromPersianCharacter method. + /// + [TestMethod] + public void GetDayNameFromPersianCharacter_ShouldReturnCorrectDayName() + { + // Arrange + char persianCharacter = 'ی'; + + // Act + string result = toGregorian.GetDayNameFromPersianCharacter(persianCharacter); + + // Assert + Assert.AreEqual("Sunday", result); + } + + /// + /// Tests the ConvertRangeToGregorian method. + /// + [TestMethod] + public void ConvertRangeToGregorian_ShouldReturnCorrectDateRange() + { + // Arrange + int startYear = 1402; + int startMonth = 1; + int startDay = 1; + int endYear = 1402; + int endMonth = 1; + int endDay = 10; + + // Act + List result = toGregorian.ConvertRangeToGregorian(startYear, startMonth, startDay, endYear, endMonth, endDay); + + // Assert + Assert.AreEqual(10, result.Count); + Assert.AreEqual(new DateTime(2023, 3, 21), result[0]); + Assert.AreEqual(new DateTime(2023, 3, 30), result[9]); + } + + /// + /// Tests the GetFirstDayOfShamsiMonth method. + /// + [TestMethod] + public void GetFirstDayOfShamsiMonth_ShouldReturnCorrectFirstDay() + { + // Arrange + int shamsiYear = 1402; + int shamsiMonth = 1; + + // Act + DateTime result = toGregorian.GetFirstDayOfShamsiMonth(shamsiYear, shamsiMonth); + + // Assert + Assert.AreEqual(new DateTime(2023, 3, 21), result); + } +} From 6bfe94953bfe2421d192c172472fe273074976ab Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 03:04:14 +0330 Subject: [PATCH 3/5] Revamp README.md for clarity and comprehensive info The README.md file has been significantly updated to improve clarity, structure, and content. Key changes include: - Updated title and description for better readability. - Added badges for NuGet version, downloads, and build status. - Introduced a new "Features" section. - Added "Installation" instructions for NuGet. - Expanded usage examples with more detailed code snippets. - Added a "Version History" section. - Included guidelines for contributing. - Specified the MIT License. - Added a support section for issue reporting. - Enhanced "Getting Started" instructions. - Removed outdated and redundant sections. --- README.md | 177 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 6e5ff51..97e2901 100644 --- a/README.md +++ b/README.md @@ -2,111 +2,138 @@ ![Persian Date Library](https://lh3.googleusercontent.com/p_InfUloerXCEMJLLGA4n8HAQT7yR1kTn53cpYwFlFHkqa9TlaXE9K6BVef6i19JJzo=s180-rw) -Persian Data Library is a library that can be convert **Gregorian** (Milady) year to **Solar Hijri** (Shamsi) year in simplest way! - -------------------------- - -| Target | Branch | Version | -| ------ | ------ | ------ | -| Github | master | v1.0.5 | - - -## Persian Date Public Version -| Target | Branch | Version | Download link | Total downloads | CI Build Status | CD Build Status | -| ------ | ------ | ------ | ------ | ------ | ------ | ------ | -| Nuget | master | v1.0.6 | [![NuGet](https://img.shields.io/nuget/v/PersianDateShamsi.svg)](https://www.nuget.org/packages/PersianDateShamsi) | [![NuGet downloads](https://img.shields.io/nuget/dt/PersianDateShamsi.svg)](https://www.nuget.org/packages/PersianDateShamsi) | [![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CI.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) | [![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CD.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) | -| Release | master | v1.0.5 | [![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CI.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) | | [![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CI.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) | [![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CD.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) | - -## Cross Platform - -| Platform | Supported Version | -| ------ | ------ | -| .NET | 8.0 | - -## Code Example - -```c# -PersianDateShamsi persianDateShamsi = new PersianDateShamsi(); - - Console.WriteLine(persianDateShamsi.GetShamsiYear(DateTime.Now)); - //Result : 1398 - - Console.WriteLine(persianDateShamsi.GetShamsiMonthName(DateTime.Now)); - //Result : بهمن - - Console.WriteLine(persianDateShamsi.GetShamsiDayString(DateTime.Now)); - //Result : 03 - - Console.WriteLine(persianDateShamsi.GetShamsiDayName(DateTime.Now)); - //Result : پنجشنبه - - Console.WriteLine(persianDateShamsi.GetShamsiDayShortName(DateTime.Now)); - //Result : پ -``` +Convert Gregorian (Miladi) dates to Solar Hijri (Shamsi) dates with ease! + +[![NuGet](https://img.shields.io/nuget/v/PersianDateShamsi.svg)](https://www.nuget.org/packages/PersianDateShamsi) +[![NuGet downloads](https://img.shields.io/nuget/dt/PersianDateShamsi.svg)](https://www.nuget.org/packages/PersianDateShamsi) +[![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CI.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) +[![Build Status](https://github.com/hootanht/PersianDate/actions/workflows/CD.yml/badge.svg)](https://github.com/hootanht/PersianDate/actions) + +## Features -Extension Method For DateTime +- Convert Gregorian dates to Shamsi (Persian) dates +- Support for both `DateTime` and `DateTimeOffset` +- Get Shamsi year, month, and day components +- Get Shamsi month and day names +- Extension methods for easy conversion -```c# +## Installation - Console.WriteLine(DateTime.Now.ToShamsiDate()); - //Result : 1398/11/03 - - Console.WriteLine(DateTime.Now.ToShortShamsiDate()); - //Result : 98/11/03 - - Console.WriteLine(DateTime.Now.ToLongShamsiDate()); - //Result : پنجشنبه 3 بهمن 1398 +Install the package via NuGet: + +```sh +dotnet add package PersianDateShamsi ``` -## Version changes -Version 1.0.6 +## Usage + +### Basic Conversion + +```csharp +using PersianDate; --Add support for .Net 8.0 +PersianDateShamsi persianDate = new PersianDateShamsi(); +DateTime now = DateTime.Now; -Version 1.0.4 +int shamsiYear = persianDate.GetShamsiYear(now); +string shamsiMonthName = persianDate.GetShamsiMonthName(now); +string shamsiDayString = persianDate.GetShamsiDayString(now); +string shamsiDayName = persianDate.GetShamsiDayName(now); +string shamsiDayShortName = persianDate.GetShamsiDayShortName(now); --Add support for .Net 5.0 and 6.0 +Console.WriteLine($"Year: {shamsiYear}"); +Console.WriteLine($"Month: {shamsiMonthName}"); +Console.WriteLine($"Day: {shamsiDayString}"); +Console.WriteLine($"Day Name: {shamsiDayName}"); +Console.WriteLine($"Short Day Name: {shamsiDayShortName}"); +``` -Version 1.0.3 +### Extension Methods --Change .Net Standard 2.0 To .NET 7.0 +```csharp +using PersianDate; -Version 1.0.2 +DateTime? dateTime = new DateTime(2023, 10, 5); +DateTimeOffset? dateTimeOffset = new DateTimeOffset(2023, 10, 5, 0, 0, 0, TimeSpan.Zero); --Improve Flexibility +Console.WriteLine(dateTime.ToShamsiDate()); // Output: 1402/07/13 +Console.WriteLine(dateTimeOffset.ToShamsiDate()); // Output: 1402/07/13 -Version 1.0.1 +Console.WriteLine(dateTime.ToShortShamsiDate()); // Output: 02/07/13 +Console.WriteLine(dateTimeOffset.ToShortShamsiDate()); // Output: 02/07/13 --Change .Net Standard 2.1 To 2.0 To Support More Platforms +Console.WriteLine(dateTime.ToLongShamsiDate()); // Output: پنجشنبه 13 مهر 1402 +Console.WriteLine(dateTimeOffset.ToLongShamsiDate()); // Output: پنجشنبه 13 مهر 1402 +``` -## CI Pipeline +### Converting to Gregorian -The CI pipeline is now integrated into the CD pipeline and is defined in `.github/workflows/CI.yml`. It uses `macos-latest` as the VM image. The pipeline handles manual triggers, pull requests, and releases. It restores NuGet packages, builds the solution, runs tests, and validates the NuGet package. The pipeline installs .NET version 8.0.x to ensure compatibility with all targeted frameworks. +```csharp +using PersianDate; -## CD Pipeline +ToGregorian toGregorian = new ToGregorian(); -The CD pipeline is defined in `.github/workflows/CD.yml` and uses `macos-latest` as the VM image. It includes steps for creating, validating, testing, and deploying the NuGet package. The pipeline handles manual triggers, pull requests, and releases. It uses environment variables for the NuGet directory and the latest versions of GitHub Actions for checkout, setup-dotnet, and upload-artifact. The pipeline installs .NET version 8.0.x to ensure compatibility with all targeted frameworks. Additionally, it includes a step to delete existing tags if they already exist before creating a new release to avoid the 'Validation Failed: already_exists' error. The step now handles the case where the tag deletion fails and ensures the deletion step successfully removes the existing tag before proceeding to create a new release. +int gregorianYear = toGregorian.GetGregorianYear(1402, 1, 1); +DateTime gregorianDate = toGregorian.ToGregorianDate(1402, 1, 1); +int gregorianMonth = toGregorian.GetGregorianMonth(1402, 1, 1); +int gregorianDay = toGregorian.GetGregorianDay(1402, 1, 1); -## Requirements +Console.WriteLine($"Gregorian Year: {gregorianYear}"); +Console.WriteLine($"Gregorian Date: {gregorianDate}"); +Console.WriteLine($"Gregorian Month: {gregorianMonth}"); +Console.WriteLine($"Gregorian Day: {gregorianDay}"); +``` -- .NET 8.0 SDK +## Supported Platforms -## Setup and Run +- .NET 8.0 -1. **Install .NET 8.0 SDK**: Download and install the .NET 8.0 SDK from the official [.NET website](https://dotnet.microsoft.com/download/dotnet/8.0). +## Getting Started -2. **Restore Dependencies**: Open a terminal or command prompt in the project directory and run: +1. Install [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) +2. Clone the repository: + ```sh + git clone https://github.com/hootanht/PersianDate.git + ``` +3. Navigate to the project directory: + ```sh + cd PersianDate + ``` +4. Restore dependencies: ```sh dotnet restore ``` - -3. **Build the Project**: Build the project by running: +5. Build the project: ```sh dotnet build ``` - -4. **Run Tests**: Run the tests to ensure everything is working correctly: +6. Run tests: ```sh dotnet test ``` +## Version History + +| Version | Changes | +|---------|----------------------------------------------| +| 1.0.6 | Added support for .NET 8.0 | +| 1.0.4 | Added support for .NET 5.0 and 6.0 | +| 1.0.3 | Changed from .NET Standard 2.0 to .NET 7.0 | +| 1.0.2 | Improved flexibility | +| 1.0.1 | Changed from .NET Standard 2.1 to 2.0 for broader platform support | + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +## Support + +If you encounter any issues or have questions, please [open an issue](https://github.com/hootanht/PersianDate/issues) on GitHub. + +--- + +Made with ❤️ by [Hootan Hemmati](https://github.com/hootanht) \ No newline at end of file From c19bfd3eca52f73d02f4836273b8d8c5a6f4e846 Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 03:20:07 +0330 Subject: [PATCH 4/5] Update PersianDateShamsi to 1.0.7 and enhance README Updated PersianDateShamsi package version from 1.0.6 to 1.0.7 in PersianDate.csproj. Enhanced README.md with example outputs for various methods: - GetShamsiMonthName, GetShamsiDayString, GetShamsiDayName, GetShamsiDayShortName - Extension methods: ToShamsiDate, ToShortShamsiDate, ToLongShamsiDate - Gregorian conversion methods: GetGregorianYear, ToGregorianDate, GetGregorianMonth, GetGregorianDay Updated version history in README.md to include version 1.0.7, noting support for DateTimeOffset in ToGregorian class and updates to PersianDateShamsi.cs and ToShamsi.cs. --- PersianDate/PersianDate.csproj | 2 +- README.md | 38 ++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/PersianDate/PersianDate.csproj b/PersianDate/PersianDate.csproj index 261b82c..5f3d0df 100644 --- a/PersianDate/PersianDate.csproj +++ b/PersianDate/PersianDate.csproj @@ -15,7 +15,7 @@ Add support for .Net 8.0 en-US https://github.com/hootanht/PrsianDate - 1.0.6 + 1.0.7 https://github.com/hootanht/PersianDate git disable diff --git a/README.md b/README.md index 97e2901..423c8a4 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,19 @@ string shamsiDayName = persianDate.GetShamsiDayName(now); string shamsiDayShortName = persianDate.GetShamsiDayShortName(now); Console.WriteLine($"Year: {shamsiYear}"); +// Output: Year: 1402 + Console.WriteLine($"Month: {shamsiMonthName}"); +// Output: Month: فروردین + Console.WriteLine($"Day: {shamsiDayString}"); +// Output: Day: 01 + Console.WriteLine($"Day Name: {shamsiDayName}"); +// Output: Day Name: سه‌شنبه + Console.WriteLine($"Short Day Name: {shamsiDayShortName}"); +// Output: Short Day Name: سه‌ ``` ### Extension Methods @@ -56,14 +65,23 @@ using PersianDate; DateTime? dateTime = new DateTime(2023, 10, 5); DateTimeOffset? dateTimeOffset = new DateTimeOffset(2023, 10, 5, 0, 0, 0, TimeSpan.Zero); -Console.WriteLine(dateTime.ToShamsiDate()); // Output: 1402/07/13 -Console.WriteLine(dateTimeOffset.ToShamsiDate()); // Output: 1402/07/13 +Console.WriteLine(dateTime.ToShamsiDate()); +// Output: 1402/07/13 + +Console.WriteLine(dateTimeOffset.ToShamsiDate()); +// Output: 1402/07/13 -Console.WriteLine(dateTime.ToShortShamsiDate()); // Output: 02/07/13 -Console.WriteLine(dateTimeOffset.ToShortShamsiDate()); // Output: 02/07/13 +Console.WriteLine(dateTime.ToShortShamsiDate()); +// Output: 02/07/13 -Console.WriteLine(dateTime.ToLongShamsiDate()); // Output: پنجشنبه 13 مهر 1402 -Console.WriteLine(dateTimeOffset.ToLongShamsiDate()); // Output: پنجشنبه 13 مهر 1402 +Console.WriteLine(dateTimeOffset.ToShortShamsiDate()); +// Output: 02/07/13 + +Console.WriteLine(dateTime.ToLongShamsiDate()); +// Output: پنجشنبه 13 مهر 1402 + +Console.WriteLine(dateTimeOffset.ToLongShamsiDate()); +// Output: پنجشنبه 13 مهر 1402 ``` ### Converting to Gregorian @@ -79,9 +97,16 @@ int gregorianMonth = toGregorian.GetGregorianMonth(1402, 1, 1); int gregorianDay = toGregorian.GetGregorianDay(1402, 1, 1); Console.WriteLine($"Gregorian Year: {gregorianYear}"); +// Output: Gregorian Year: 2023 + Console.WriteLine($"Gregorian Date: {gregorianDate}"); +// Output: Gregorian Date: 2023-03-21 + Console.WriteLine($"Gregorian Month: {gregorianMonth}"); +// Output: Gregorian Month: 3 + Console.WriteLine($"Gregorian Day: {gregorianDay}"); +// Output: Gregorian Day: 21 ``` ## Supported Platforms @@ -116,6 +141,7 @@ Console.WriteLine($"Gregorian Day: {gregorianDay}"); | Version | Changes | |---------|----------------------------------------------| +| 1.0.7 | Added support for `DateTimeOffset` in `ToGregorian` class and updated `PersianDateShamsi.cs` and `ToShamsi.cs` accordingly | | 1.0.6 | Added support for .NET 8.0 | | 1.0.4 | Added support for .NET 5.0 and 6.0 | | 1.0.3 | Changed from .NET Standard 2.0 to .NET 7.0 | From e3ded6e9c911c067e8e9f45bcd39f509cd804aa5 Mon Sep 17 00:00:00 2001 From: Hootan Hemmati Date: Thu, 3 Oct 2024 03:25:47 +0330 Subject: [PATCH 5/5] Add MIT License to the project Introduced an MIT License to the project, granting permissions for use, copy, modification, merging, publishing, distribution, sublicensing, and selling of the software. The license includes a 2024 copyright notice attributed to Hootan Hemmati and specifies that the software is provided "as is" without any warranty, limiting the liability of the authors. --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4bd519b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Hootan Hemmati + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.