Esempio n. 1
0
        public EcmaValue Construct(EcmaValue[] arg)
        {
            if (arg[0].IsUndefined())
            {
                return(EcmaValue.Object(new DateInstance(this, TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalMilliseconds)));
            }

            if (arg[1].IsUndefined())
            {
                double a1;
                if (arg[0].IsString())
                {
                    a1 = DatePrototype.Parse(arg[0].ToString(this.State));
                }
                else
                {
                    a1 = arg[0].ToInteger(this.State);
                }

                return(EcmaValue.Object(new DateInstance(this, DateFunc.TimeClip(a1))));
            }

            double year    = arg[0].ToInteger(this.State);
            double month   = arg[1].ToInteger(this.State);
            double date    = 1;
            double hours   = 0;
            double minutes = 0;
            double seconds = 0;
            double ms      = 0;

            if (!arg[2].IsUndefined())
            {
                date = arg[2].ToInteger(this.State);
            }

            if (!arg[3].IsUndefined())
            {
                hours = arg[3].ToInteger(this.State);
            }

            if (!arg[4].IsUndefined())
            {
                minutes = arg[4].ToInteger(this.State);
            }

            if (!arg[5].IsUndefined())
            {
                seconds = arg[5].ToInteger(this.State);
            }

            if (!arg[6].IsUndefined())
            {
                ms = arg[6].ToInteger(this.State);
            }

            if (!Double.IsNaN(year) && 0 <= year && year <= 99)
            {
                year += 1900;
            }

            return(EcmaValue.Object(new DateInstance(this,
                                                     DateFunc.TimeClip(
                                                         DateFunc.UTC(
                                                             DateFunc.MakeDate(
                                                                 DateFunc.MakeDay(
                                                                     year,
                                                                     month,
                                                                     date
                                                                     ),
                                                                 DateFunc.MakeTime(
                                                                     hours,
                                                                     minutes,
                                                                     seconds,
                                                                     ms
                                                                     )
                                                                 )
                                                             )
                                                         )
                                                     )));
        }
Esempio n. 2
0
        public double Parse(string str)
        {
            DateTime now = DateTime.UtcNow;

            double year    = now.Year;
            double months  = now.Month;
            double date    = now.Day;
            double hour    = now.Hour;
            double minuts  = now.Minute;
            double seconds = now.Second;
            double ms      = now.Millisecond;
            double buffer;

            if (str.IndexOf("/") != -1)
            {
                string[] dateParts = str.Split('/');
                if (dateParts.Length == 3)
                {
                    if (Double.TryParse(dateParts[0], out buffer))
                    {
                        year = buffer;
                    }

                    if (Double.TryParse(dateParts[1], out buffer))
                    {
                        months = buffer;
                    }

                    if (dateParts[2].IndexOf(":") != -1)
                    {
                        int    pos = dateParts[2].IndexOf(' ');
                        string sm  = dateParts[2].Substring(0, pos);
                        if (Double.TryParse(sm, out buffer))
                        {
                            date = buffer;
                        }

                        dateParts = dateParts[2].Substring(pos + 1).Split(':');

                        if (Double.TryParse(dateParts[0], out buffer))
                        {
                            hour = buffer;
                        }

                        if (Double.TryParse(dateParts[1], out buffer))
                        {
                            minuts = buffer;
                        }

                        if (Double.TryParse(dateParts[2], out buffer))
                        {
                            seconds = buffer;
                        }

                        if (Double.TryParse(dateParts[3], out buffer))
                        {
                            ms = buffer;
                        }
                    }
                    else
                    {
                        if (Double.TryParse(dateParts[2], out buffer))
                        {
                            date = buffer;
                        }
                    }
                }
            }
            return(DateFunc.MakeDate(
                       DateFunc.MakeDay(
                           year,
                           months,
                           date
                           ),
                       DateFunc.MakeTime(
                           hour,
                           minuts,
                           seconds,
                           ms
                           )
                       ));
        }
Esempio n. 3
0
        public DateConstructor(EcmaState state)
        {
            this.State = state;
            this.Put("length", EcmaValue.Number(7));
            this.Prototype     = state.Function;
            this.DatePrototype = new DatePrototype(state, this);

            this.Put("prototype", EcmaValue.Object(this.DatePrototype));
            this.Property["prototype"].DontDelete = true;
            this.Property["prototype"].DontEnum   = true;
            this.Property["prototype"].ReadOnly   = true;

            this.Put("parse", EcmaValue.Object(new NativeFunctionInstance(
                                                   1,
                                                   this.State,
                                                   (self, arg) =>
            {
                return(EcmaValue.Object(new DateInstance(this, DatePrototype.Parse(arg[0].ToString(State)))));
            }
                                                   )));
            this.Put("UTC", EcmaValue.Object(new NativeFunctionInstance(
                                                 7,
                                                 this.State,
                                                 (self, arg) =>
            {
                if (arg[0].IsUndefined())
                {
                    return(EcmaValue.Number(
                               TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalMilliseconds
                               ));
                }

                double year    = arg[0].ToInteger(State);
                double month   = arg[1].IsUndefined() ? 1 : arg[1].ToInteger(State);
                double date    = arg[2].IsUndefined() ? 1 : arg[2].ToInteger(State);
                double hours   = arg[3].IsUndefined() ? 0 : arg[3].ToInteger(State);
                double minutes = arg[4].IsUndefined() ? 0 : arg[4].ToInteger(State);
                double seconds = arg[5].IsUndefined() ? 0 : arg[5].ToInteger(State);
                double ms      = arg[6].IsUndefined() ? 0 : arg[6].ToInteger(State);

                if (!Double.IsNaN(year) && 0 <= year && year <= 99)
                {
                    year += 1900;
                }

                return(EcmaValue.Number(
                           DateFunc.MakeDate(
                               DateFunc.MakeDay(
                                   year,
                                   month,
                                   date
                                   ),
                               DateFunc.MakeTime(
                                   hours,
                                   minutes,
                                   seconds,
                                   ms
                                   )
                               )
                           ));
            }
                                                 )));
        }
Esempio n. 4
0
 public DatePrototype(EcmaState state, DateConstructor constructor)
 {
     this.Class     = "Date";
     this.Value     = EcmaValue.Number(Double.NaN);
     this.Prototype = state.Object;
     this.Put("constructor", EcmaValue.Object(constructor));
     this.Put("toString", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         AssetDate(self);
         return(ConvertToString(state, self));
     })));
     this.Put("valueOf", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         AssetDate(self);
         return(ConvertToString(state, self));
     })));
     this.Put("getTime", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         AssetDate(self);
         return(self.Value);
     })));
     this.Put("getYear", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double year = self.Value.ToInteger(state);
         if (Double.IsNaN(year))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.YearFromTime(DateFunc.LocalTime(year)) - 1900));
     })));
     this.Put("getFullYear", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double year = self.Value.ToInteger(state);
         if (Double.IsNaN(year))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.YearFromTime(DateFunc.LocalTime(year))));
     })));
     this.Put("getUTCFullYear", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double year = self.Value.ToInteger(state);
         if (Double.IsNaN(year))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.YearFromTime(year)));
     })));
     this.Put("getMonth", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double month = self.Value.ToInteger(state);
         if (Double.IsNaN(month))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.MonthFromTime(DateFunc.LocalTime(month))));
     })));
     this.Put("getUTCMonth", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double month = self.Value.ToInteger(state);
         if (Double.IsNaN(month))
         {
             return(EcmaValue.Number(double.NaN));
         }
         return(EcmaValue.Number(DateFunc.MonthFromTime(month)));
     })));
     this.Put("getDate", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.DateFromTime(DateFunc.LocalTime(t))));
     })));
     this.Put("getUTCDate", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.DateFromTime(t)));
     })));
     this.Put("getDay", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.WeekDay(DateFunc.LocalTime(t))));
     })));
     this.Put("getUTCDay", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.WeekDay(t)));
     })));
     this.Put("getHours", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.HourFromTime(DateFunc.LocalTime(t))));
     })));
     this.Put("getUTCHours", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.HourFromTime(t)));
     })));
     this.Put("getMinutes", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.MinFromTime(DateFunc.LocalTime(t))));
     })));
     this.Put("getUTCMinutes", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.MinFromTime(t)));
     })));
     this.Put("getSeconds", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) =>
     {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.SecFromTime(DateFunc.LocalTime(t))));
     })));
     this.Put("getUTCSeconds", EcmaValue.Object(new NativeFunctionInstance(0, state, (self, arg) => {
         double t = self.Value.ToInteger(state);
         if (Double.IsNaN(t))
         {
             return(EcmaValue.Number(Double.NaN));
         }
         return(EcmaValue.Number(DateFunc.SecFromTime(t)));
     })));
 }