ParserFunctions: Set day to last day of month if only Year-Feb passed [#644]

staging
gnosygnu 4 years ago
parent 2dee585fc1
commit 374de22699

@ -66,11 +66,13 @@ public class DateAdp_ implements Gfo_invk {
public static DateAdp dateTime_obj_(Object v) {return new DateAdp((GregorianCalendar)v);}
public static final DateAdp_ Gfs = new DateAdp_();
public static int DaysInMonth(DateAdp date) {
int rv = DaysInMonth_ary[date.Month() - Int_.Base1];
if (rv == 28 && IsLeapYear(date.Year())) rv = 29;
public static int DaysInMonth(DateAdp date) {return DaysInMonth(date.Month(), date.Year());}
public static int DaysInMonth(int month, int year) {
int rv = DaysInMonth_ary[month - Int_.Base1];
if (rv == 28 && IsLeapYear(year)) rv = 29;
return rv;
} static int [] DaysInMonth_ary = {31,28,31,30,31,30,31,31,30,31,30,31};
}
private static int [] DaysInMonth_ary = {31,28,31,30,31,30,31,31,30,31,30,31};
public static boolean IsLeapYear(int year) {
if (year % 4 != 0) return false;
else if (year % 400 == 0) return true;

@ -22,4 +22,11 @@ public class Pft_func_time__uncommon__tst {
@Test public void Year__5_digits() {fxt.Test_parse_tmpl_str_test("{{#time:Y-m-d|00123-4-5}}" , "{{test}}" , "2003-04-05");} // PURPOSE: emulate PHP's incorrect date parsing; EX:ca.w:Nicolau_de_Mira; DATE:2014-04-17
@Test public void Year__multiple() {fxt.Test_parse_tmpl_str_test("{{#time:Y-m-d|12 November 2016 2008}}" , "{{test}}" , "2016-11-12");} // PURPOSE: multiple years should take 1st, not last; EX:en.w:Antipas,_Cotabato; DATE:2017-04-01
@Test public void Day__multiple() {fxt.Test_parse_tmpl_str_test("{{#time:Y-m-d|2 12 November 2016}}" , "{{test}}" , "<strong class=\"error\">Invalid month: 12</strong>");} // PURPOSE: multiple days should generate error, not create a real date; EX:en.w:Antipas,_Cotabato; DATE:2017-04-01
@Test public void Month_max() { // ISSUE#:644 DATE:2020-01-12
// NOTE: {{#time}} initializes to today's date; set day to something greater than Feb's 28/29
Datetime_now.Manual_(DateAdp_.new_(2019, 12, 31, 0, 1, 2, 3));
// now parse February (28/29 days) using 31-day date above
fxt.Test__parse__tmpl_to_html("{{#time:F|2000-02}}", "February"); // failed with invalid time b/c it was setting 2000-02-31
}
}

@ -19,6 +19,7 @@ public class Pxd_date_bldr {
public Pxd_date_bldr(int year, int month, int day, int hour, int minute, int second, int frac) {
this.seg_ary = new int[] {year, month, day, hour, minute, second, frac};
}
public int Seg_get(int idx) {return seg_ary[idx];}
// most workers will just set individual segments
public void Seg_set(int idx, int val) {

@ -66,6 +66,13 @@ class Pxd_itm_int extends Pxd_itm_base implements Pxd_itm_int_interface {
}
else {
if (seg_idx == -1) return false; // PAGE:New_York_City EX: March 14, 2,013; DATE:2016-07-06
if (seg_idx == DateAdp_.SegIdx_month) {
// NOTE: if day > month, set it to month's max; ISSUE#:644; DATE:2020-01-12
int day = bldr.Seg_get(DateAdp_.SegIdx_day);
int daysinmonth = DateAdp_.DaysInMonth(val, bldr.Seg_get(DateAdp_.SegIdx_year));
if (day > daysinmonth)
bldr.Seg_set(DateAdp_.SegIdx_day, daysinmonth);
}
bldr.Seg_set(seg_idx, val);
}
}

Loading…
Cancel
Save