Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.11', 'stable']
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Build
run: go build ./...

- name: Test
run: go test -race ./...
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# monotick
# monotick

Monotonic timestamps as int64 nanoseconds.

[![Go Reference](https://pkg.go.dev/badge/github.com/dengaleev/monotick.svg)](https://pkg.go.dev/github.com/dengaleev/monotick)
[![CI](https://github.com/dengaleev/monotick/actions/workflows/ci.yml/badge.svg)](https://github.com/dengaleev/monotick/actions/workflows/ci.yml)

See [documentation](https://pkg.go.dev/github.com/dengaleev/monotick) for details.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dengaleev/monotick

go 1.11
40 changes: 40 additions & 0 deletions monotick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package monotick provides monotonic timestamps as int64 nanoseconds.
//
// # Why int64?
//
// For measuring durations, you don't need wall clock time — just a counter.
// int64 nanoseconds can represent ~292 years, while int32 overflows in ~2 seconds.
//
// # How it works
//
// A reference time.Time is captured at package init. Now() returns
// time.Since(start) as int64 nanoseconds. This approach:
//
// - Uses only public Go APIs (no go:linkname or runtime internals)
// - Requires no CGO
// - Works on all platforms
//
// Timestamps are relative to process start, not system boot. This is
// sufficient for measuring durations within a process.
//
// # Example
//
// start := monotick.Now()
// // ... do work ...
// elapsed := monotick.Since(start)
package monotick

import "time"

var start = time.Now()

// Now returns nanoseconds elapsed since process start (monotonic).
func Now() int64 {
return int64(time.Since(start))
}

// Since returns nanoseconds elapsed since t, where t is a value
// previously returned by Now.
func Since(t int64) int64 {
return Now() - t
}
Loading