4
4
"context"
5
5
"errors"
6
6
"reflect"
7
+ "regexp"
8
+ "strconv"
7
9
"strings"
8
10
"sync"
9
11
"time"
@@ -43,6 +45,7 @@ func NewTask(manager *pgqueue.Manager, threads uint) (server.Task, error) {
43
45
self .decoder = marshaler .NewDecoder ("json" ,
44
46
convertPtr ,
45
47
convertPGTime ,
48
+ convertPGDuration ,
46
49
convertFloatToIntUint ,
47
50
marshaler .ConvertTime ,
48
51
marshaler .ConvertDuration ,
@@ -306,11 +309,13 @@ func joinName(parts ...string) string {
306
309
// PRIVATE METHODS
307
310
308
311
var (
309
- nilValue = reflect .ValueOf (nil )
310
- timeType = reflect .TypeOf (time.Time {})
312
+ nilValue = reflect .ValueOf (nil )
313
+ timeType = reflect .TypeOf (time.Time {})
314
+ durationType = reflect .TypeOf (time .Duration (0 ))
315
+ rePostgresDuration = regexp .MustCompile (`^(\d+):(\d+):(\d+)$` )
311
316
)
312
317
313
- // convertTime returns time in postgres format
318
+ // convertPGTime returns time from postgres format
314
319
func convertPGTime (src reflect.Value , dest reflect.Type ) (reflect.Value , error ) {
315
320
// Pass value through
316
321
if src .Type () == dest {
@@ -328,6 +333,32 @@ func convertPGTime(src reflect.Value, dest reflect.Type) (reflect.Value, error)
328
333
return nilValue , nil
329
334
}
330
335
336
+ // convertPGDuration returns duration from postgres format
337
+ func convertPGDuration (src reflect.Value , dest reflect.Type ) (reflect.Value , error ) {
338
+ // Pass value through
339
+ if src .Type () == dest {
340
+ return src , nil
341
+ }
342
+
343
+ if dest == durationType {
344
+ // Convert 00:00:00 => time.Duration
345
+ if parts := rePostgresDuration .FindStringSubmatch (src .String ()); len (parts ) == 4 {
346
+ if hours , err := strconv .ParseUint (parts [1 ], 10 , 64 ); err != nil {
347
+ return nilValue , err
348
+ } else if minutes , err := strconv .ParseUint (parts [2 ], 10 , 64 ); err != nil {
349
+ return nilValue , err
350
+ } else if seconds , err := strconv .ParseUint (parts [3 ], 10 , 64 ); err != nil {
351
+ return nilValue , err
352
+ } else {
353
+ return reflect .ValueOf (time .Duration (hours )* time .Hour + time .Duration (minutes )* time .Minute + time .Duration (seconds )* time .Second ), nil
354
+ }
355
+ }
356
+ }
357
+
358
+ // Skip
359
+ return nilValue , nil
360
+ }
361
+
331
362
// convertPtr returns value if pointer
332
363
func convertPtr (src reflect.Value , dest reflect.Type ) (reflect.Value , error ) {
333
364
// Pass value through
0 commit comments