44 "context"
55 "errors"
66 "reflect"
7+ "regexp"
8+ "strconv"
79 "strings"
810 "sync"
911 "time"
@@ -43,6 +45,7 @@ func NewTask(manager *pgqueue.Manager, threads uint) (server.Task, error) {
4345 self .decoder = marshaler .NewDecoder ("json" ,
4446 convertPtr ,
4547 convertPGTime ,
48+ convertPGDuration ,
4649 convertFloatToIntUint ,
4750 marshaler .ConvertTime ,
4851 marshaler .ConvertDuration ,
@@ -306,11 +309,13 @@ func joinName(parts ...string) string {
306309// PRIVATE METHODS
307310
308311var (
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+)$` )
311316)
312317
313- // convertTime returns time in postgres format
318+ // convertPGTime returns time from postgres format
314319func convertPGTime (src reflect.Value , dest reflect.Type ) (reflect.Value , error ) {
315320 // Pass value through
316321 if src .Type () == dest {
@@ -328,6 +333,32 @@ func convertPGTime(src reflect.Value, dest reflect.Type) (reflect.Value, error)
328333 return nilValue , nil
329334}
330335
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+
331362// convertPtr returns value if pointer
332363func convertPtr (src reflect.Value , dest reflect.Type ) (reflect.Value , error ) {
333364 // Pass value through
0 commit comments