Skip to content

Commit

Permalink
Merge pull request #15 from shahabfar/release
Browse files Browse the repository at this point in the history
add Islamic date conversions
  • Loading branch information
shahabfar authored Mar 28, 2024
2 parents 3d78ac4 + 90db1a6 commit e37e374
Show file tree
Hide file tree
Showing 16 changed files with 791 additions and 71 deletions.
65 changes: 60 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ The `PersianDateTime` class in the Persia.Net library is a comprehensive utility
| `Today` | Gets the current date in the Persian calendar.|


### Converting Gregorian Date to Persian Date
### Converting Gregorian date to Persian date

The provided code snippet is a simple example of converting a Gregorian date to a Persian date using `ToPersianDateTime()` extension method.

Expand All @@ -106,7 +106,7 @@ In addition to date conversion, the `PersianDateTime` class also includes built-
| `ToPersianWeekdayString` | Converts the date to a string in Persian as well as week day name.<br>برای مثال, یکشنبه ۱۴۰۲/۱۲/۲۰|
| `ToLongPersianOrdinalWords` |Converts the date to a string in the Persian format with Year in digit and Day and Month in words as well as weekday name.<br>برای مثال, یکشنبه بیستم اسفند ۱۴۰۲|

## Converting Persian Date to Gregorian DateTime
## Converting Persian date to Gregorian DateTime
In .NET, the `PersianDateTime` class provides straightforward methods for converting Persian dates to Gregorian dates or `DateTime`. Specifically, the `ToDateTime` and `ToDateOnly` methods can be utilized for this purpose. Both of these methods are available as static and instance methods within the `PersianDateTime` class. This allows for flexible usage depending on the specific requirements of your code.

The `ToDateTime` method in the `PersianDateTime` class allows you to convert a Persian date to a Gregorian `DateTime`. This method is static and takes three parameters: `year`, `month`, and `day`.
Expand Down Expand Up @@ -190,7 +190,7 @@ Here are the enumerable `TimeUnit` values:
Minutes
Seconds
```
### Retrieving Detailed Week and Month Data in the Persian Calendar
### Retrieving Detailed Week and Month data in the Persian calendar
If you're interested in obtaining information about the current week or month in the Persian calendar, this library offers specific `DateTime` extension methods designed for this purpose. These methods include:

- `CurrentPersianWeek`: Retrieves the current week.
Expand All @@ -208,6 +208,61 @@ These methods return an array of `PersianDateTime` objects, each containing deta

In the above image, you can see the date-related data for the next week of a given date as a `PersianDateTime` array.

## Converting Arabic (Islamic) Date to Persian Date
I'm actively working on this feature and it will be available shortly. Stay tuned!
## Converting Gregorian date to Islamic (Lunar Hijri) date

The provided code snippet is a simple example of converting a Gregorian date to an Islamic date using `ToIslamicDateTime()` extension method.

```csharp
var date = new DateTime(2024, 03, 26); // Define a Gregorian date
// Convert the Gregorian date to an Islamic date
var dtIslamic = date.ToIslamicDateTime(); // returns an object of IslamicDateTime class
Console.WriteLine(dtIslamic.Year);
Console.WriteLine(dtIslamic.Month);
Console.WriteLine(dtIslamic.Day);
```

In addition to date conversion, the `IslamicDateTime` class also includes built-in string generation methods. These methods transform the converted DateTime into a human-readable Islamic string, providing a more intuitive representation of the date and time in Islamic. The following table outlines these methods:

| Method | Description |
| --- | --- |
| `ToString` | Converts the date to a string in the format "yyyy/MM/dd".<br>برای مثال, 1445/09/17|
| `ToShortArabicString` | Converts the date to a string in the format "yyyy/MM/dd" with Arabic numbers.<br>برای مثال, ١٤٤٥/٠٩/١٧|
| `ToArabicString` | Converts the date to a string in the Arabic format with Year and Day in digit and Month in word.<br>برای مثال, ١٧ رمضان ١٤٤٥|
| `ToLongArabicString` | Converts the date to a string in the Arabic format with Year and Day in digit and Month in word as well as weekday name.<br>برای مثال, الاربعا ١٧ رمضان ١٤٤٥ |

## Converting Persian date to Islamic (Lunar Hijri) date

The PersianDateTime class contains `ToIslamicDateTime` and `ToIslamicDateOnly` methods to convert a Persian (Solar Hijri) date to an Islamic (Lunar Hijri) date. The `ToIslamicDateTime` method also includes time conversion. These methods are existing in both static and instance methods.

The `ToIslamicDateTime` method takes seven parameters: the year, month, day, hour, minute, second, and an optional millisecond from the Persian date while, The `ToIslamicDateOnly` method takes three parameters: the year, month, and day from the Persian date.

Here’s a small sample code to demonstrate the conversion:
```csharp
var persianDate = new PersianDateTime(1403, 01, 09);

// Convert the Persian date to an Islamic date string
var islamicDate = persianDate.ToIslamicDateTime().ToString();

// Print the Islamic date
Console.WriteLine(islamicDate);
```

## Converting Islamic (Lunar Hijri) date to Persian date

The `IslamicDateTime` class contains `ToPersianDateTime` and `ToPersianDateOnly` methods to convert an Islamic (Lunar Hijri) date to a Persian (Solar Hijri) date. The `ToPersianDateTime` method also includes time conversion. These methods are existing in both static and instance methods.

The `ToPersianDateTime` method takes seven parameters: the year, month, day, hour, minute, second, and an optional millisecond from the Islamic date. It uses a Converter to convert the Islamic date to a Persian date, sets the time, and returns the resulting Persian date and time.

The `ToPersianDateOnly` method takes three parameters: the year, month, and day from the Islamic date. It uses a Converter to convert the Islamic date to a Persian date and returns the resulting Persian date.

Here’s a small sample code to demonstrate the conversion:
```csharp
var islamicDate = new IslamicDateTime(1445, 09, 18);

// Convert the Islamic date to a Persian date string
var persianDate = islamicDate.ToPersianDateOnly().ToString();

// Print the Persian date
Console.WriteLine(persianDate);
```
128 changes: 128 additions & 0 deletions src/Persia.Net.Test/IslamicDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
namespace Persia.Net.Test;

public class IslamicDateTimeTests
{
[Fact]
public void Test_ConvertToIslamicDate_ReturnCorrectDate()
{
// Arrange
DateTime nonNullableDate = new DateTime(2024, 03, 26);

// Act
var convertedNonNullableDate = nonNullableDate.ToIslamicDateTime();

// Assert
Assert.Equal(16, convertedNonNullableDate.Day);
Assert.Equal(09, convertedNonNullableDate.Month);
Assert.Equal(1445, convertedNonNullableDate.Year);
}

[Fact]
public void Test_ConvertToDateTime_ReturnCorrectDate()
{
// Arrange
const int islamicYear = 1445;
const int islamicMonth = 09;
const int islamicDay = 17;

// Act
var date = IslamicDateTime.ToDateTime(islamicYear, islamicMonth, islamicDay).Date;
var dateOnly = IslamicDateTime.ToDateOnly(islamicYear, islamicMonth, islamicDay);

// Assert
Assert.Equal(new DateTime(2024, 03, 27).Date, date);
Assert.Equal(new DateOnly(2024, 03, 27), dateOnly);
}

[Fact]
public void Test_ConvertToIslamicDateToString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 27);

// Act
var convertedNonNullableDateString = date.ToIslamicDateTime().ToString();

// Assert
Assert.Equal("1445/09/17", convertedNonNullableDateString);
}

[Fact]
public void Test_ConvertToIslamicDateToShortArabicString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 27);

// Act
var convertedDateString = date.ToIslamicDateTime().ToShortArabicString();

// Assert
Assert.Equal("١٤٤٥/٠٩/١٧", convertedDateString);
}

[Fact]
public void Test_ConvertToIslamicDateToArabicString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 27);

// Act
var convertedDateString = date.ToIslamicDateTime().ToArabicString();

// Assert
Assert.Equal("‫١٧ رمضان ١٤٤٥‬", convertedDateString);
}

[Fact]
public void Test_ConvertToIslamicDateToLongIslamicString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 27);

// Act
var convertedDateString = date.ToIslamicDateTime().ToLongArabicString();

// Assert
Assert.Equal("الاربعا ١٧ رمضان ١٤٤٥", convertedDateString);
}

[Fact]
public void Parse_ValidDate_ReturnsCorrectIslamicDateTime()
{
// Arrange
var date = "١٤٤٥/٠٩/١٧";// "1445/09/17";

// Act
var dtIslamic = IslamicDateTime.Parse(date);
var success = IslamicDateTime.TryParse(date, out var result);
var failure = IslamicDateTime.TryParse("invalid date", out var result2);

// Assert
Assert.Equal(1445, dtIslamic.Year);
Assert.Equal(09, dtIslamic.Month);
Assert.Equal(17, dtIslamic.Day);

Assert.Throws<FormatException>(() => IslamicDateTime.Parse("invalid date"));
Assert.True(success);
Assert.Equal(1445, result.Year);
Assert.Equal(09, result.Month);
Assert.Equal(17, result.Day);
Assert.False(failure);
Assert.Null(result2);
}

[Fact]
public void Test_ConvertIslamicToPersianDate_ReturnCorrectDate()
{
// Arrange
var dtIslamic = new IslamicDateTime(1445, 09, 18);

// Act
var convertedDate = dtIslamic.ToPersianDateOnly();

// Assert
Assert.Equal(09, convertedDate.Day);
Assert.Equal(01, convertedDate.Month);
Assert.Equal(1403, convertedDate.Year);
}
}
31 changes: 23 additions & 8 deletions src/Persia.Net.Test/PersianDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Test_ConvertToPersianDate_ReturnCorrectDate()
}

[Fact]
public void Test_ConvertToPersianDateToString_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 10);
Expand All @@ -37,7 +37,7 @@ public void Test_ConvertToPersianDateToString_ReturnCorrectStirng()
}

[Fact]
public void Test_ConvertToPersianDateToPersianString_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToShortPersianString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 10);
Expand All @@ -50,7 +50,7 @@ public void Test_ConvertToPersianDateToPersianString_ReturnCorrectStirng()
}

[Fact]
public void Test_ConvertToPersianDateToPersianDayMonthString_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToPersianDayMonthString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 10);
Expand All @@ -63,7 +63,7 @@ public void Test_ConvertToPersianDateToPersianDayMonthString_ReturnCorrectStirng
}

[Fact]
public void Test_ConvertToPersianDateToPersianStringWithWeekDay_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToPersianStringWithWeekDay_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 10);
Expand All @@ -76,7 +76,7 @@ public void Test_ConvertToPersianDateToPersianStringWithWeekDay_ReturnCorrectSti
}

[Fact]
public void Test_ConvertToPersianDateToPersianDayMonthStringWithWeekday_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToPersianDayMonthStringWithWeekday_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 10);
Expand All @@ -89,7 +89,7 @@ public void Test_ConvertToPersianDateToPersianDayMonthStringWithWeekday_ReturnCo
}

[Fact]
public void Test_ConvertToPersianDateToPersianMonthString_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToPersianMonthString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 22);
Expand All @@ -102,7 +102,7 @@ public void Test_ConvertToPersianDateToPersianMonthString_ReturnCorrectStirng()
}

[Fact]
public void Test_ConvertToPersianDateToPersianMonthStringWithWeekday_ReturnCorrectStirng()
public void Test_ConvertToPersianDateToLongPersianString_ReturnCorrectString()
{
// Arrange
var date = new DateTime(2024, 03, 22);
Expand Down Expand Up @@ -145,7 +145,7 @@ public void Test_IsGivenPersianYearIsLeap_ReturnCorrectDate()
}

[Fact]
public void Test_GetDayOfYearAndDaysRamainingInYear_ReturnCorrectNumbers()
public void Test_GetDayOfYearAndDaysRemainingInYear_ReturnCorrectNumbers()
{
// Arrange
var date = new DateTime(2024, 03, 19);
Expand Down Expand Up @@ -263,5 +263,20 @@ public void Parse_ValidDate_ReturnsCorrectPersianDateTime()
Assert.False(failure);
Assert.Null(result2);
}

[Fact]
public void Test_ConvertPersianToIslamicDate_ReturnCorrectDate()
{
// Arrange
var dtPersian = new PersianDateTime(1403, 01, 09);

// Act
var convertedDate = dtPersian.ToIslamicDateTime();

// Assert
Assert.Equal(18, convertedDate.Day);
Assert.Equal(09, convertedDate.Month);
Assert.Equal(1445, convertedDate.Year);
}
}
}
14 changes: 14 additions & 0 deletions src/Persia.Net/Constants/IslamicCalendarConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Persia.Net;

public class IslamicCalendarConstants
{
/// <summary>
/// Month names in Islamic calendar.
/// </summary>
public static readonly string[] Months = ["محرم", "صفر", "ربیع الاول", "ربیع الثانی", "جمادی الاولی", "جمادی الثانیة", "رجب", "شعبان", "رمضان", "شوال", "ذی قعده", "ذی حجّه"];

/// <summary>
/// Weekday names in Islamic calendar (Arabic)
/// </summary>
public static readonly string[] Weekdays = ["اسبت", "الاحد", "الاثنین", "اثلاثا", "الاربعا", "الخمیس", "اجمعه"];
}
10 changes: 5 additions & 5 deletions src/Persia.Net/Constants/PersianCalendarConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
public class PersianCalendarConstants
{
/// <summary>
/// نام های ماه های تقویم فارسی.
/// Month names in Persian calendar.
/// </summary>
public static readonly string[] Months = ["فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند"];

/// <summary>
/// نام های روزهای ماه به زبان فارسی.
/// Day names in month in Persian
/// </summary>
public static readonly string[] Days = ["یکم","دوم","سوم","چهارم","پنجم","ششم","هفتم","هشتم","نهم","دهم","یازدهم","دوازدهم","سیزدهم",
"چهاردهم","پانزدهم","شانزدهم","هفدهم","هجدهم","نوزدهم","بیستم","بیست و یکم","بیست و دوم",
"بیست و سوم","بیست و چهارم","بیست و پنجم","بیست و ششم","بیست و هفتم","بیست و هشتم","بیست و نهم","سی ام","سی و یکم"];
"چهاردهم","پانزدهم","شانزدهم","هفدهم","هجدهم","نوزدهم","بیستم","بیست و یکم","بیست و دوم",
"بیست و سوم","بیست و چهارم","بیست و پنجم","بیست و ششم","بیست و هفتم","بیست و هشتم","بیست و نهم","سی ام","سی و یکم"];

/// <summary>
/// نام های روزهای هفته به زبان فارسی.
/// Weekday names in Persian calendar
/// </summary>
public static readonly string[] Weekdays = ["شنبه", "یکشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه"];
}
Loading

0 comments on commit e37e374

Please sign in to comment.