dates.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. } from '../dates.ts'
  7. import { Translator } from '../translator.ts'
  8. describe('Dates', () => {
  9. const dateUS = 'mm/dd/yyyy'
  10. const dateTimeUS = 'mm/dd/yyyy l:MM P'
  11. it('translates absolute dates correctly', () => {
  12. // UTC is default
  13. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  14. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  15. '04/09/2021 10:11 am',
  16. )
  17. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  18. '04/09/2021 10:11 pm',
  19. )
  20. // Switch to US/Eastern
  21. timezoneMock.register('US/Eastern', global)
  22. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  23. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  24. '04/09/2021 6:11 am',
  25. )
  26. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  27. '04/09/2021 6:11 pm',
  28. )
  29. timezoneMock.unregister(global)
  30. })
  31. it('handles UTC date strings', () => {
  32. expect(absDT('2021-04-09 10:11:12 UTC', dateUS)).toBe('04/09/2021')
  33. })
  34. it('shows relative dates correctly', () => {
  35. const b = new Date('2021-04-09T10:11:12Z')
  36. const t = new Translator()
  37. expect(relDT('2021-04-09T10:11:12Z', b, t)).toBe('just now')
  38. expect(relDT('2021-04-09T10:12:11Z', b, t)).toBe('just now')
  39. expect(relDT('2021-04-09T10:12:12Z', b, t)).toBe('in 1 minute')
  40. expect(relDT('2021-04-09T10:13:12Z', b, t)).toBe('in 2 minutes')
  41. expect(relDT('2021-04-09T11:10:12Z', b, t)).toBe('in 59 minutes')
  42. expect(relDT('2021-04-09T10:10:13Z', b, t)).toBe('just now')
  43. expect(relDT('2021-04-09T10:10:12Z', b, t)).toBe('1 minute ago')
  44. expect(relDT('2021-04-09T10:09:12Z', b, t)).toBe('2 minutes ago')
  45. expect(relDT('2021-04-09T09:12:12Z', b, t)).toBe('59 minutes ago')
  46. expect(relDT('2021-04-09T09:11:12Z', b, t)).toBe('1 hour ago')
  47. expect(relDT('2021-04-08T11:11:12Z', b, t)).toBe('23 hours ago')
  48. expect(relDT('2021-04-09T11:11:12Z', b, t)).toBe('in 1 hour')
  49. expect(relDT('2021-04-10T09:11:12Z', b, t)).toBe('in 23 hours')
  50. expect(relDT('2021-04-08T10:11:12Z', b, t)).toBe('1 day ago')
  51. expect(relDT('2021-04-03T10:11:12Z', b, t)).toBe('6 days ago')
  52. expect(relDT('2021-04-10T10:11:12Z', b, t)).toBe('in 1 day')
  53. expect(relDT('2021-04-15T10:11:12Z', b, t)).toBe('in 6 days')
  54. expect(relDT('2021-04-02T10:11:12Z', b, t)).toBe('1 week ago')
  55. expect(relDT('2021-03-12T10:11:12Z', b, t)).toBe('4 weeks ago')
  56. expect(relDT('2021-04-16T10:11:12Z', b, t)).toBe('in 1 week')
  57. expect(relDT('2021-05-07T10:11:12Z', b, t)).toBe('in 4 weeks')
  58. expect(relDT('2021-03-09T10:11:12Z', b, t)).toBe('1 month ago')
  59. expect(relDT('2020-04-19T10:11:12Z', b, t)).toBe('11 months ago')
  60. expect(relDT('2021-05-09T10:11:12Z', b, t)).toBe('in 1 month')
  61. expect(relDT('2022-03-09T10:11:12Z', b, t)).toBe('in 11 months')
  62. expect(relDT('2020-04-09T10:11:12Z', b, t)).toBe('1 year ago')
  63. expect(relDT('2000-04-09T10:11:12Z', b, t)).toBe('21 years ago')
  64. expect(relDT('2022-04-09T10:11:12Z', b, t)).toBe('in 1 year')
  65. expect(relDT('2042-04-09T10:11:12Z', b, t)).toBe('in 21 years')
  66. })
  67. })