コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }