-
Notifications
You must be signed in to change notification settings - Fork 717
Jalaali (Persian) Date input support #1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inamvar
wants to merge
4
commits into
gui-cs:v1_develop
Choose a base branch
from
inamvar:JalaaliDateInput
base: v1_develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ | |
// | ||
// Licensed under the MIT license | ||
// | ||
using NStack; | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using NStack; | ||
|
||
namespace Terminal.Gui { | ||
/// <summary> | ||
|
@@ -25,6 +25,7 @@ public class DateField : TextField { | |
string sepChar; | ||
string longFormat; | ||
string shortFormat; | ||
bool isJalaali; | ||
|
||
int fieldLen => isShort ? shortFieldLen : longFieldLen; | ||
string format => isShort ? shortFormat : longFormat; | ||
|
@@ -47,9 +48,14 @@ public class DateField : TextField { | |
/// <param name="y">The y coordinate.</param> | ||
/// <param name="date">Initial date contents.</param> | ||
/// <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. Choose a reason for hiding this commentThe 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. |
||
{ | ||
|
||
this.isShort = isShort; | ||
this.isJalaali = isJalaali; | ||
Initialize (date, isShort); | ||
|
||
} | ||
|
||
/// <summary> | ||
|
@@ -61,19 +67,28 @@ public DateField () : this (DateTime.MinValue) { } | |
/// Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Computed"/> layout. | ||
/// </summary> | ||
/// <param name="date"></param> | ||
public DateField (DateTime date) : base ("") | ||
/// <param name="isJalaali"></param> | ||
public DateField (DateTime date, bool isJalaali = false) : base ("") | ||
{ | ||
Width = fieldLen + 2; | ||
|
||
this.isJalaali = isJalaali; | ||
if (!isJalaali) | ||
this.isShort = true; | ||
Width = FieldLen + 2; | ||
|
||
Initialize (date); | ||
} | ||
|
||
void Initialize (DateTime date, bool isShort = false) | ||
{ | ||
CultureInfo cultureInfo = CultureInfo.CurrentCulture; | ||
sepChar = cultureInfo.DateTimeFormat.DateSeparator; | ||
longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern); | ||
shortFormat = GetShortFormat (longFormat); | ||
longFormat = isJalaali ? $" yyyy{sepChar}MM{sepChar}dd" : GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern); | ||
shortFormat = isJalaali ? $" yy{sepChar}MM{sepChar}dd" : GetShortFormat (longFormat); | ||
CursorPosition = 1; | ||
|
||
this.isShort = isShort; | ||
|
||
Date = date; | ||
CursorPosition = 1; | ||
TextChanged += DateField_Changed; | ||
|
@@ -109,8 +124,10 @@ void Initialize (DateTime date, bool isShort = false) | |
void DateField_Changed (ustring e) | ||
{ | ||
try { | ||
|
||
if (!DateTime.TryParseExact (GetDate (Text).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) | ||
Text = e; | ||
|
||
} catch (Exception) { | ||
Text = e; | ||
} | ||
|
@@ -155,14 +172,23 @@ public DateTime Date { | |
|
||
var oldData = date; | ||
date = value; | ||
this.Text = value.ToString (format); | ||
var args = new DateTimeEventArgs<DateTime> (oldData, value, format); | ||
|
||
if (isJalaali) { | ||
|
||
this.Text = ToJalaaliString (Format, date); | ||
} else { | ||
this.Text = value.ToString (Format); | ||
} | ||
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format); | ||
|
||
if (oldData != value) { | ||
OnDateChanged (args); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// Get or set the date format for the widget. | ||
/// </summary> | ||
|
@@ -236,7 +262,27 @@ bool SetText (ustring text) | |
vals [idx] = day.ToString (); | ||
} else | ||
day = Int32.Parse (vals [idx].ToString ()); | ||
string d = GetDate (month, day, year, frm); | ||
DateTime result; | ||
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) | ||
|
@@ -420,6 +466,28 @@ public virtual void OnDateChanged (DateTimeEventArgs<DateTime> args) | |
{ | ||
DateChanged?.Invoke (args); | ||
} | ||
|
||
string ToJalaaliString (string format, DateTime date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should have a unit test for this. |
||
{ | ||
if (string.IsNullOrEmpty (format)) format = " yyyy/MM/dd"; | ||
PersianCalendar pc = new PersianCalendar (); | ||
|
||
|
||
var year = pc.GetYear (date); | ||
var month = pc.GetMonth (date); | ||
var day = pc.GetDayOfMonth (date); | ||
|
||
|
||
format = format.Replace ("yyyy", year.ToString (CultureInfo.InvariantCulture)); | ||
format = format.Replace ("yy", (year % 100).ToString ("00", CultureInfo.InvariantCulture)); | ||
format = format.Replace ("MM", month.ToString ("00", CultureInfo.InvariantCulture)); | ||
format = format.Replace ("M", month.ToString (CultureInfo.InvariantCulture)); | ||
format = format.Replace ("dd", day.ToString ("00", CultureInfo.InvariantCulture)); | ||
format = format.Replace ("d", day.ToString (CultureInfo.InvariantCulture)); | ||
|
||
return format; | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.