@@ -122,7 +122,7 @@ func NewSqliteStore(cfg *SqliteConfig, network *chaincfg.Params) (*SqliteSwapSto
122
122
ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
123
123
defer cancel ()
124
124
125
- err = baseDB .FixFaultyTimestamps (ctx , parseSqliteTimeStamp )
125
+ err = baseDB .FixFaultyTimestamps (ctx )
126
126
if err != nil {
127
127
log .Errorf ("Failed to fix faulty timestamps: %v" , err )
128
128
return nil , err
@@ -209,8 +209,7 @@ func (db *BaseDB) ExecTx(ctx context.Context, txOptions TxOptions,
209
209
210
210
// FixFaultyTimestamps fixes faulty timestamps in the database, caused
211
211
// by using milliseconds instead of seconds as the publication deadline.
212
- func (b * BaseDB ) FixFaultyTimestamps (ctx context.Context ,
213
- parseTimeFunc func (string ) (time.Time , error )) error {
212
+ func (b * BaseDB ) FixFaultyTimestamps (ctx context.Context ) error {
214
213
215
214
// Manually fetch all the loop out swaps.
216
215
rows , err := b .DB .QueryContext (
@@ -248,7 +247,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context,
248
247
defer tx .Rollback () //nolint: errcheck
249
248
250
249
for _ , swap := range loopOutSwaps {
251
- faultyTime , err := parseTimeFunc (swap .PublicationDeadline )
250
+ faultyTime , err := parseTimeStamp (swap .PublicationDeadline )
252
251
if err != nil {
253
252
return err
254
253
}
@@ -309,14 +308,29 @@ func (r *SqliteTxOptions) ReadOnly() bool {
309
308
return r .readOnly
310
309
}
311
310
311
+ // parseTimeStamp tries to parse a timestamp string with both the
312
+ // parseSqliteTimeStamp and parsePostgresTimeStamp functions.
313
+ // If both fail, it returns an error.
314
+ func parseTimeStamp (dateTimeStr string ) (time.Time , error ) {
315
+ t , err := parseSqliteTimeStamp (dateTimeStr )
316
+ if err != nil {
317
+ t , err = parsePostgresTimeStamp (dateTimeStr )
318
+ if err != nil {
319
+ return time.Time {}, err
320
+ }
321
+ }
322
+
323
+ return t , nil
324
+ }
325
+
312
326
// parseSqliteTimeStamp parses a timestamp string in the format of
313
327
// "YYYY-MM-DD HH:MM:SS +0000 UTC" and returns a time.Time value.
314
328
// NOTE: we can't use time.Parse() because it doesn't support having years
315
329
// with more than 4 digits.
316
330
func parseSqliteTimeStamp (dateTimeStr string ) (time.Time , error ) {
317
331
// Split the date and time parts.
318
332
parts := strings .Fields (strings .TrimSpace (dateTimeStr ))
319
- if len (parts ) <= 2 {
333
+ if len (parts ) < 2 {
320
334
return time.Time {}, fmt .Errorf ("invalid timestamp format: %v" ,
321
335
dateTimeStr )
322
336
}
@@ -385,7 +399,10 @@ func parseTimeParts(datePart, timePart string) (time.Time, error) {
385
399
return time.Time {}, err
386
400
}
387
401
388
- second , err := strconv .Atoi (timeParts [2 ])
402
+ // Parse the seconds and ignore the fractional part.
403
+ secondParts := strings .Split (timeParts [2 ], "." )
404
+
405
+ second , err := strconv .Atoi (secondParts [0 ])
389
406
if err != nil {
390
407
return time.Time {}, err
391
408
}
0 commit comments