private static EcmaValue SetComponentsUtc(EcmaValue value, EcmaDateComponent start, int max, EcmaValue[] args) { EcmaDate date = value.GetUnderlyingObject <EcmaDate>(); long[] checkedValues = new long[Math.Min(args.Length, max)]; int i = 0; for (int length = checkedValues.Length; i < length; i++) { if (!ValidateArgument(args[i], out checkedValues[i])) { date.Timestamp = EcmaTimestamp.Invalid; return(EcmaValue.NaN); } } EcmaTimestamp result = date.Timestamp; if (!result.IsValid && start == EcmaDateComponent.Year) { result = new EcmaTimestamp(0); } if (result.IsValid) { result = (EcmaTimestamp)EcmaTimestamp.GetTimestampUtc(result.Value, (int)start, checkedValues); } date.Timestamp = result; return(result.IsValid ? result.Value : EcmaValue.NaN); }
public static EcmaValue UTC(params EcmaValue[] args) { if (args.Length == 0) { return(EcmaValue.NaN); } long[] checkedValues = new long[args.Length]; for (int i = 0, length = args.Length; i < length; i++) { if (!DatePrototype.ValidateArgument(args[i], out checkedValues[i])) { return(EcmaValue.NaN); } } if (checkedValues[0] >= 0 && checkedValues[0] <= 99) { checkedValues[0] += 1900; } EcmaTimestamp timestamp = new EcmaTimestamp(EcmaTimestamp.GetTimestampUtc(0, 0, checkedValues)); if (timestamp.IsValid) { return(timestamp.Value); } return(EcmaValue.NaN); }
public static EcmaValue SetTime([This] EcmaValue value, EcmaValue time) { EcmaDate date = value.GetUnderlyingObject <EcmaDate>(); time = time.ToNumber(); EcmaTimestamp dt = time.IsFinite ? new EcmaTimestamp(time.ToInt64()) : EcmaTimestamp.Invalid; date.Timestamp = dt; return(dt.IsValid ? dt.Value : EcmaValue.NaN); }
public static EcmaTimestamp ParseInternal(string inputString) { Guard.ArgumentNotNull(inputString, "inputString"); Match r = Regex.Match(inputString, "([+-]?\\d{3,6})-(\\d{2})-(\\d{2})(?:(?:T|\\s+)(\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d{3}))?(Z|[+-]\\d{1,4})?)?"); if (r.Success) { long y = Int64.Parse(r.Groups[1].Value); long m = Int64.Parse(r.Groups[2].Value); long d = Int64.Parse(r.Groups[3].Value); if (r.Groups[4].Success) { long h = Int64.Parse(r.Groups[4].Value); long n = Int64.Parse(r.Groups[5].Value); long s = Int64.Parse(r.Groups[6].Value); long ms = r.Groups[7].Success ? Int64.Parse(r.Groups[7].Value) : 0; long tz = (int)TimeZoneInfo.Local.BaseUtcOffset.TotalMilliseconds; if (r.Groups[8].Success) { string tzStr = r.Groups[8].Value; if (tzStr[0] == 'Z') { tz = 0; } else if (tzStr.Length <= 3) { tz = Int32.Parse(tzStr) * 864000; } else { tz = Int32.Parse(tzStr.Substring(0, tzStr.Length - 2)) * 864000 + Int32.Parse(tzStr.Substring(tzStr.Length - 2)) * 60000; } } return(new EcmaTimestamp(EcmaTimestamp.GetTimestampUtc(0, 0, y, m - 1, d, h, n, s, ms - tz))); } return(new EcmaTimestamp(EcmaTimestamp.GetTimestampUtc(0, 0, y, m - 1, d))); } DateTime dt; if (DateTime.TryParseExact(inputString, "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out dt)) { return(EcmaTimestamp.FromNativeDateTime(dt)); } int index1 = inputString.IndexOf('('); int index2 = inputString.IndexOf(')'); if (index1 >= 0 && index2 >= 0 && DateTime.TryParseExact(inputString.Substring(0, index1), "ddd MMM dd yyyy HH:mm:ss \\G\\M\\Tzzz", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowWhiteSpaces, out dt)) { return(EcmaTimestamp.FromNativeDateTime(dt)); } return(DateTime.TryParse(inputString, out dt) ? EcmaTimestamp.FromNativeDateTime(dt) : EcmaTimestamp.Invalid); }
public static EcmaValue Date([NewTarget] RuntimeObject constructor, [This] EcmaValue thisValue, params EcmaValue[] args) { if (constructor == null) { return(EcmaTimestamp.FromNativeDateTime(DateTime.Now).ToString()); } EcmaTimestamp timestamp = default; if (args.Length == 0) { timestamp = EcmaTimestamp.FromNativeDateTime(DateTime.UtcNow); } else if (args.Length == 1) { if (args[0].GetUnderlyingObject() is EcmaDate dt) { timestamp = dt.Timestamp; } else { EcmaValue primitive = args[0].ToPrimitive(); if (primitive.Type == EcmaValueType.String) { timestamp = ParseInternal((string)args[0]); } else { EcmaValue num = primitive.ToNumber(); timestamp = num.IsFinite ? new EcmaTimestamp(num.ToInt64()) : EcmaTimestamp.Invalid; } } } else { long[] checkedValues = new long[args.Length]; for (int i = 0, length = args.Length; i < length; i++) { if (!DatePrototype.ValidateArgument(args[i], out checkedValues[i])) { timestamp = EcmaTimestamp.Invalid; } } if (timestamp.Value != EcmaTimestamp.Invalid.Value) { timestamp = new EcmaTimestamp(EcmaTimestamp.GetTimestamp(EcmaTimestamp.LocalEpoch.Value, 0, checkedValues)); } } thisValue.GetUnderlyingObject <EcmaDate>().Timestamp = timestamp; return(thisValue); }
public static EcmaValue DatePrototypeToLocaleTimeString([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options) { EcmaTimestamp dt = thisValue.GetUnderlyingObject <EcmaDate>().Timestamp; if (!dt.IsValid) { return("Invalid Date"); } DateTimeFormat formatter = new DateTimeFormat(); ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales); formatter.Init(requestedLocales, DateTimeFormatConstructor.CreateOptions(options, false, true, false, false)); return(formatter.Format(thisValue).ToString()); }
public static EcmaValue Parse(EcmaValue str) { EcmaTimestamp ts = ParseInternal(str.ToStringOrThrow()); return(ts.IsValid ? ts.Value : EcmaValue.NaN); }
public static EcmaValue Now() { return(EcmaTimestamp.FromNativeDateTime(DateTime.Now).Value); }
public static EcmaValue GetUTCDay([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(!dt.IsValid ? EcmaValue.NaN : dt.GetComponentUtc(EcmaDateComponent.WeekDay)); }
public static EcmaValue GetTimezoneOffset([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(!dt.IsValid ? EcmaValue.NaN : EcmaTimestamp.TimezoneOffset); }
public static EcmaValue GetSeconds([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(!dt.IsValid ? EcmaValue.NaN : dt.GetComponent(EcmaDateComponent.Seconds)); }
public static EcmaValue ToJSON([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(!dt.IsValid ? null : dt.ToISOString()); }
public static string ToLocaleTimeString([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(dt.ToTimeString(DateTimeFormatInfo.CurrentInfo)); }
public static string ToUTCString([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(dt.ToUTCString(DateTimeFormatInfo.InvariantInfo)); }
public static string ToISOString([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(dt.ToISOString()); }
public static EcmaValue ValueOf([This] EcmaValue value) { EcmaTimestamp dt = value.GetUnderlyingObject <EcmaDate>().Timestamp; return(!dt.IsValid ? EcmaValue.NaN : dt.Value); }