DateInstance.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. namespace Jint.Native.Date
  5. {
  6. public class DateInstance : ObjectInstance
  7. {
  8. // Maximum allowed value to prevent DateTime overflow
  9. internal static readonly double Max = (DateTime.MaxValue - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
  10. // Minimum allowed value to prevent DateTime overflow
  11. internal static readonly double Min = -(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) - DateTime.MinValue).TotalMilliseconds;
  12. public DateInstance(Engine engine)
  13. : base(engine)
  14. {
  15. }
  16. public override string Class
  17. {
  18. get
  19. {
  20. return "Date";
  21. }
  22. }
  23. public DateTime ToDateTime()
  24. {
  25. if (double.IsNaN(PrimitiveValue) || PrimitiveValue > Max || PrimitiveValue < Min)
  26. {
  27. throw new JavaScriptException(Engine.RangeError);
  28. }
  29. else
  30. {
  31. return DateConstructor.Epoch.AddMilliseconds(PrimitiveValue);
  32. }
  33. }
  34. public double PrimitiveValue { get; set; }
  35. }
  36. }