duration

package
v1.221.0-test.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package duration provides utilities for parsing human-readable duration strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExpired

func IsExpired(updatedAt time.Time, ttl string) (bool, error)

IsExpired reports whether a cache entry last refreshed at updatedAt has exceeded ttl.

A zero TTL (see IsZeroTTL) is always expired. A non-zero TTL is parsed with ParseDuration; on a parse error the error is returned and callers should fail safe by treating the entry as expired to avoid serving stale data. This helper makes no decision for an empty ttl (""). The meaning of "no TTL configured" differs between subsystems (reuse-forever versus refresh-each-run), so callers handle "" explicitly.

func IsZeroTTL

func IsZeroTTL(ttl string) bool

IsZeroTTL reports whether the TTL string represents a zero duration.

ParseDuration rejects "0" (a zero duration is not a valid period for cleanup scheduling), but for cache TTLs a zero value is a legitimate, common case meaning "always expired / never reuse". Callers detect it with this helper before parsing.

func Parse

func Parse(s string) (int64, error)

Parse parses a duration string into seconds.

Supported formats:

  • Integer seconds: "3600" → 3600
  • Duration with suffix: "1h", "30m", "7d", "10s" → seconds
  • Keywords: "minute", "hourly", "daily", "weekly", "monthly", "yearly"

Examples:

Parse("3600")    → 3600, nil
Parse("1h")      → 3600, nil
Parse("7d")      → 604800, nil
Parse("daily")   → 86400, nil
Parse("invalid") → 0, error

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string and returns a time.Duration.

This is a convenience wrapper around Parse that converts the result from seconds to time.Duration.

Examples:

ParseDuration("1h")    → 1 * time.Hour, nil
ParseDuration("7d")    → 7 * 24 * time.Hour, nil
ParseDuration("daily") → 24 * time.Hour, nil

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL