@@ -78,18 +78,18 @@ function getTimestampFromFilename(filename: string): string {
78
78
return '' ;
79
79
}
80
80
81
- // Helper function to format a Date object into a migration timestamp string (YYYYMMDDhhmmss)
81
+ // Helper function to format a Date object into a migration timestamp string (YYYYMMDDhhmmss) using UTC
82
82
function formatDateToTimestamp ( date : Date ) : string {
83
- const year = date . getFullYear ( ) ;
84
- const month = String ( date . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
85
- const day = String ( date . getDate ( ) ) . padStart ( 2 , '0' ) ;
86
- const hours = String ( date . getHours ( ) ) . padStart ( 2 , '0' ) ;
87
- const minutes = String ( date . getMinutes ( ) ) . padStart ( 2 , '0' ) ;
88
- const seconds = String ( date . getSeconds ( ) ) . padStart ( 2 , '0' ) ;
83
+ const year = date . getUTCFullYear ( ) ;
84
+ const month = String ( date . getUTCMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
85
+ const day = String ( date . getUTCDate ( ) ) . padStart ( 2 , '0' ) ;
86
+ const hours = String ( date . getUTCHours ( ) ) . padStart ( 2 , '0' ) ;
87
+ const minutes = String ( date . getUTCMinutes ( ) ) . padStart ( 2 , '0' ) ;
88
+ const seconds = String ( date . getUTCSeconds ( ) ) . padStart ( 2 , '0' ) ;
89
89
return `${ year } ${ month } ${ day } ${ hours } ${ minutes } ${ seconds } ` ;
90
90
}
91
91
92
- // Helper function to parse a timestamp string into a Date object
92
+ // Helper function to parse a timestamp string into a Date object (interpreted as UTC)
93
93
function parseTimestampToDate ( timestamp : string ) : Date | null {
94
94
// Validate format: YYYYMMDDhhmmss
95
95
if ( ! timestamp || timestamp . length !== 14 || ! / ^ \d { 14 } $ / . test ( timestamp ) ) {
@@ -103,42 +103,42 @@ function parseTimestampToDate(timestamp: string): Date | null {
103
103
const minutes = parseInt ( timestamp . substring ( 10 , 12 ) , 10 ) ;
104
104
const seconds = parseInt ( timestamp . substring ( 12 , 14 ) , 10 ) ;
105
105
106
- // Create date and validate (invalid dates like Feb 31 will be auto-corrected by JS Date)
107
- const date = new Date ( year , month , day , hours , minutes , seconds ) ;
106
+ // Create date in UTC and validate (invalid dates like Feb 31 will be auto-corrected by JS Date)
107
+ const date = new Date ( Date . UTC ( year , month , day , hours , minutes , seconds ) ) ;
108
108
109
109
// Additional validation to ensure the parsed date matches the input
110
110
// This catches edge cases like month=13 that JS Date would autocorrect
111
111
if (
112
- date . getFullYear ( ) !== year ||
113
- date . getMonth ( ) !== month ||
114
- date . getDate ( ) !== day ||
115
- date . getHours ( ) !== hours ||
116
- date . getMinutes ( ) !== minutes ||
117
- date . getSeconds ( ) !== seconds
112
+ date . getUTCFullYear ( ) !== year ||
113
+ date . getUTCMonth ( ) !== month ||
114
+ date . getUTCDate ( ) !== day ||
115
+ date . getUTCHours ( ) !== hours ||
116
+ date . getUTCMinutes ( ) !== minutes ||
117
+ date . getUTCSeconds ( ) !== seconds
118
118
) {
119
119
return null ;
120
120
}
121
121
122
122
return date ;
123
123
}
124
124
125
- // Helper function to generate a new timestamp that's higher than the reference timestamp
125
+ // Helper function to generate a new timestamp that's higher than the reference timestamp (using UTC)
126
126
function generateNewTimestamp (
127
127
referenceTimestamp : string ,
128
128
increment = 1
129
129
) : string {
130
130
// First try to parse the reference timestamp to a Date
131
131
const parsedDate = parseTimestampToDate ( referenceTimestamp ) ;
132
132
133
- // If we couldn't parse it, use current time
133
+ // If we couldn't parse it, use current UTC time
134
134
if ( ! parsedDate ) {
135
135
return formatDateToTimestamp ( new Date ( ) ) ;
136
136
}
137
137
138
138
// Add the specified number of seconds (default: 1)
139
- parsedDate . setSeconds ( parsedDate . getSeconds ( ) + increment ) ;
139
+ parsedDate . setUTCSeconds ( parsedDate . getUTCSeconds ( ) + increment ) ;
140
140
141
- // Get current time for comparison
141
+ // Get current UTC time for comparison
142
142
const now = new Date ( ) ;
143
143
144
144
// Return either the incremented timestamp or current time, whichever is later
@@ -147,7 +147,7 @@ function generateNewTimestamp(
147
147
return formatDateToTimestamp ( parsedDate ) ;
148
148
} else {
149
149
// If we're already at or past current time, add increment to now
150
- now . setSeconds ( now . getSeconds ( ) + increment ) ;
150
+ now . setUTCSeconds ( now . getUTCSeconds ( ) + increment ) ;
151
151
return formatDateToTimestamp ( now ) ;
152
152
}
153
153
}
0 commit comments