@@ -16,21 +16,38 @@ describe('formatDate', () => {
1616 } )
1717
1818 it ( 'handles end-of-year dates consistently in UTC' , ( ) => {
19- // 2022-12-31T23:00:00Z is still Dec 31 in UTC even if it's Jan 1 locally
19+ // Still Dec 31 in UTC even when local TZ has crossed into Jan 1
2020 expect ( formatDate ( '2022-12-31T23:00:00.000Z' ) ) . toBe ( '2022-12-31' )
2121 } )
2222
2323 it ( 'handles dates near midnight boundary in UTC' , ( ) => {
24- // This is Jan 1 00:30 UTC — should be 2023-01-01, not 2022-12-31
2524 expect ( formatDate ( '2023-01-01T00:30:00.000Z' ) ) . toBe ( '2023-01-01' )
2625 } )
2726
27+ it ( 'treats date-only strings as UTC calendar dates' , ( ) => {
28+ expect ( formatDate ( '2023-01-01' ) ) . toBe ( '2023-01-01' )
29+ expect ( formatDate ( '2024-12-31' ) ) . toBe ( '2024-12-31' )
30+ } )
31+
32+ it ( 'treats offset-less ISO datetimes as UTC' , ( ) => {
33+ // ES would parse this as local time; we force UTC
34+ expect ( formatDate ( '2023-01-01T00:00:00' ) ) . toBe ( '2023-01-01' )
35+ expect ( formatDate ( '2022-12-31T23:30:00' ) ) . toBe ( '2022-12-31' )
36+ } )
37+
2838 it ( 'parses space-separated datetime as UTC' , ( ) => {
2939 expect ( formatDate ( '2022-06-15 14:30:00' ) ) . toBe ( '2022-06-15' )
3040 } )
3141
32- it ( 'handles Date object input' , ( ) => {
42+ it ( 'respects explicit non-UTC offsets' , ( ) => {
43+ // 2023-01-01 00:30 in UTC+5:30 → 2022-12-31 19:00 UTC
44+ expect ( formatDate ( '2023-01-01T00:30:00+05:30' ) ) . toBe ( '2022-12-31' )
45+ } )
46+
47+ it ( 'handles Date object input via UTC components' , ( ) => {
3348 expect ( formatDate ( new Date ( '2022-06-15T14:30:00.000Z' ) ) ) . toBe ( '2022-06-15' )
49+ // Near midnight UTC boundary
50+ expect ( formatDate ( new Date ( '2022-12-31T23:00:00.000Z' ) ) ) . toBe ( '2022-12-31' )
3451 } )
3552
3653 it ( 'throws on invalid date' , ( ) => {
@@ -40,7 +57,15 @@ describe('formatDate', () => {
4057
4158 it ( 'produces same output as the build-time copy' , async ( ) => {
4259 const buildTime = await import ( '../../src/utils/content/transformers/utils' )
43- const inputs = [ '2022-06-15T12:00:00.000Z' , '2023-01-01T00:00:00.000Z' , '2024-12-31T23:59:59.000Z' ]
60+ const inputs = [
61+ '2022-06-15T12:00:00.000Z' ,
62+ '2023-01-01T00:00:00.000Z' ,
63+ '2024-12-31T23:59:59.000Z' ,
64+ '2023-01-01' ,
65+ '2023-01-01T00:00:00' ,
66+ '2022-06-15 14:30:00' ,
67+ '2023-01-01T00:30:00+05:30' ,
68+ ]
4469 for ( const input of inputs ) {
4570 expect ( formatDate ( input ) ) . toBe ( buildTime . formatDate ( input ) )
4671 }
@@ -61,16 +86,30 @@ describe('formatDateTime', () => {
6186 } )
6287
6388 it ( 'uses UTC time components regardless of system timezone' , ( ) => {
64- // Midnight UTC should always produce 00:00:00
6589 expect ( formatDateTime ( '2022-06-15T00:00:00.000Z' ) ) . toBe ( '2022-06-15 00:00:00' )
66- // 23:59:59 UTC should always produce that time, not shift to next day
6790 expect ( formatDateTime ( '2022-12-31T23:59:59.000Z' ) ) . toBe ( '2022-12-31 23:59:59' )
6891 } )
6992
93+ it ( 'treats offset-less ISO datetimes as UTC' , ( ) => {
94+ expect ( formatDateTime ( '2022-06-15T14:30:45' ) ) . toBe ( '2022-06-15 14:30:45' )
95+ expect ( formatDateTime ( '2022-12-31T23:00:00' ) ) . toBe ( '2022-12-31 23:00:00' )
96+ } )
97+
98+ it ( 'parses space-separated datetime as UTC' , ( ) => {
99+ expect ( formatDateTime ( '2022-06-15 14:30:45' ) ) . toBe ( '2022-06-15 14:30:45' )
100+ } )
101+
102+ it ( 'respects explicit non-UTC offsets' , ( ) => {
103+ expect ( formatDateTime ( '2022-06-15T14:30:45+02:00' ) ) . toBe ( '2022-06-15 12:30:45' )
104+ } )
105+
106+ it ( 'handles Date object input' , ( ) => {
107+ expect ( formatDateTime ( new Date ( '2022-06-15T14:30:45.000Z' ) ) ) . toBe ( '2022-06-15 14:30:45' )
108+ } )
109+
70110 it ( 'the date portion matches formatDate output' , ( ) => {
71111 const input = '2022-06-15T14:30:45.000Z'
72- const result = formatDateTime ( input )
73- expect ( result . split ( ' ' ) [ 0 ] ) . toBe ( formatDate ( input ) )
112+ expect ( formatDateTime ( input ) . split ( ' ' ) [ 0 ] ) . toBe ( formatDate ( input ) )
74113 } )
75114
76115 it ( 'throws on invalid datetime' , ( ) => {
@@ -80,7 +119,13 @@ describe('formatDateTime', () => {
80119
81120 it ( 'produces same output as the build-time copy' , async ( ) => {
82121 const buildTime = await import ( '../../src/utils/content/transformers/utils' )
83- const inputs = [ '2022-06-15T14:30:45.000Z' , '2023-01-01T00:00:00.000Z' ]
122+ const inputs = [
123+ '2022-06-15T14:30:45.000Z' ,
124+ '2023-01-01T00:00:00.000Z' ,
125+ '2022-06-15T14:30:45' ,
126+ '2022-06-15 14:30:45' ,
127+ '2022-06-15T14:30:45+02:00' ,
128+ ]
84129 for ( const input of inputs ) {
85130 expect ( formatDateTime ( input ) ) . toBe ( buildTime . formatDateTime ( input ) )
86131 }
0 commit comments