formatRFC3339.cjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. exports.formatRFC3339 = formatRFC3339;
  3. var _index = require("./_lib/addLeadingZeros.cjs");
  4. var _index2 = require("./isValid.cjs");
  5. var _index3 = require("./toDate.cjs");
  6. /**
  7. * The {@link formatRFC3339} function options.
  8. */
  9. /**
  10. * @name formatRFC3339
  11. * @category Common Helpers
  12. * @summary Format the date according to the RFC 3339 standard (https://tools.ietf.org/html/rfc3339#section-5.6).
  13. *
  14. * @description
  15. * Return the formatted date string in RFC 3339 format. Options may be passed to control the parts and notations of the date.
  16. *
  17. * @param date - The original date
  18. * @param options - An object with options.
  19. *
  20. * @returns The formatted date string
  21. *
  22. * @throws `date` must not be Invalid Date
  23. *
  24. * @example
  25. * // Represent 18 September 2019 in RFC 3339 format:
  26. * formatRFC3339(new Date(2019, 8, 18, 19, 0, 52))
  27. * //=> '2019-09-18T19:00:52Z'
  28. *
  29. * @example
  30. * // Represent 18 September 2019 in RFC 3339 format, 3 digits of second fraction
  31. * formatRFC3339(new Date(2019, 8, 18, 19, 0, 52, 234), {
  32. * fractionDigits: 3
  33. * })
  34. * //=> '2019-09-18T19:00:52.234Z'
  35. */
  36. function formatRFC3339(date, options) {
  37. const date_ = (0, _index3.toDate)(date, options?.in);
  38. if (!(0, _index2.isValid)(date_)) {
  39. throw new RangeError("Invalid time value");
  40. }
  41. const fractionDigits = options?.fractionDigits ?? 0;
  42. const day = (0, _index.addLeadingZeros)(date_.getDate(), 2);
  43. const month = (0, _index.addLeadingZeros)(date_.getMonth() + 1, 2);
  44. const year = date_.getFullYear();
  45. const hour = (0, _index.addLeadingZeros)(date_.getHours(), 2);
  46. const minute = (0, _index.addLeadingZeros)(date_.getMinutes(), 2);
  47. const second = (0, _index.addLeadingZeros)(date_.getSeconds(), 2);
  48. let fractionalSecond = "";
  49. if (fractionDigits > 0) {
  50. const milliseconds = date_.getMilliseconds();
  51. const fractionalSeconds = Math.trunc(
  52. milliseconds * Math.pow(10, fractionDigits - 3),
  53. );
  54. fractionalSecond =
  55. "." + (0, _index.addLeadingZeros)(fractionalSeconds, fractionDigits);
  56. }
  57. let offset = "";
  58. const tzOffset = date_.getTimezoneOffset();
  59. if (tzOffset !== 0) {
  60. const absoluteOffset = Math.abs(tzOffset);
  61. const hourOffset = (0, _index.addLeadingZeros)(
  62. Math.trunc(absoluteOffset / 60),
  63. 2,
  64. );
  65. const minuteOffset = (0, _index.addLeadingZeros)(absoluteOffset % 60, 2);
  66. // If less than 0, the sign is +, because it is ahead of time.
  67. const sign = tzOffset < 0 ? "+" : "-";
  68. offset = `${sign}${hourOffset}:${minuteOffset}`;
  69. } else {
  70. offset = "Z";
  71. }
  72. return `${year}-${month}-${day}T${hour}:${minute}:${second}${fractionalSecond}${offset}`;
  73. }