The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ DateConstructor

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using System;
  19.     using System.Diagnostics;
  20.    
  21.     public class DateConstructor : ScriptFunction
  22.     {
  23.         static internal readonly DateConstructor ob = new DateConstructor();
  24.        
  25.         private DatePrototype originalPrototype;
  26.        
  27.         internal DateConstructor() : base(FunctionPrototype.ob, "Date", 7)
  28.         {
  29.             this.originalPrototype = DatePrototype.ob;
  30.             DatePrototype._constructor = this;
  31.             this.proto = DatePrototype.ob;
  32.         }
  33.        
  34.         internal DateConstructor(LenientFunctionPrototype parent, LenientDatePrototype prototypeProp) : base(parent, "Date", 7)
  35.         {
  36.             this.originalPrototype = prototypeProp;
  37.             prototypeProp.constructor = this;
  38.             this.proto = prototypeProp;
  39.             this.noExpando = false;
  40.         }
  41.        
  42.         internal override object Call(object[] args, object thisob)
  43.         {
  44.             return this.Invoke();
  45.         }
  46.        
  47.         internal DateObject Construct(DateTime dt)
  48.         {
  49.             return new DateObject(this.originalPrototype, dt.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970);
  50.         }
  51.        
  52.         internal override object Construct(object[] args)
  53.         {
  54.             return this.CreateInstance(args);
  55.         }
  56.        
  57.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs)]
  58.         public new DateObject CreateInstance(params object[] args)
  59.         {
  60.             if (args.Length == 0)
  61.                 return new DateObject(this.originalPrototype, DateTime.Now.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970);
  62.             if (args.Length == 1) {
  63.                 object arg0 = args[0];
  64.                 IConvertible ic = Convert.GetIConvertible(arg0);
  65.                 TypeCode tcode = Convert.GetTypeCode(arg0, ic);
  66.                 if (tcode == TypeCode.DateTime)
  67.                     return new DateObject(this.originalPrototype, ic.ToDateTime(null).ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970);
  68.                 object yv = Convert.ToPrimitive(arg0, PreferredType.Either, ref ic);
  69.                 if (Convert.GetTypeCode(yv, ic) == TypeCode.String)
  70.                     return new DateObject(this.originalPrototype, parse(ic.ToString(null)));
  71.                 double d = Convert.ToNumber(yv, ic);
  72.                 if (DatePrototype.minDate <= d && d <= DatePrototype.maxDate)
  73.                     return new DateObject(this.originalPrototype, d);
  74.                 return new DateObject(this.originalPrototype, Double.NaN);
  75.             }
  76.             double dyear = Convert.ToNumber(args[0]);
  77.             double dmonth = Convert.ToNumber(args[1]);
  78.             double ddate = (args.Length > 2) ? Convert.ToNumber(args[2]) : 1;
  79.             double dhours = (args.Length > 3) ? Convert.ToNumber(args[3]) : 0;
  80.             double dminutes = (args.Length > 4) ? Convert.ToNumber(args[4]) : 0;
  81.             double dseconds = (args.Length > 5) ? Convert.ToNumber(args[5]) : 0;
  82.             double dms = (args.Length > 6) ? Convert.ToNumber(args[6]) : 0;
  83.             int y = (int)Runtime.DoubleToInt64(dyear);
  84.             if (!Double.IsNaN(dyear) && 0 <= y && y <= 99)
  85.                 dyear = y + 1900;
  86.             double day = DatePrototype.MakeDay(dyear, dmonth, ddate);
  87.             double time = DatePrototype.MakeTime(dhours, dminutes, dseconds, dms);
  88.             return new DateObject(this.originalPrototype, DatePrototype.TimeClip(DatePrototype.UTC(DatePrototype.MakeDate(day, time))));
  89.         }
  90.        
  91.         public string Invoke()
  92.         {
  93.             return DatePrototype.DateToString(DateTime.Now.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970);
  94.         }
  95.        
  96.         [JSFunctionAttribute(0, JSBuiltin.Date_parse)]
  97.         public static double parse(string str)
  98.         {
  99.             return DatePrototype.ParseDate(str);
  100.         }
  101.        
  102.         [JSFunctionAttribute(0, JSBuiltin.Date_UTC)]
  103.         public static double UTC(object year, object month, object date, object hours, object minutes, object seconds, object ms)
  104.         {
  105.             if (year is Missing)
  106.                 return DateTime.Now.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970;
  107.             double dyear = Convert.ToNumber(year);
  108.             double dmonth = month is Missing ? 0 : Convert.ToNumber(month);
  109.             double ddate = date is Missing ? 1 : Convert.ToNumber(date);
  110.             double dhours = hours is Missing ? 0 : Convert.ToNumber(hours);
  111.             double dminutes = minutes is Missing ? 0 : Convert.ToNumber(minutes);
  112.             double dseconds = seconds is Missing ? 0 : Convert.ToNumber(seconds);
  113.             double dms = ms is Missing ? 0 : Convert.ToNumber(ms);
  114.             int y = (int)Runtime.DoubleToInt64(dyear);
  115.             if (!Double.IsNaN(dyear) && 0 <= y && y <= 99)
  116.                 dyear = y + 1900;
  117.             double day = DatePrototype.MakeDay(dyear, dmonth, ddate);
  118.             double time = DatePrototype.MakeTime(dhours, dminutes, dseconds, dms);
  119.             return DatePrototype.TimeClip(DatePrototype.MakeDate(day, time));
  120.         }
  121.     }
  122. }

Developer Fusion