DateFromTime.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $EvalError = GetIntrinsic('%EvalError%');
  4. var DayWithinYear = require('./DayWithinYear');
  5. var InLeapYear = require('./InLeapYear');
  6. var MonthFromTime = require('./MonthFromTime');
  7. // https://262.ecma-international.org/5.1/#sec-15.9.1.5
  8. module.exports = function DateFromTime(t) {
  9. var m = MonthFromTime(t);
  10. var d = DayWithinYear(t);
  11. if (m === 0) {
  12. return d + 1;
  13. }
  14. if (m === 1) {
  15. return d - 30;
  16. }
  17. var leap = InLeapYear(t);
  18. if (m === 2) {
  19. return d - 58 - leap;
  20. }
  21. if (m === 3) {
  22. return d - 89 - leap;
  23. }
  24. if (m === 4) {
  25. return d - 119 - leap;
  26. }
  27. if (m === 5) {
  28. return d - 150 - leap;
  29. }
  30. if (m === 6) {
  31. return d - 180 - leap;
  32. }
  33. if (m === 7) {
  34. return d - 211 - leap;
  35. }
  36. if (m === 8) {
  37. return d - 242 - leap;
  38. }
  39. if (m === 9) {
  40. return d - 272 - leap;
  41. }
  42. if (m === 10) {
  43. return d - 303 - leap;
  44. }
  45. if (m === 11) {
  46. return d - 333 - leap;
  47. }
  48. throw new $EvalError('Assertion failed: MonthFromTime returned an impossible value: ' + m);
  49. };