go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/incrutil/cast_value_helpers.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package incrutil
     9  
    10  import (
    11  	"fmt"
    12  	"strconv"
    13  	"strings"
    14  	"time"
    15  )
    16  
    17  const (
    18  	durationDay = 24 * time.Hour
    19  )
    20  
    21  func formatDuration(duration time.Duration) string {
    22  	var output []string
    23  	var negative = duration < 0
    24  	if negative {
    25  		output = append(output, "-")
    26  		duration = -duration
    27  	}
    28  	if duration > durationDay {
    29  		days := duration / durationDay
    30  		output = append(output, fmt.Sprintf("%dd", days))
    31  		duration = duration - (days * durationDay)
    32  	}
    33  	output = append(output, duration.String())
    34  	return strings.Join(output, " ")
    35  }
    36  
    37  func parseDuration(value string) (time.Duration, error) {
    38  	pieces := strings.Fields(value)
    39  	var days, rest time.Duration
    40  	if len(pieces) > 2 {
    41  		return 0, fmt.Errorf("duration; expected up to (2) fields and no more")
    42  	} else if len(pieces) == 2 {
    43  		if strings.HasSuffix(pieces[0], "d") {
    44  			rawDays := strings.TrimSuffix(pieces[0], "d")
    45  			parsedDays, err := strconv.ParseInt(rawDays, 10, 64)
    46  			if err != nil {
    47  				return 0, err
    48  			}
    49  			days = time.Duration(parsedDays) * durationDay
    50  		} else {
    51  			return 0, fmt.Errorf(`invalid duration; fiest field should have the suffix "d"`)
    52  		}
    53  		var err error
    54  		rest, err = time.ParseDuration(pieces[1])
    55  		if err != nil {
    56  			return 0, err
    57  		}
    58  	} else {
    59  		var err error
    60  		rest, err = time.ParseDuration(pieces[1])
    61  		if err != nil {
    62  			return 0, err
    63  		}
    64  	}
    65  	return days + rest, nil
    66  }
    67  
    68  const TimeFormatYearTime = "2006-01-02 15:04"
    69  const TimeFormatRFC3339Esque = "2006-01-02T15:04:05.999999999"
    70  
    71  // ParseTime parses a timestamp from a string.
    72  func ParseTime(input string) (output time.Time, err error) {
    73  	formats := []string{
    74  		time.RFC3339,
    75  		time.RFC3339Nano,
    76  		TimeFormatYearTime,
    77  		TimeFormatRFC3339Esque,
    78  		time.DateOnly,
    79  	}
    80  	for _, format := range formats {
    81  		output, err = time.Parse(format, input)
    82  		if err == nil {
    83  			return
    84  		}
    85  	}
    86  	err = fmt.Errorf("unable to parse string as timestamp: %v", input)
    87  	return
    88  }
    89  
    90  func b2f64(b bool) float64 {
    91  	if b {
    92  		return 1
    93  	}
    94  	return 0
    95  }
    96  
    97  func b2i64(b bool) int64 {
    98  	if b {
    99  		return 1
   100  	}
   101  	return 0
   102  }