mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Wikibase: Refactor wbase prop_writer to static functions
This commit is contained in:
@@ -31,12 +31,7 @@ public class Wbase_claim_entity extends Wbase_claim_base {
|
||||
public String Entity_tid_str() {return Wbase_claim_entity_type_.Reg.Get_str_or_fail(entity_tid);}
|
||||
public byte[] Entity_tid_bry() {return Wbase_claim_entity_type_.Reg.Get_bry_or_fail(entity_tid);}
|
||||
|
||||
public byte[] Page_ttl_db() {
|
||||
return entity_tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_db, entity_id_bry)
|
||||
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, entity_id_bry)
|
||||
;
|
||||
}
|
||||
public byte[] Page_ttl_db() {return To_xid__db(entity_tid, entity_id_bry);}
|
||||
public byte[] Page_ttl_gui() {
|
||||
return entity_tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_gui, entity_id_bry)
|
||||
@@ -47,4 +42,11 @@ public class Wbase_claim_entity extends Wbase_claim_base {
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), this.Entity_tid_str(), Int_.To_str(entity_id));
|
||||
}
|
||||
|
||||
public static byte[] To_xid__db(byte tid, byte[] bry) { // EX: 'item,2' -> q2; 'property,2' -> Property:P2
|
||||
return tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_db, bry)
|
||||
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, bry)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,16 +39,18 @@ public class Wbase_claim_quantity extends Wbase_claim_base {
|
||||
if (lbound_as_num == null) lbound_as_num = To_decimal("lower", lbound);
|
||||
return lbound_as_num;
|
||||
} private Decimal_adp lbound_as_num;
|
||||
private Decimal_adp To_decimal(String type, byte[] v) {
|
||||
if (v == null) throw Err_.new_("wbase", "value is null", "type", type);
|
||||
int len = v.length; if (len == 0) throw Err_.new_("wbase", "value is empty", "type", type);
|
||||
if (v[0] == Byte_ascii.Plus) v = Bry_.Mid(v, 1);
|
||||
return Decimal_adp_.parse(String_.new_a7(v));
|
||||
}
|
||||
|
||||
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_quantity(this);}
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(amount), String_.new_u8(unit), String_.new_u8(ubound), String_.new_u8(lbound));
|
||||
}
|
||||
|
||||
public static final byte[] Unit_1 = Bry_.new_a7("1");
|
||||
public static Decimal_adp To_decimal(String name, byte[] bry) {
|
||||
if (bry == null) throw Err_.new_wo_type("wbase.claim: value is null", "name", name);
|
||||
int len = bry.length;
|
||||
if (len == 0) throw Err_.new_wo_type("wbase.claim: value is empty", "name", name);
|
||||
if (bry[0] == Byte_ascii.Plus) bry = Bry_.Mid(bry, 1);
|
||||
return Decimal_adp_.parse(String_.new_a7(bry));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.xtns.wbases.claims.itms; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*;
|
||||
import gplx.xowa.xtns.wbases.claims.enums.*;
|
||||
public class Wbase_claim_string extends Wbase_claim_base {
|
||||
public Wbase_claim_string(int pid, byte snak_tid, byte[] val) {super(pid, snak_tid);
|
||||
this.val = val;
|
||||
public Wbase_claim_string(int pid, byte snak_tid, byte[] val_bry) {super(pid, snak_tid);
|
||||
this.val_bry = val_bry;
|
||||
}
|
||||
@Override public byte Val_tid() {return Wbase_claim_type_.Tid__string;}
|
||||
public byte[] Val_str() {return val;} private final byte[] val;
|
||||
public byte[] Val_bry() {return val_bry;} private final byte[] val_bry;
|
||||
|
||||
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_str(this);}
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(val));
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(val_bry));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
public void Calendar_ttl_(byte[] v) {calendar_ttl = v;}
|
||||
|
||||
public Wbase_date Time_as_date() {
|
||||
if (time_as_date == null) time_as_date = Wbase_date.Parse(time, this.Precision_int(), this.Before_int(), this.After_int(), this.Calendar_is_julian());
|
||||
if (time_as_date == null) time_as_date = Wbase_date_.Parse(time, this.Precision_int(), this.Before_int(), this.After_int(), this.Calendar_is_julian());
|
||||
return time_as_date;
|
||||
} private Wbase_date time_as_date;
|
||||
public int Precision_int() {
|
||||
@@ -74,10 +74,10 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
boolean calendar_is_julian = this.Calendar_is_julian();
|
||||
byte[] calendar_display = null;
|
||||
if (calendar_is_julian) {
|
||||
date = Wbase_date.Xto_julian(date);
|
||||
date = Wbase_date_.To_julian(date);
|
||||
calendar_display = msgs.Time_julian();
|
||||
}
|
||||
Wbase_date.Xto_str(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date_.To_bfr(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
if (calendar_display != null)
|
||||
bfr.Add_byte_space().Add(calendar_display);
|
||||
} catch (Exception e) {
|
||||
@@ -89,10 +89,10 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
try {
|
||||
byte[] calendar_display = null;
|
||||
if (calendar_is_julian) {
|
||||
date = Wbase_date.Xto_julian(date);
|
||||
date = Wbase_date_.To_julian(date);
|
||||
calendar_display = msgs.Time_julian();
|
||||
}
|
||||
Wbase_date.Xto_str(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date_.To_bfr(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
if (calendar_display != null)
|
||||
bfr.Add_byte_space().Add(calendar_display);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -16,8 +16,6 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.wbases.claims.itms.times; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.xtns.wbases.hwtrs.*;
|
||||
public class Wbase_date {
|
||||
public Wbase_date(long year, int month, int day, int hour, int minute, int second, int precision, int before, int after, boolean calendar_is_julian) {
|
||||
this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second;
|
||||
@@ -33,151 +31,6 @@ public class Wbase_date {
|
||||
public int Before() {return before;} private final int before;
|
||||
public int After() {return after;} private final int after;
|
||||
public boolean Calendar_is_julian() {return calendar_is_julian;} private final boolean calendar_is_julian;
|
||||
public static Wbase_date Parse(byte[] date, int precision, int before, int after, boolean calendar_is_julian) {// EX:+00000002001-02-03T04:05:06Z
|
||||
int year_sign = 1;
|
||||
switch (date[0]) {
|
||||
case Byte_ascii.Plus: break;
|
||||
case Byte_ascii.Dash: year_sign = -1; break;
|
||||
default: throw Err_.new_unhandled(date[0]);
|
||||
}
|
||||
int year_end = Bry_find_.Find_fwd(date, Byte_ascii.Dash, 1);
|
||||
long year = Long_.parse_or(String_.new_a7(date, 1, year_end), -1); if (year == -1) throw Err_.new_wo_type("parse failed", "raw", String_.new_a7(date));
|
||||
int month = Bry_.To_int_or(date, year_end + 1, year_end + 3, -1);
|
||||
int day = Bry_.To_int_or(date, year_end + 4, year_end + 6, -1);
|
||||
int hour = Bry_.To_int_or(date, year_end + 7, year_end + 9, -1);
|
||||
int minute = Bry_.To_int_or(date, year_end + 10, year_end + 12, -1);
|
||||
int second = Bry_.To_int_or(date, year_end + 13, year_end + 15, -1);
|
||||
return new Wbase_date(year * year_sign, month, day, hour, minute, second, precision, before, after, calendar_is_julian);
|
||||
}
|
||||
public static Wbase_date Xto_julian(Wbase_date date) {
|
||||
int a = (int)Math_.Floor((14 - date.Month() / 12));
|
||||
int y = (int)date.Year() + 4800 - a;
|
||||
int m = date.Month() + 12 * a - 3;
|
||||
int julian = date.Day() + (int)Math_.Floor((153 * m + 2) / 5) + 365 * y + (int)Math_.Floor(y / 4) - (int)Math_.Floor(y / 100) + (int)Math_.Floor(y / 400) - 32045;
|
||||
int c = julian + 32082;
|
||||
int d = (int)Math_.Floor((4 * c + 3) / 1461);
|
||||
int e = c - (int)Math_.Floor((1461 * d) / 4);
|
||||
int n = (int)Math_.Floor((5 * e + 2) / 153);
|
||||
int new_y = d - 4800 + (int)Math_.Floor(n / 10);
|
||||
int new_m = n + 3 - 12 * (int)Math_.Floor(n / 10);
|
||||
int new_d = e - (int)Math_.Floor((153 * n + 2) / 5) + 1;
|
||||
return new Wbase_date(new_y, new_m, new_d, date.Hour(), date.Minute(), date.Second(), date.precision, date.before, date.after, date.calendar_is_julian);
|
||||
}
|
||||
public static void Xto_str(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
boolean calendar_is_julian = date.calendar_is_julian;
|
||||
if (calendar_is_julian)
|
||||
date = Xto_julian(date);
|
||||
long year = date.Year();
|
||||
int months_bgn = msgs.Month_bgn_idx();
|
||||
byte[][] months = msgs.Ary();
|
||||
int precision = date.precision;
|
||||
byte[] time_spr = msgs.Sym_time_spr();
|
||||
switch (precision) {
|
||||
case Wbase_date.Fmt_ym: // EX: "Feb 2001"
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(year);
|
||||
break;
|
||||
case Wbase_date.Fmt_ymd: // EX: "3 Feb 2001"
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdh: // EX: "4:00 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(0, 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdhn: // EX: "4:05 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
default:
|
||||
if (precision <= 9) // y, round to (9 - prec)
|
||||
Xto_str_fmt_y(bfr, tmp_fmtr, tmp_bfr, msgs, date, precision);
|
||||
else { // EX: "4:05:06 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Second(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (calendar_is_julian)
|
||||
bfr.Add(msgs.Time_julian());
|
||||
Xto_str_beforeafter(bfr, tmp_fmtr, tmp_bfr, msgs, date);
|
||||
}
|
||||
private static void Xto_str_beforeafter(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
byte[] bry = null;
|
||||
int before = date.before;
|
||||
int after = date.after;
|
||||
if (before == 0) {
|
||||
if (after != 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
else {
|
||||
if (after == 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else if (before == after)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plusminus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).Add(msgs.Sym_list_comma()).Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
if (bry != null) {
|
||||
bry = tmp_fmtr.Fmt_(msgs.Sym_fmt_parentheses()).Bld_bry_many(tmp_bfr, bry);
|
||||
bfr.Add_byte_space().Add(bry);
|
||||
}
|
||||
}
|
||||
private static void Xto_str_fmt_y(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date, int precision) {
|
||||
int year_pow = 9 - precision;
|
||||
byte[] year_fmt = msgs.Ary()[msgs.Time_year_idx() + year_pow];
|
||||
long year = date.Year();
|
||||
byte[] repl_fmt = null;
|
||||
if (year <= 0) { // negative
|
||||
if (year_pow < 4) // negative years < 999 get "BC"
|
||||
repl_fmt = msgs.Time_relative_bc();
|
||||
else // negative years > 999 get "ago"
|
||||
repl_fmt = msgs.Time_relative_ago();
|
||||
}
|
||||
else {
|
||||
if (year_pow > 4) // positive years > 999 get "in time"
|
||||
repl_fmt = msgs.Time_relative_in();
|
||||
}
|
||||
if (repl_fmt != null)
|
||||
year_fmt = tmp_fmtr.Fmt_(repl_fmt).Bld_bry_many(tmp_bfr, year_fmt);
|
||||
if (year <= 0)
|
||||
year *= -1; // convert negative to positive; note that negative year will be reported with "BC" / "ago"
|
||||
switch (year_pow) {
|
||||
case 0: break; // noop
|
||||
default:
|
||||
year = (int)(year / Math_.Pow(10, year_pow));
|
||||
break;
|
||||
}
|
||||
byte[] year_bry = tmp_fmtr.Fmt_(year_fmt).Bld_bry_many(tmp_bfr, year);
|
||||
bfr.Add(year_bry);
|
||||
}
|
||||
public static final int
|
||||
Fmt_y = 9
|
||||
, Fmt_ym = 10
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.wbases.claims.itms.times; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.xtns.wbases.hwtrs.*;
|
||||
public class Wbase_date_ {
|
||||
public static Wbase_date Parse(byte[] date, int precision, int before, int after, boolean calendar_is_julian) {// EX:+00000002001-02-03T04:05:06Z
|
||||
int year_sign = 1;
|
||||
switch (date[0]) {
|
||||
case Byte_ascii.Plus: break;
|
||||
case Byte_ascii.Dash: year_sign = -1; break;
|
||||
default: throw Err_.new_unhandled(date[0]);
|
||||
}
|
||||
int year_end = Bry_find_.Find_fwd(date, Byte_ascii.Dash, 1);
|
||||
long year = Long_.parse_or(String_.new_a7(date, 1, year_end), -1); if (year == -1) throw Err_.new_wo_type("parse failed", "raw", String_.new_a7(date));
|
||||
int month = Bry_.To_int_or(date, year_end + 1, year_end + 3, -1);
|
||||
int day = Bry_.To_int_or(date, year_end + 4, year_end + 6, -1);
|
||||
int hour = Bry_.To_int_or(date, year_end + 7, year_end + 9, -1);
|
||||
int minute = Bry_.To_int_or(date, year_end + 10, year_end + 12, -1);
|
||||
int second = Bry_.To_int_or(date, year_end + 13, year_end + 15, -1);
|
||||
return new Wbase_date(year * year_sign, month, day, hour, minute, second, precision, before, after, calendar_is_julian);
|
||||
}
|
||||
public static Wbase_date To_julian(Wbase_date date) {
|
||||
int a = (int)Math_.Floor((14 - date.Month() / 12));
|
||||
int y = (int)date.Year() + 4800 - a;
|
||||
int m = date.Month() + 12 * a - 3;
|
||||
int julian = date.Day() + (int)Math_.Floor((153 * m + 2) / 5) + 365 * y + (int)Math_.Floor(y / 4) - (int)Math_.Floor(y / 100) + (int)Math_.Floor(y / 400) - 32045;
|
||||
int c = julian + 32082;
|
||||
int d = (int)Math_.Floor((4 * c + 3) / 1461);
|
||||
int e = c - (int)Math_.Floor((1461 * d) / 4);
|
||||
int n = (int)Math_.Floor((5 * e + 2) / 153);
|
||||
int new_y = d - 4800 + (int)Math_.Floor(n / 10);
|
||||
int new_m = n + 3 - 12 * (int)Math_.Floor(n / 10);
|
||||
int new_d = e - (int)Math_.Floor((153 * n + 2) / 5) + 1;
|
||||
return new Wbase_date(new_y, new_m, new_d, date.Hour(), date.Minute(), date.Second(), date.Precision(), date.Before(), date.After(), date.Calendar_is_julian());
|
||||
}
|
||||
public static void To_bfr(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
boolean calendar_is_julian = date.Calendar_is_julian();
|
||||
if (calendar_is_julian) date = To_julian(date);
|
||||
long year = date.Year();
|
||||
int months_bgn = msgs.Month_bgn_idx();
|
||||
byte[][] months = msgs.Ary();
|
||||
int precision = date.Precision();
|
||||
byte[] time_spr = msgs.Sym_time_spr();
|
||||
switch (precision) {
|
||||
case Wbase_date.Fmt_ym: // EX: "Feb 2001"
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(year);
|
||||
break;
|
||||
case Wbase_date.Fmt_ymd: // EX: "3 Feb 2001"
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdh: // EX: "4:00 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(0, 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdhn: // EX: "4:05 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
default:
|
||||
if (precision <= 9) // y, round to (9 - prec)
|
||||
Xto_str_fmt_y(bfr, tmp_fmtr, tmp_bfr, msgs, date, precision);
|
||||
else { // EX: "4:05:06 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Second(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (calendar_is_julian)
|
||||
bfr.Add(msgs.Time_julian());
|
||||
Xto_str_beforeafter(bfr, tmp_fmtr, tmp_bfr, msgs, date);
|
||||
}
|
||||
private static void Xto_str_beforeafter(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
byte[] bry = null;
|
||||
int before = date.Before();
|
||||
int after = date.After();
|
||||
if (before == 0) {
|
||||
if (after != 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
else {
|
||||
if (after == 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else if (before == after)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plusminus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).Add(msgs.Sym_list_comma()).Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
if (bry != null) {
|
||||
bry = tmp_fmtr.Fmt_(msgs.Sym_fmt_parentheses()).Bld_bry_many(tmp_bfr, bry);
|
||||
bfr.Add_byte_space().Add(bry);
|
||||
}
|
||||
}
|
||||
private static void Xto_str_fmt_y(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date, int precision) {
|
||||
int year_pow = 9 - precision;
|
||||
byte[] year_fmt = msgs.Ary()[msgs.Time_year_idx() + year_pow];
|
||||
long year = date.Year();
|
||||
byte[] repl_fmt = null;
|
||||
if (year <= 0) { // negative
|
||||
if (year_pow < 4) // negative years < 999 get "BC"
|
||||
repl_fmt = msgs.Time_relative_bc();
|
||||
else // negative years > 999 get "ago"
|
||||
repl_fmt = msgs.Time_relative_ago();
|
||||
}
|
||||
else {
|
||||
if (year_pow > 4) // positive years > 999 get "in time"
|
||||
repl_fmt = msgs.Time_relative_in();
|
||||
}
|
||||
if (repl_fmt != null)
|
||||
year_fmt = tmp_fmtr.Fmt_(repl_fmt).Bld_bry_many(tmp_bfr, year_fmt);
|
||||
if (year <= 0)
|
||||
year *= -1; // convert negative to positive; note that negative year will be reported with "BC" / "ago"
|
||||
switch (year_pow) {
|
||||
case 0: break; // noop
|
||||
default:
|
||||
year = (int)(year / Math_.Pow(10, year_pow));
|
||||
break;
|
||||
}
|
||||
byte[] year_bry = tmp_fmtr.Fmt_(year_fmt).Bld_bry_many(tmp_bfr, year);
|
||||
bfr.Add(year_bry);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ class Wbase_date_fxt {
|
||||
public int Init_before() {return init_before;} public Wbase_date_fxt Init_before_(int v) {init_before = v; return this;} private int init_before;
|
||||
public int Init_after() {return init_after;} public Wbase_date_fxt Init_after_(int v) {init_after = v; return this;} private int init_after;
|
||||
public void Test_parse(String raw, long expd_y, int expd_m, int expd_d, int expd_h, int expd_n, int expd_s) {
|
||||
Wbase_date actl_date = Wbase_date.Parse(Bry_.new_a7(raw), Wbase_date.Fmt_ymdhns, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date actl_date = Wbase_date_.Parse(Bry_.new_a7(raw), Wbase_date.Fmt_ymdhns, init_before, init_after, init_calendar_is_julian);
|
||||
Tfds.Eq(expd_y, actl_date.Year());
|
||||
Tfds.Eq(expd_m, actl_date.Month());
|
||||
Tfds.Eq(expd_d, actl_date.Day());
|
||||
@@ -78,15 +78,15 @@ class Wbase_date_fxt {
|
||||
}
|
||||
public void Test_julian(int[] orig_ary, int[] expd) {
|
||||
Wbase_date orig = new Wbase_date(orig_ary[0], orig_ary[1], orig_ary[2], 0, 0, 0, 0, 0, 0, init_calendar_is_julian);
|
||||
Wbase_date actl = Wbase_date.Xto_julian(orig);
|
||||
Wbase_date actl = Wbase_date_.To_julian(orig);
|
||||
Tfds.Eq(expd[0], (int)actl.Year(), "y");
|
||||
Tfds.Eq(expd[1], actl.Month(), "m");
|
||||
Tfds.Eq(expd[2], actl.Day(), "d");
|
||||
}
|
||||
public void Test_xto_str(String raw, int precision, String expd) {
|
||||
if (msgs == null) msgs = Wdata_hwtr_msgs.new_en_();
|
||||
Wbase_date date = Wbase_date.Parse(Bry_.new_a7(raw), precision, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date.Xto_str(tmp_bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date date = Wbase_date_.Parse(Bry_.new_a7(raw), precision, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date_.To_bfr(tmp_bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Tfds.Eq(expd, tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user