Jalaali (Persian) Date input support#1205
Conversation
|
Is this something that could be done with inheritance? By making Then users could create their own custom date formats and calendars types? Could even have a Jalaali version of DateField (either in core or Scenarios) to show how to implement. |
Sounds good! It needs to change some methods identifiers in |
this seems like a good approach. |
tig
left a comment
There was a problem hiding this comment.
We currently have zero unit tests for DateField. Via other tests, it has about 53% code coverage.
I'd like to ask you to add a DateFieldTest.cs module with some tests that would get this closer to 80%. Would you please do this as part of this PR?
Examples:
- the constructors
- functions like
GetLongFormat, 'GetDate', etc... - anywhere you see any sort of logic that maniuplates stuff
Thanks!!!!
| /// <param name="isShort">If true, shows only two digits for the year.</param> | ||
| public DateField (int x, int y, DateTime date, bool isShort = false) : base (x, y, isShort ? 10 : 12, "") | ||
| /// <param name="isJalaali">If true, parse will convert jalaali input fo georgian date</param> | ||
| public DateField (int x, int y, DateTime date, bool isShort = false, bool isJalaali = false) : base (x, y, isShort ? 10 : 12, "") |
There was a problem hiding this comment.
A few unit tests that prove the various constructors work as they should would be good.
| DateChanged?.Invoke (args); | ||
| } | ||
|
|
||
| string ToJalaaliString (string format, DateTime date) |
There was a problem hiding this comment.
You should have a unit test for this.
|
Hi @inamvar - We'd love to integrate this PR. Will you have a chance soon to address the changes requested? |
|
@inamvar - are you still interested in helping with this? |
|
@inamvar - I'm marking this as a Draft PR. Hope you come back some time and help us get this merged in! |
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for the Jalaali (Persian) calendar to the DateField component, allowing users to input and display dates using the Persian calendar system. The implementation includes new constructor parameters, date conversion logic, and formatting capabilities.
Key Changes
- Added
isJalaaliparameter toDateFieldconstructors to enable Persian calendar mode - Implemented
ToJalaaliStringmethod to convert Gregorian dates to Jalaali format - Modified
SetTextmethod to handle Jalaali date parsing and validation
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 22 comments.
| File | Description |
|---|---|
| Terminal.Gui/Views/DateField.cs | Core implementation of Jalaali calendar support including new constructors, date conversion logic, and formatting method |
| UICatalog/Scenarios/Text.cs | Minor whitespace-only change with no functional impact |
Comments suppressed due to low confidence (2)
Terminal.Gui/Views/DateField.cs:289
- Logic error: The variable
dis undefined in the Jalaali code path (lines 266-285). WhenisJalaaliis true, the code skips thestring d = GetDate(...)declaration on line 280, but line 287 still attempts to usedinDateTime.TryParseExact(d, format, ...), which will cause a compilation error.
if (isJalaali) {
try {
PersianCalendar pc = new PersianCalendar ();
if (year < 10) {
year += 1400;
}
result = pc.ToDateTime (year, month, day, 12, 0, 0, 0);
year = result.Year;
month = result.Month;
day = result.Day;
} catch {
return false;
}
} else {
string d = GetDate (month, day, year, frm);
if (!DateTime.TryParseExact (d, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out result) ||
!isValidDate)
return false;
}
if (!DateTime.TryParseExact (d, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
!isValidDate)
return false;
Terminal.Gui/Views/DateField.cs:289
- Unreachable code: Lines 287-289 contain duplicate validation logic that can never be reached when
isJalaaliis true (because line 278 already returns false on error). WhenisJalaaliis false, these lines are also unreachable because line 284 already returns false if validation fails. This entire block appears to be dead code left over from the original implementation and should be removed.
if (!DateTime.TryParseExact (d, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
!isValidDate)
return false;
| this.Text = ToJalaaliString (Format, date); | ||
| } else { | ||
| this.Text = value.ToString (Format); | ||
| } | ||
| var args = new DateTimeEventArgs<DateTime> (oldData, value, Format); |
There was a problem hiding this comment.
Case sensitivity issue: Format should be format (lowercase). The property format is defined as a lowercase property expression, not Format.
| this.Text = ToJalaaliString (Format, date); | |
| } else { | |
| this.Text = value.ToString (Format); | |
| } | |
| var args = new DateTimeEventArgs<DateTime> (oldData, value, Format); | |
| this.Text = ToJalaaliString (format, date); | |
| } else { | |
| this.Text = value.ToString (format); | |
| } | |
| var args = new DateTimeEventArgs<DateTime> (oldData, value, format); |
| this.isShort = isShort; | ||
|
|
||
| Date = date; | ||
| CursorPosition = 1; |
There was a problem hiding this comment.
Duplicate CursorPosition assignment. Line 88 sets CursorPosition = 1; but it's set again on line 93. This is redundant - one of these assignments should be removed.
| CursorPosition = 1; |
| /// </summary> | ||
| /// <param name="date"></param> | ||
| public DateField (DateTime date) : base ("") | ||
| /// <param name="isJalaali"></param> |
There was a problem hiding this comment.
Missing XML documentation: The new isJalaali parameter lacks proper XML documentation. According to the CONTRIBUTING.md guidelines, all public API parameters must have clear, concise documentation using <param> tags.
|
|
||
| this.isJalaali = isJalaali; | ||
| if (!isJalaali) | ||
| this.isShort = true; | ||
| Width = FieldLen + 2; | ||
|
|
There was a problem hiding this comment.
[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.
|
|
||
|
|
||
| var year = pc.GetYear (date); | ||
| var month = pc.GetMonth (date); | ||
| var day = pc.GetDayOfMonth (date); | ||
|
|
||
|
|
There was a problem hiding this comment.
[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.
| var year = pc.GetYear (date); | |
| var month = pc.GetMonth (date); | |
| var day = pc.GetDayOfMonth (date); | |
| var year = pc.GetYear (date); | |
| var month = pc.GetMonth (date); | |
| var day = pc.GetDayOfMonth (date); |
| string sepChar; | ||
| string longFormat; | ||
| string shortFormat; | ||
| bool isJalaali; |
There was a problem hiding this comment.
Field 'isJalaali' can be 'readonly'.
| bool isJalaali; | |
| readonly bool isJalaali; |
| } catch { | ||
| return false; |
There was a problem hiding this comment.
Generic catch clause.
| } catch { | |
| return false; | |
| } catch (ArgumentOutOfRangeException) { | |
| return false; | |
| } catch (FormatException) { | |
| return false; |
| if (isJalaali) { | ||
|
|
||
| this.Text = ToJalaaliString (Format, date); | ||
| } else { | ||
| this.Text = value.ToString (Format); | ||
| } |
There was a problem hiding this comment.
Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
| if (isJalaali) { | |
| this.Text = ToJalaaliString (Format, date); | |
| } else { | |
| this.Text = value.ToString (Format); | |
| } | |
| this.Text = isJalaali ? ToJalaaliString (Format, date) : value.ToString (Format); |
|
Too stale. Closing. |
resolving issue #1204