go.temporal.io/server@v1.23.0/common/primitives/timestamp/parse_duration.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2021 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  package timestamp
    24  
    25  import (
    26  	"errors"
    27  	"fmt"
    28  	"regexp"
    29  	"strconv"
    30  	"strings"
    31  	"time"
    32  )
    33  
    34  var (
    35  	reUnitless = regexp.MustCompile(`^(\d+(\.\d*)?|(\.\d+))$`)
    36  	reDays     = regexp.MustCompile(`(\d+(\.\d*)?|(\.\d+))d`)
    37  
    38  	errInvalidDuration        = errors.New("invalid duration")
    39  	errInvalidDurationHours   = errors.New("invalid duration: hours must be a positive number")
    40  	errInvalidDurationMinutes = errors.New("invalid duration: minutes must be from 0 to 59")
    41  	errInvalidDurationSeconds = errors.New("invalid duration: seconds must be from 0 to 59")
    42  )
    43  
    44  // ParseDuration is like time.ParseDuration, but supports unit "d" for days
    45  // (always interpreted as exactly 24 hours).
    46  func ParseDuration(s string) (time.Duration, error) {
    47  	s = reDays.ReplaceAllStringFunc(s, func(v string) string {
    48  		fv, err := strconv.ParseFloat(strings.TrimSuffix(v, "d"), 64)
    49  		if err != nil {
    50  			return v // will cause time.ParseDuration to return an error
    51  		}
    52  		return fmt.Sprintf("%fh", 24*fv)
    53  	})
    54  	return time.ParseDuration(s)
    55  }
    56  
    57  // ParseDurationDefaultDays is like time.ParseDuration, but supports unit "d"
    58  // for days (always interpreted as exactly 24 hours), and also supports
    59  // unit-less numbers, which are interpreted as days.
    60  func ParseDurationDefaultDays(s string) (time.Duration, error) {
    61  	if reUnitless.MatchString(s) {
    62  		s += "d"
    63  	}
    64  	return ParseDuration(s)
    65  }
    66  
    67  // ParseDurationDefaultSeconds is like time.ParseDuration, but supports unit "d"
    68  // for days (always interpreted as exactly 24 hours), and also supports
    69  // unit-less numbers, which are interpreted as seconds.
    70  func ParseDurationDefaultSeconds(s string) (time.Duration, error) {
    71  	if reUnitless.MatchString(s) {
    72  		s += "s"
    73  	}
    74  	return ParseDuration(s)
    75  }
    76  
    77  func ParseHHMMSSDuration(d string) (time.Duration, error) {
    78  	var hours, minutes, seconds time.Duration
    79  	_, err := fmt.Sscanf(d, "%d:%d:%d", &hours, &minutes, &seconds)
    80  	if err != nil {
    81  		return 0, errInvalidDuration
    82  	}
    83  	if hours < 0 {
    84  		return 0, errInvalidDurationHours
    85  	}
    86  	if minutes < 0 || minutes > 59 {
    87  		return 0, errInvalidDurationMinutes
    88  	}
    89  	if seconds < 0 || seconds > 59 {
    90  		return 0, errInvalidDurationSeconds
    91  	}
    92  
    93  	return hours*time.Hour + minutes*time.Minute + seconds*time.Second, nil
    94  }