Go API Documentation

github.com/TykTechnologies/tyk/internal/time

No package summary is available.

Package

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const (
	// Nanosecond is an alias maintained to be used across the project.
	Nanosecond	= time.Nanosecond
	// Microsecond is an alias maintained to be used across the project.
	Microsecond	= time.Microsecond
	// Millisecond is an alias maintained to be used across the project.
	Millisecond	= time.Millisecond
	// Second is an alias maintained to be used across the project.
	Second	= time.Second
	// Minute is an alias maintained to be used across the project.
	Minute	= time.Minute
	// Hour is an alias maintained to be used across the project.
	Hour	= time.Hour
)

Types

Duration

Duration is an alias maintained to be used across the project.

Field name Field type Comment
type

time.Duration

No comment on field.
type Duration = time.Duration

ReadableDuration

ReadableDuration is a type alias for time.Duration, so that shorthand notation can be used. Examples of valid shorthand notations:

  • "1h" : one hour
  • "20m" : twenty minutes
  • "30s" : thirty seconds
  • "1m29s": one minute and twenty-nine seconds
  • "1h30m" : one hour and thirty minutes

An empty value is interpreted as "0s". It's important to format the string correctly, as invalid formats will be considered as 0s/empty.

Field name Field type Comment
type

time.Duration

No comment on field.
type ReadableDuration time.Duration

Functions

func (*ReadableDuration) UnmarshalJSON

UnmarshalJSON converts human-readable shorthand notation for time.Duration into ReadableDuration from json format.

func (d *ReadableDuration) UnmarshalJSON(data []byte) error {
	in, err := strconv.Unquote(string(data))
	if err != nil {
		return errors.New("error while parsing duration")
	}

	if in == "" {
		*d = 0
		return nil
	}

	// suppress error, return zero value
	duration, _ := time.ParseDuration(in)	//nolint:errcheck
	*d = ReadableDuration(duration)
	return nil
}

Cognitive complexity: 4, Cyclomatic complexity: 3

Uses: errors.New, strconv.Unquote, time.ParseDuration.

func (ReadableDuration) MarshalJSON

MarshalJSON converts ReadableDuration into human-readable shorthand notation for time.Duration into json format.

func (d ReadableDuration) MarshalJSON() ([]byte, error) {
	return []byte(fmt.Sprintf(`"%s"`, time.Duration(d).String())), nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: fmt.Sprintf, time.Duration.

func (ReadableDuration) Microseconds

Microseconds returns ReadableDuration in microseconds.

func (d ReadableDuration) Microseconds() int64 {
	return Duration(d).Microseconds()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (ReadableDuration) Milliseconds

Millisecond returns ReadableDuration in milliseconds.

func (d ReadableDuration) Milliseconds() int64 {
	return Duration(d).Milliseconds()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (ReadableDuration) Nanoseconds

Nanoseconds returns ReadableDuration in nanoseconds.

func (d ReadableDuration) Nanoseconds() int64 {
	return Duration(d).Nanoseconds()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (ReadableDuration) Seconds

Seconds returns ReadableDuration rounded down to the seconds.

func (d ReadableDuration) Seconds() float64 {
	durationInSeconds := math.Floor(Duration(d).Seconds())
	return durationInSeconds
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: math.Floor.

Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 3. Benchmarks: 0.

Test functions

TestReadableDuration_MarshalJSON

References: assert.Equal, assert.NoError, json.Marshal, testing.T, time.Millisecond, time.Minute.

TestReadableDuration_Seconds

References: assert.Equal, assert.NoError, json.Unmarshal, testing.T.

TestReadableDuration_UnmarshalJSON

References: assert.Equal, assert.NoError, json.Unmarshal, testing.T, time.Hour, time.Millisecond, time.Minute.