dates.spec.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import timezoneMock from 'timezone-mock'
  3. import {
  4. absoluteDateTime as absDT,
  5. relativeDateTime as relDT,
  6. getISODatetime,
  7. } from '../dates.ts'
  8. import { Translator } from '../translator.ts'
  9. describe('Dates', () => {
  10. const dateUS = 'mm/dd/yyyy'
  11. const dateTimeUS = 'mm/dd/yyyy l:MM P'
  12. it('translates absolute dates correctly', () => {
  13. // UTC is default
  14. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  15. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  16. '04/09/2021 10:11 am',
  17. )
  18. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  19. '04/09/2021 10:11 pm',
  20. )
  21. // Switch to US/Eastern
  22. timezoneMock.register('US/Eastern', global)
  23. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  24. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  25. '04/09/2021 6:11 am',
  26. )
  27. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  28. '04/09/2021 6:11 pm',
  29. )
  30. timezoneMock.unregister(global)
  31. })
  32. it('handles UTC date strings', () => {
  33. expect(absDT('2021-04-09 10:11:12 UTC', dateUS)).toBe('04/09/2021')
  34. })
  35. it('shows relative dates correctly', () => {
  36. const b = new Date('2021-04-09T10:11:12Z')
  37. const t = new Translator()
  38. expect(relDT('2021-04-09T10:11:12Z', b, t)).toBe('just now')
  39. expect(relDT('2021-04-09T10:12:11Z', b, t)).toBe('just now')
  40. expect(relDT('2021-04-09T10:12:12Z', b, t)).toBe('in 1 minute')
  41. expect(relDT('2021-04-09T10:13:12Z', b, t)).toBe('in 2 minutes')
  42. expect(relDT('2021-04-09T11:10:12Z', b, t)).toBe('in 59 minutes')
  43. expect(relDT('2021-04-09T10:10:13Z', b, t)).toBe('just now')
  44. expect(relDT('2021-04-09T10:10:12Z', b, t)).toBe('1 minute ago')
  45. expect(relDT('2021-04-09T10:09:12Z', b, t)).toBe('2 minutes ago')
  46. expect(relDT('2021-04-09T09:12:12Z', b, t)).toBe('59 minutes ago')
  47. expect(relDT('2021-04-09T09:11:12Z', b, t)).toBe('1 hour ago')
  48. expect(relDT('2021-04-08T11:11:12Z', b, t)).toBe('23 hours ago')
  49. expect(relDT('2021-04-09T11:11:12Z', b, t)).toBe('in 1 hour')
  50. expect(relDT('2021-04-10T09:11:12Z', b, t)).toBe('in 23 hours')
  51. expect(relDT('2021-04-08T10:11:12Z', b, t)).toBe('1 day ago')
  52. expect(relDT('2021-04-03T10:11:12Z', b, t)).toBe('6 days ago')
  53. expect(relDT('2021-04-10T10:11:12Z', b, t)).toBe('in 1 day')
  54. expect(relDT('2021-04-15T10:11:12Z', b, t)).toBe('in 6 days')
  55. expect(relDT('2021-04-02T10:11:12Z', b, t)).toBe('1 week ago')
  56. expect(relDT('2021-03-12T10:11:12Z', b, t)).toBe('4 weeks ago')
  57. expect(relDT('2021-04-16T10:11:12Z', b, t)).toBe('in 1 week')
  58. expect(relDT('2021-05-07T10:11:12Z', b, t)).toBe('in 4 weeks')
  59. expect(relDT('2021-03-09T10:11:12Z', b, t)).toBe('1 month ago')
  60. expect(relDT('2020-04-19T10:11:12Z', b, t)).toBe('11 months ago')
  61. expect(relDT('2021-05-09T10:11:12Z', b, t)).toBe('in 1 month')
  62. expect(relDT('2022-03-09T10:11:12Z', b, t)).toBe('in 11 months')
  63. expect(relDT('2020-04-09T10:11:12Z', b, t)).toBe('1 year ago')
  64. expect(relDT('2000-04-09T10:11:12Z', b, t)).toBe('21 years ago')
  65. expect(relDT('2022-04-09T10:11:12Z', b, t)).toBe('in 1 year')
  66. expect(relDT('2042-04-09T10:11:12Z', b, t)).toBe('in 21 years')
  67. })
  68. it('shows ISO date times', () => {
  69. expect(getISODatetime('2021-04-09T10:11:12Z')).toBe(
  70. '2021-04-09T10:11:12.000Z',
  71. )
  72. expect(getISODatetime('2021-04-09 10:11:12')).toBe(
  73. '2021-04-09T10:11:12.000Z',
  74. )
  75. expect(getISODatetime('2021-04-09')).toBe('2021-04-09T00:00:00.000Z')
  76. })
  77. })