Skip to content

Conversation

@inamvar
Copy link

@inamvar inamvar commented Apr 12, 2021

resolving issue #1204

@tznind
Copy link
Collaborator

tznind commented Apr 27, 2021

Is this something that could be done with inheritance? By making GetShortFormat and maybe the Date property virtual (perhaps combined with making shortFormat and longFormat public (or readonly/protected)?

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.

@inamvar
Copy link
Author

inamvar commented Apr 27, 2021

Is this something that could be done with inheritance? By making GetShortFormat and maybe the Date property virtual (perhaps combined with making shortFormat and longFormat public (or readonly/protected)?

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 DateField class and set them as virtual methods to be able to override them in the inherited class.

@tig
Copy link
Collaborator

tig commented Apr 27, 2021

Is this something that could be done with inheritance? By making GetShortFormat and maybe the Date property virtual (perhaps combined with making shortFormat and longFormat public (or readonly/protected)?
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 DateField class and set them as virtual methods to be able to override them in the inherited class.

this seems like a good approach.

Copy link
Collaborator

@tig tig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inamvar,

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, "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a unit test for this.

@tig
Copy link
Collaborator

tig commented May 30, 2021

Hi @inamvar - We'd love to integrate this PR. Will you have a chance soon to address the changes requested?

@tig
Copy link
Collaborator

tig commented Jul 21, 2021

@inamvar - are you still interested in helping with this?

@tig
Copy link
Collaborator

tig commented Oct 20, 2022

@inamvar - I'm marking this as a Draft PR. Hope you come back some time and help us get this merged in!

@tig tig marked this pull request as draft October 20, 2022 14:29
@inamvar inamvar marked this pull request as ready for review October 23, 2022 13:08
@tig tig requested a review from Copilot November 12, 2025 18:53
@tig tig removed the request for review from migueldeicaza November 12, 2025 18:54
Copilot finished reviewing on behalf of tig November 12, 2025 18:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isJalaali parameter to DateField constructors to enable Persian calendar mode
  • Implemented ToJalaaliString method to convert Gregorian dates to Jalaali format
  • Modified SetText method 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 d is undefined in the Jalaali code path (lines 266-285). When isJalaali is true, the code skips the string d = GetDate(...) declaration on line 280, but line 287 still attempts to use d in DateTime.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 isJalaali is true (because line 278 already returns false on error). When isJalaali is 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;

Comment on lines +178 to +182
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitivity issue: Format should be format (lowercase). The property format is defined as a lowercase property expression, not Format.

Suggested change
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);

Copilot uses AI. Check for mistakes.
this.isShort = isShort;

Date = date;
CursorPosition = 1;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
CursorPosition = 1;

Copilot uses AI. Check for mistakes.
/// </summary>
/// <param name="date"></param>
public DateField (DateTime date) : base ("")
/// <param name="isJalaali"></param>
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +73 to +78

this.isJalaali = isJalaali;
if (!isJalaali)
this.isShort = true;
Width = FieldLen + 2;

Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copilot uses AI. Check for mistakes.
Comment on lines +474 to +480


var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);


Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
}
result = pc.ToDateTime (year, month, day, 12, 0, 0, 0);
year = result.Year;
month = result.Month;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to month is useless, since its value is never read.

Suggested change
month = result.Month;

Copilot uses AI. Check for mistakes.
result = pc.ToDateTime (year, month, day, 12, 0, 0, 0);
year = result.Year;
month = result.Month;
day = result.Day;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to day is useless, since its value is never read.

Suggested change
day = result.Day;

Copilot uses AI. Check for mistakes.
string sepChar;
string longFormat;
string shortFormat;
bool isJalaali;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field 'isJalaali' can be 'readonly'.

Suggested change
bool isJalaali;
readonly bool isJalaali;

Copilot uses AI. Check for mistakes.
Comment on lines +276 to +277
} catch {
return false;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic catch clause.

Suggested change
} catch {
return false;
} catch (ArgumentOutOfRangeException) {
return false;
} catch (FormatException) {
return false;

Copilot uses AI. Check for mistakes.
Comment on lines +176 to +181
if (isJalaali) {

this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.

Suggested change
if (isJalaali) {
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
this.Text = isJalaali ? ToJalaaliString (Format, date) : value.ToString (Format);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants