Skip to content
69 changes: 55 additions & 14 deletions public/cui.js
Original file line number Diff line number Diff line change
Expand Up @@ -40033,6 +40033,10 @@ CUI.DateTime = (function(superClass) {
add_AD: {
"default": false,
check: Boolean
},
avoid_bc_conversion: {
"default": false,
check: Boolean
}
});
this.removeOpt("getValueForDisplay");
Expand Down Expand Up @@ -40332,7 +40336,7 @@ CUI.DateTime = (function(superClass) {
case "store":
return CUI.DateTime.formatMoment(mom, output_format[_output_format], parseZone);
default:
return CUI.DateTime.formatMomentWithBc(mom, output_format[_output_format]);
return CUI.DateTime.formatMomentWithBc(mom, output_format[_output_format], false, this._avoid_bc_conversion);
}
};

Expand Down Expand Up @@ -40427,7 +40431,7 @@ CUI.DateTime = (function(superClass) {
if (!mom.isValid()) {
return value;
}
return CUI.DateTime.formatMomentWithBc(mom, this.getCurrentFormatDisplay());
return CUI.DateTime.formatMomentWithBc(mom, this.getCurrentFormatDisplay(), false, this._avoid_bc_conversion);
};

DateTime.prototype.getValueForInput = function(v) {
Expand All @@ -40442,7 +40446,7 @@ CUI.DateTime = (function(superClass) {
if (!mom.isValid()) {
return v;
}
return CUI.DateTime.formatMomentWithBc(mom, this.__input_format.input);
return CUI.DateTime.formatMomentWithBc(mom, this.__input_format.input, false, this._avoid_bc_conversion);
};

DateTime.prototype.__checkInput = function(value) {
Expand Down Expand Up @@ -40717,7 +40721,8 @@ CUI.DateTime = (function(superClass) {
if (use_formats == null) {
use_formats = formats;
}
if (!((stringValue != null ? typeof stringValue.trim === "function" ? stringValue.trim().length : void 0 : void 0) > 0)) {
stringValue = stringValue != null ? typeof stringValue.trim === "function" ? stringValue.trim() : void 0 : void 0;
if (!((stringValue != null ? stringValue.length : void 0) > 0)) {
return moment.invalid();
}
for (i = 0, len = formats.length; i < len; i++) {
Expand Down Expand Up @@ -41422,6 +41427,25 @@ CUI.DateTime = (function(superClass) {
return str;
};

DateTime.formatWithoutBC = function(datestr_or_moment, output_format, output_type, parseZone, locale) {
var dt, opts, str;
if (parseZone == null) {
parseZone = false;
}
if (locale == null) {
locale = null;
}
opts = {
avoid_bc_conversion: true
};
if (locale) {
opts.locale = locale;
}
dt = new CUI.DateTime(opts);
str = dt.format(datestr_or_moment, output_format, output_type, parseZone);
return str;
};

DateTime.formatWithInputTypes = function(datestr, output_types, output_format, locale) {
var dt, mom, opts;
if (locale == null) {
Expand Down Expand Up @@ -41457,7 +41481,7 @@ CUI.DateTime = (function(superClass) {
if (!mom.isValid()) {
return null;
}
return this.formatMomentWithBc(mom, dt.getCurrentFormatDisplay(), dt._add_AD);
return this.formatMomentWithBc(mom, dt.getCurrentFormatDisplay(), dt._add_AD, dt._avoid_bc_conversion);
};

DateTime.formatMoment = function(mom, format, parseZone) {
Expand All @@ -41470,11 +41494,17 @@ CUI.DateTime = (function(superClass) {
return mom.format(format);
};

DateTime.formatMomentWithBc = function(mom, format, add_AD) {
DateTime.formatMomentWithBc = function(mom, format, add_AD, avoid_bc_conversion) {
var bc, regexp, replace, v;
if (add_AD == null) {
add_AD = false;
}
if (avoid_bc_conversion == null) {
avoid_bc_conversion = false;
}
if (avoid_bc_conversion) {
return DateTime.formatMoment(mom, format, false);
}
if (mom.year() === 0) {
return "1 " + CUI.DateTime.defaults.bc_appendix_output;
}
Expand Down Expand Up @@ -41515,8 +41545,11 @@ CUI.DateTime = (function(superClass) {
return CUI.DateTimeRangeGrammar.stringToDateRange(string, locale);
};

DateTime.dateRangeToString = function(from, to, locale) {
return CUI.DateTimeRangeGrammar.dateRangeToString(from, to, locale);
DateTime.dateRangeToString = function(from, to, locale, avoid_bc) {
if (avoid_bc == null) {
avoid_bc = false;
}
return CUI.DateTimeRangeGrammar.dateRangeToString(from, to, locale, avoid_bc);
};

DateTime.convertTimeFormatFromGoToMoment = function(goString) {
Expand Down Expand Up @@ -42627,8 +42660,8 @@ CUI.DateTimeRangeGrammar = (function() {

DateTimeRangeGrammar.PARSE_GRAMMARS["en-US"] = [["DATE to DATE", "range", [0, 2], null, "RANGE"], ["YEAR to YEAR", "range", [0, 2], null, "RANGE_YEAR"], ["YEAR - YEAR A.D.", "range", [0, 2]], ["YEAR - YEAR AD", "range", [0, 2]], ["YEAR to YEAR A.D.", "range", [0, 2]], ["YEAR to YEAR AD", "range", [0, 2]], ["from YEAR to YEAR", "range", [1, 3]], ["YEAR BC - YEAR A.D.", "range", [0, 3], [true]], ["YEAR BC - YEAR AD", "range", [0, 3], [true]], ["YEAR BC - YEAR CE", "range", [0, 3], [true]], ["YEAR BC - YEAR ad", "range", [0, 3], [true]], ["YEAR B.C. - YEAR A.D.", "range", [0, 3], [true]], ["YEAR B.C. - YEAR AD", "range", [0, 3], [true]], ["YEAR B.C. - YEAR CE", "range", [0, 3], [true]], ["YEAR B.C. - YEAR ad", "range", [0, 3], [true]], ["YEAR B.C. to YEAR", "range", [0, 3], [true]], ["YEAR bc - YEAR A.D.", "range", [0, 3], [true]], ["YEAR bc - YEAR AD", "range", [0, 3], [true]], ["YEAR bc - YEAR CE", "range", [0, 3], [true]], ["YEAR bc - YEAR ad", "range", [0, 3], [true]], ["YEAR ac - YEAR A.D.", "range", [0, 3], [true]], ["YEAR ac - YEAR AD", "range", [0, 3], [true]], ["YEAR ac - YEAR CE", "range", [0, 3], [true]], ["YEAR ac - YEAR ad", "range", [0, 3], [true]], ["YEAR - YEAR BC", "range", [0, 2], [true, true]], ["YEAR - YEAR B.C.", "range", [0, 2], [true, true]], ["MONTH YEAR", "monthRange", [0, 1]], ["after YEAR", "yearOpenEnd", [1], [false], "AFTER"], ["after YEAR BC", "yearOpenEnd", [1], [true], "AFTER_BC"], ["before YEAR", "yearOpenStart", [1], [false], "BEFORE"], ["before YEAR BC", "yearOpenStart", [1], [true], "BEFORE_BC"], ["around YEAR", "yearRange", [1], [false, true, true], "AROUND"], ["around YEAR BC", "yearRange", [1], [true, true, true], "AROUND_BC"], ["YEAR_DOT millennium", "millennium", [0], null, "MILLENNIUM"], ["YEAR_DOT millennium BC", "millennium", [0], [true], "MILLENNIUM_BC"], ["YEAR millennium", "millennium", [0]], ["YEAR millennium BC", "millennium", [0], [true]], ["YEAR BCE", "getFromTo", [0], [true]], ["YEAR bc", "getFromTo", [0], [true]], ["YEAR BC", "getFromTo", [0], [true]], ["YEAR B.C.", "getFromTo", [0], [true]], ["YEAR AD", "getFromTo", [0]], ["YEAR A.D.", "getFromTo", [0]], ["CENTURY century", "century", [0], null, "CENTURY"], ["CENTURY century BC", "century", [0], [true], "CENTURY_BC"], ["Early CENTURY century", "earlyCentury", [1], null, "EARLY_CENTURY"], ["Early CENTURY century BC", "earlyCentury", [1], [true], "EARLY_CENTURY_BC"], ["Late CENTURY century", "lateCentury", [1], null, "LATE_CENTURY"], ["Late CENTURY century BC", "lateCentury", [1], [true], "LATE_CENTURY_BC"], ["CENTURY century - CENTURY century", "centuryRange", [0, 3], null, "CENTURY_RANGE"], ["CENTURY century BC - CENTURY century", "centuryRange", [0, 4], [true], "CENTURY_RANGE_FROM_BC"], ["CENTURY century B.C. - CENTURY century", "centuryRange", [0, 4], [true]], ["CENTURY century BC - CENTURY century BC", "centuryRange", [0, 4], [true, true], "CENTURY_RANGE_BC"], ["CENTURY century B.C. - CENTURY century B.C.", "centuryRange", [0, 4], [true, true]]];

DateTimeRangeGrammar.dateRangeToString = function(from, to, locale) {
var centerYear, century, format, fromCentury, fromIsYear, fromMoment, fromSplit, fromYear, getPossibleString, grammarKey, grammars, i, isBC, j, k, key, len, len1, len2, millennium, month, possibleString, ref, ref1, ref2, toCentury, toIsYear, toMoment, toSplit, toYear, yearsDifference;
DateTimeRangeGrammar.dateRangeToString = function(from, to, locale, avoid_bc) {
var centerYear, century, format, format_fn, fromCentury, fromIsYear, fromMoment, fromSplit, fromYear, getPossibleString, grammarKey, grammars, i, isBC, j, k, key, len, len1, len2, millennium, month, possibleString, ref, ref1, ref2, toCentury, toIsYear, toMoment, toSplit, toYear, yearsDifference;
if (from == null) {
from = null;
}
Expand All @@ -42638,9 +42671,17 @@ CUI.DateTimeRangeGrammar = (function() {
if (locale == null) {
locale = CUI.DateTime.getLocale();
}
if (avoid_bc == null) {
avoid_bc = false;
}
if (!CUI.util.isString(from) && !CUI.util.isString(to)) {
return;
}
if (avoid_bc) {
format_fn = CUI.DateTime.formatWithoutBC;
} else {
format_fn = CUI.DateTime.format;
}
if (to === "") {
to = null;
}
Expand All @@ -42664,7 +42705,7 @@ CUI.DateTimeRangeGrammar = (function() {
fromYear = fromMoment.year();
}
if (from === to) {
return CUI.DateTime.format(from, "display_short");
return format_fn(from, "display_short");
}
if (CUI.DateTimeRangeGrammar.REGEXP_YEAR.test(to)) {
toIsYear = true;
Expand Down Expand Up @@ -42706,7 +42747,7 @@ CUI.DateTimeRangeGrammar = (function() {
} else if (possibleStringArray[value] === "CENTURY") {
parameters[index] += "th";
} else if (CUI.util.isString(parameters[index])) {
parameters[index] = CUI.DateTime.format(parameters[index], "display_short");
parameters[index] = format_fn(parameters[index], "display_short");
}
possibleStringArray[value] = parameters[index];
}
Expand Down Expand Up @@ -42844,10 +42885,10 @@ CUI.DateTimeRangeGrammar = (function() {
}
if (fromIsYear || toIsYear) {
if (fromIsYear) {
from = CUI.DateTime.format(from, "display_short");
from = format_fn(from, "display_short");
}
if (toIsYear) {
to = CUI.DateTime.format(to, "display_short");
to = format_fn(to, "display_short");
fromSplit = from.split(CUI.DateTimeRangeGrammar.REGEXP_SPACE);
toSplit = to.split(CUI.DateTimeRangeGrammar.REGEXP_SPACE);
if (fromSplit[1] === toSplit[1]) {
Expand Down
2 changes: 1 addition & 1 deletion public/cui.min.js

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions src/elements/DateTime/DateTime.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class CUI.DateTime extends CUI.Input
add_AD:
default: false
check: Boolean
avoid_bc_conversion:
default: false
check: Boolean

@removeOpt("getValueForDisplay")
@removeOpt("getValueForInput")
Expand Down Expand Up @@ -324,7 +327,7 @@ class CUI.DateTime extends CUI.Input
when "store"
return CUI.DateTime.formatMoment(mom, output_format[_output_format], parseZone)
else
return CUI.DateTime.formatMomentWithBc(mom, output_format[_output_format])
return CUI.DateTime.formatMomentWithBc(mom, output_format[_output_format], false, @_avoid_bc_conversion)

regexpMatcher: ->
YYYY:
Expand Down Expand Up @@ -401,7 +404,7 @@ class CUI.DateTime extends CUI.Input
if not mom.isValid()
return value

return CUI.DateTime.formatMomentWithBc(mom, @getCurrentFormatDisplay())
return CUI.DateTime.formatMomentWithBc(mom, @getCurrentFormatDisplay(),false, @_avoid_bc_conversion)

getValueForInput: (v = @getValue()) ->
if CUI.util.isEmpty(v?.trim())
Expand All @@ -411,7 +414,7 @@ class CUI.DateTime extends CUI.Input
if not mom.isValid()
return v

return CUI.DateTime.formatMomentWithBc(mom, @__input_format.input)
return CUI.DateTime.formatMomentWithBc(mom, @__input_format.input,false, @_avoid_bc_conversion)

__checkInput: (value) ->
@__calendarButton.enable()
Expand Down Expand Up @@ -632,7 +635,8 @@ class CUI.DateTime extends CUI.Input
# these formats are the "allowed" formats, this is used in __checkInput

parse: (stringValue, formats = @__input_formats, use_formats = formats) ->
if not (stringValue?.trim?().length > 0)
stringValue = stringValue?.trim?()
if not (stringValue?.length > 0)
return moment.invalid()

for format in formats
Expand Down Expand Up @@ -1451,6 +1455,17 @@ class CUI.DateTime extends CUI.Input
# console.debug "DateTime.format", date, type, output_type, DateTime.__locale, str
str

@formatWithoutBC: (datestr_or_moment, output_format, output_type, parseZone = false, locale = null) ->
opts = {
avoid_bc_conversion: true
}
if locale
opts.locale = locale
dt = new CUI.DateTime(opts)
str = dt.format(datestr_or_moment, output_format, output_type, parseZone)
# console.debug "DateTime.format", date, type, output_type, DateTime.__locale, str
str

# limit output to the given types
# the library is very awkward here...
@formatWithInputTypes: (datestr, output_types, output_format, locale = null) ->
Expand Down Expand Up @@ -1481,7 +1496,7 @@ class CUI.DateTime extends CUI.Input
if not mom.isValid()
return null

@formatMomentWithBc(mom, dt.getCurrentFormatDisplay(), dt._add_AD)
@formatMomentWithBc(mom, dt.getCurrentFormatDisplay(), dt._add_AD, dt._avoid_bc_conversion)

@formatMoment: (mom, format, parseZone) ->
if mom.bc
Expand All @@ -1493,7 +1508,10 @@ class CUI.DateTime extends CUI.Input
return mom.format(format)

# BC appendix always adds one year. Therefore year 0 is 1 BC.
@formatMomentWithBc: (mom, format, add_AD = false) ->
@formatMomentWithBc: (mom, format, add_AD = false, avoid_bc_conversion = false) ->
if avoid_bc_conversion
return DateTime.formatMoment(mom, format, false)

if mom.year() == 0
return "1 #{CUI.DateTime.defaults.bc_appendix_output}"

Expand All @@ -1514,7 +1532,6 @@ class CUI.DateTime extends CUI.Input
regexp = new RegExp(replace);
return v.replace(regexp, ""+mom.year())


mom.subtract(1, "year")
v = mom.format(format) + " " + CUI.DateTime.defaults.bc_appendix_output
# remove the "-" and all possible zeros.
Expand All @@ -1531,8 +1548,8 @@ class CUI.DateTime extends CUI.Input
@stringToDateRange: (string, locale) ->
return CUI.DateTimeRangeGrammar.stringToDateRange(string, locale)

@dateRangeToString: (from, to, locale) ->
return CUI.DateTimeRangeGrammar.dateRangeToString(from, to, locale)
@dateRangeToString: (from, to, locale, avoid_bc = false) ->
return CUI.DateTimeRangeGrammar.dateRangeToString(from, to, locale, avoid_bc)


@convertTimeFormatFromGoToMoment: (goString) ->
Expand Down
16 changes: 11 additions & 5 deletions src/elements/DateTime/DateTimeRangeGrammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,16 @@ class CUI.DateTimeRangeGrammar
["CENTURY century B.C. - CENTURY century B.C.", "centuryRange", [0, 4], [true, true]]
]

@dateRangeToString: (from = null, to = null, locale = CUI.DateTime.getLocale()) ->
@dateRangeToString: (from = null, to = null, locale = CUI.DateTime.getLocale(), avoid_bc = false) ->

if not CUI.util.isString(from) and not CUI.util.isString(to)
return

if avoid_bc
format_fn = CUI.DateTime.formatWithoutBC
else
format_fn = CUI.DateTime.format

# In case that to or from are an empty string, force it to be null, otherwise the date check will fail.
if to == ""
to = null
Expand All @@ -290,7 +296,7 @@ class CUI.DateTimeRangeGrammar
fromYear = fromMoment.year()

if from == to
return CUI.DateTime.format(from, "display_short")
return format_fn(from, "display_short")

if CUI.DateTimeRangeGrammar.REGEXP_YEAR.test(to)
toIsYear = true
Expand Down Expand Up @@ -328,7 +334,7 @@ class CUI.DateTimeRangeGrammar
else if possibleStringArray[value] == "CENTURY"
parameters[index] += "th"
else if CUI.util.isString(parameters[index])
parameters[index] = CUI.DateTime.format(parameters[index], "display_short")
parameters[index] = format_fn(parameters[index], "display_short",)
possibleStringArray[value] = parameters[index]

possibleString = possibleStringArray.join(" ")
Expand Down Expand Up @@ -447,9 +453,9 @@ class CUI.DateTimeRangeGrammar

if fromIsYear or toIsYear
if fromIsYear
from = CUI.DateTime.format(from, "display_short")
from = format_fn(from, "display_short")
if toIsYear
to = CUI.DateTime.format(to, "display_short")
to = format_fn(to, "display_short")

# Removes the 'BC' / v. Chr. from 'from' to only show it in the end. For example: 15 - 10 v. Chr.
fromSplit = from.split(CUI.DateTimeRangeGrammar.REGEXP_SPACE)
Expand Down