public FormattedString Format(double value, RelativeTimeUnit parsedUnit, out string[] units) { EnsureInitialized(); CldrRelativeTimeFormat formatter = CldrRelativeTimeFormat.Resolve(this.Locale, parsedUnit, this.Style); return(formatter.Format(value, this.NumberFormat, this.Numeric, out units)); }
private CldrRelativeTimeFormat(string locale, string type) { int pos = type.IndexOf('-'); this.unit = IntlProviderOptions.ParseEnum <RelativeTimeUnit>(pos < 0 ? type : type.Substring(0, pos)); this.locale = locale; }
/// <summary>Apply a relative time token to the a UTC date for 2000-01-01 using a new <see cref="ArithmeticTimePlugin"/> instance, and return a string representation of the resulting date.</summary> /// <param name="value">The relative time multiplier.</param> /// <param name="unit">The relative time unit.</param> protected string TryApply(int value, RelativeTimeUnit unit) { DateTime date = new DateTime(2000, 1, 1); TimeToken token = new TimeToken(ArithmeticTimePlugin.Key, null, value.ToString(CultureInfo.InvariantCulture), unit); DateTime? result = new ArithmeticTimePlugin().TryApply(token, date); return(TestHelpers.GetRepresentation(result)); }
private static string GetLdmlFieldType(RelativeTimeUnit unit, RelativeTimeStyle style) { string str = IntlProviderOptions.ToStringValue(unit); if (style == RelativeTimeStyle.Long) { return(str); } return(str + "-" + IntlProviderOptions.ToStringValue(style)); }
public FormattedString Format(EcmaValue value, EcmaValue unit, out string[] units) { EcmaValue number = value.ToNumber(); string unitStr = unit.ToStringOrThrow(); if (number.IsNaN || !number.IsFinite) { throw new EcmaRangeErrorException("Value need to be finite number for Intl.RelativeTimeFormat.prototype.format()"); } if (singularForms.TryGetValue(unitStr, out string singular)) { unitStr = singular; } double doubleValue = number.ToDouble(); RelativeTimeUnit parsedUnit = IntlProviderOptions.ParseEnum <RelativeTimeUnit>(unitStr); return(Format(doubleValue, parsedUnit, out units)); }
/********* ** Public methods *********/ /// <summary>Scan the front of an input string to read a set of matching tokens.</summary> /// <param name="input">The natural time format string.</param> /// <returns>Returns a set of matching tokens, or an empty collection if no supported token was found.</returns> public IEnumerable <TimeToken> Tokenize(string input) { // parse input Match match = this.ParsePattern.Match(input); if (!match.Success) { yield break; } // extract tokens int patternCount = match.Groups["expression"].Captures.Count; for (int i = 0; i < patternCount; i++) { // extract parts string expression = match.Groups["expression"].Captures[i].Value; string sign = match.Groups["sign"].Captures[i].Value; string rawValue = match.Groups["value"].Captures[i].Value; string rawUnit = match.Groups["unit"].Captures[i].Value; bool negate = match.Groups["negate"].Captures[i].Value.Length > 0; // parse value int value = string.IsNullOrWhiteSpace(rawValue) ? 1 : int.Parse(rawValue); if (sign == "-") { value *= -1; } if (negate) { value *= -1; // note: double-negation (like -1 year ago) is valid } // parse unit RelativeTimeUnit unit = this.ParseUnit(rawUnit); if (unit == RelativeTimeUnit.Unknown) { yield break; // unsupported unit } // return token yield return(new TimeToken(ArithmeticTimePlugin.Key, expression, value.ToString(CultureInfo.InvariantCulture), unit)); } }
/// <summary>Apply a natural time token to a date value.</summary> /// <param name="token">The natural time token to apply.</param> /// <param name="date">The date value to apply the token to.</param> /// <returns>Returns the modified date, or <c>null</c> if the token is not supported.</returns> public DateTime?TryApply(TimeToken token, DateTime date) { // parse token if (token.Parser != ArithmeticTimePlugin.Key || !(token.Context is RelativeTimeUnit)) { return(null); } RelativeTimeUnit unit = (RelativeTimeUnit)token.Context; int value = int.Parse(token.Value); // apply switch (unit) { case RelativeTimeUnit.Seconds: return(date.AddSeconds(value)); case RelativeTimeUnit.Minutes: return(date.AddMinutes(value)); case RelativeTimeUnit.Hours: return(date.AddHours(value)); case RelativeTimeUnit.Days: return(date.AddDays(value)); case RelativeTimeUnit.Weeks: return(date.AddDays(value * 7)); case RelativeTimeUnit.Fortnights: return(date.AddDays(value * 14)); case RelativeTimeUnit.Months: return(date.AddMonths(value)); case RelativeTimeUnit.Years: return(date.AddYears(value)); default: throw new FormatException(String.Format("Invalid arithmetic time unit: {0}", unit)); } }
public string Apply_SupportsZeroUnits(int value, RelativeTimeUnit unit) { return(this.TryApply(value, unit)); }
public static CldrRelativeTimeFormat Resolve(string locale, RelativeTimeUnit unit, RelativeTimeStyle style) { return(Resolve(locale, GetLdmlFieldType(unit, style))); }
public FormattedString Format(double value, RelativeTimeUnit parsedUnit) { return(Format(value, parsedUnit, out _)); }