Implementation:Ollama Ollama Format Time
| Knowledge Sources | |
|---|---|
| Domains | Utility, Formatting |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Utility package that converts timestamps and durations into human-readable relative time strings like "About a minute", "3 hours ago", or "2 days from now".
Description
The internal humanDuration function converts a time.Duration into a natural language string by cascading through time thresholds: seconds ("Less than a second", "1 second", "N seconds"), minutes ("About a minute", "N minutes"), hours ("About an hour", "N hours"), days, weeks, months, and years. HumanTime calculates the delta from time.Now() and appends "ago" or "from now" as appropriate. HumanTimeLower returns the lowercase version via strings.ToLower. Zero-value times return a configurable fallback string, and dates more than 20 years in the future return "Forever".
Usage
Used in the Ollama CLI to display model creation times and other timestamps in a user-friendly relative format, such as in ollama list output showing "2 days ago".
Code Reference
Source Location
- Repository: Ollama
- File: format/time.go
- Lines: 1-70
Signature
func HumanTime(t time.Time, zeroValue string) string
func HumanTimeLower(t time.Time, zeroValue string) string
Import
import "github.com/ollama/ollama/format"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| t | time.Time | Yes | Timestamp to format relative to now |
| zeroValue | string | Yes | Fallback string for zero-value times |
Outputs
| Name | Type | Description |
|---|---|---|
| formatted | string | Human-readable relative time string (e.g., "3 hours ago") |
Usage Examples
import (
"time"
"github.com/ollama/ollama/format"
)
// Recent timestamp
created := time.Now().Add(-2 * time.Hour)
fmt.Println(format.HumanTime(created, "Never")) // "2 hours ago"
// Zero value
fmt.Println(format.HumanTime(time.Time{}, "Never")) // "Never"
// Future timestamp
future := time.Now().Add(30 * time.Minute)
fmt.Println(format.HumanTime(future, "")) // "30 minutes from now"
// Lowercase version
fmt.Println(format.HumanTimeLower(created, "never")) // "2 hours ago"