DateFromTime.js 988 B

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