@@ -122,7 +122,7 @@ func NewSqliteStore(cfg *SqliteConfig, network *chaincfg.Params) (*SqliteSwapSto
122122 ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
123123 defer cancel ()
124124
125- err = baseDB .FixFaultyTimestamps (ctx , parseSqliteTimeStamp )
125+ err = baseDB .FixFaultyTimestamps (ctx )
126126 if err != nil {
127127 log .Errorf ("Failed to fix faulty timestamps: %v" , err )
128128 return nil , err
@@ -209,8 +209,7 @@ func (db *BaseDB) ExecTx(ctx context.Context, txOptions TxOptions,
209209
210210// FixFaultyTimestamps fixes faulty timestamps in the database, caused
211211// 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 {
214213
215214 // Manually fetch all the loop out swaps.
216215 rows , err := b .DB .QueryContext (
@@ -248,7 +247,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context,
248247 defer tx .Rollback () //nolint: errcheck
249248
250249 for _ , swap := range loopOutSwaps {
251- faultyTime , err := parseTimeFunc (swap .PublicationDeadline )
250+ faultyTime , err := parseTimeStamp (swap .PublicationDeadline )
252251 if err != nil {
253252 return err
254253 }
@@ -309,14 +308,29 @@ func (r *SqliteTxOptions) ReadOnly() bool {
309308 return r .readOnly
310309}
311310
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+
312326// parseSqliteTimeStamp parses a timestamp string in the format of
313327// "YYYY-MM-DD HH:MM:SS +0000 UTC" and returns a time.Time value.
314328// NOTE: we can't use time.Parse() because it doesn't support having years
315329// with more than 4 digits.
316330func parseSqliteTimeStamp (dateTimeStr string ) (time.Time , error ) {
317331 // Split the date and time parts.
318332 parts := strings .Fields (strings .TrimSpace (dateTimeStr ))
319- if len (parts ) <= 2 {
333+ if len (parts ) < 2 {
320334 return time.Time {}, fmt .Errorf ("invalid timestamp format: %v" ,
321335 dateTimeStr )
322336 }
@@ -385,7 +399,10 @@ func parseTimeParts(datePart, timePart string) (time.Time, error) {
385399 return time.Time {}, err
386400 }
387401
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 ])
389406 if err != nil {
390407 return time.Time {}, err
391408 }
0 commit comments