period

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 4 Imported by: 0

README

Period

GitHub tag (latest by date) GitHub last commit GitHub issues GitHub

A Period is a predefined range of milliseconds during which operations may happen. It has an ID, ie. the number of Periods elapsed since the original Period set at initialization. You can also get its start and end timestamps.

It's mainly used in immutable shared ledgers that implements the DRD2 consensus protocol.

Usage
go get github.com/cyrildever/period

You first have to initialize the main parameters of the Period environment: (in milliseconds)

import "github.com/cyrildever/period"

originTimestamp := 1574252822201
periodSpan := 10000

err := period.Init(originTimestamp, periodSpan)

Then, you can start using Periods:

import (
    "github.com/cyrildever/period"
    "github.com/cyrildever/period/timestamp"
)

currentTimestamp, err := timestamp.CurrentMillis()
p := period.Get(currentTimestamp)

if currentTimestamp != p.StartTimestampMillis() {
    // It's an error
}
if p.EndTimestampMillis() != p.StartTimestampMillis() + period.Span() - 1 {
    // It's another error
}
if p.Next().ID != p.ID + 1 {
    // Yet another error
}

IMPORTANT: You might want to notice that, as there is no year zero in the Gregorian calendar, the first Period ID is 1 (not 0 as we, computer engineers, generally like to start our arrays).

NB: You should set a NTP server somewhere but, if you don't, you still can get the current timestamp of the machine running your program by passing true as an argument to the CurrentMillis function, eg.

currentTimestamp, _ := timestamp.CurrentMillis(true)
API
// Mandatory initialization
err := period.Init(originTimestamp, periodSpan, isTestEnvironment)
if _, ok := err.(*period.AlreadyInitializedError); !ok {
    // Do something with any error other than period already initialized
}

// To get the current Period
currentPeriod, err := period.Current()

// To get a period at timestamp ts
periodAtTS, err = period.Get(ts)

// To get the span of the Periods either in milliseconds or as a time.Duration
ms := period.Span()
duration := period.GetDuration()

// To get the start of a Period
start := currentPeriod.StartTimestampMillis()

// To get the end of a Period, ie. the last millisecond before moving to the next Period
end := currentPeriod.EndTimestampMillis()

// To get one's following Period
nextPeriod := currentPeriod.Next()

// Unset test environment
isTest := period.SetTestEnvironment(false)
License

This module is distributed under a MIT license.
See the LICENSE file.


© 2019-2026 Cyril Dever. All rights reserved.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDuration

func GetDuration() time.Duration

GetDuration ...

func Init

func Init(originTimestamp, periodSpan uint64, isTestEnvironment ...bool) error

Init must be called before anything, otherwise a panic may occur.

It needs two mandatory arguments: an origin timestamp and a non-null period span (both in milliseconds), and an optional argument (passing it `true` if this is a test environment). It may throw an `AlreadyInitializedError` because the period module can't be initialized twice.

func IsTestEnvironment

func IsTestEnvironment() bool

func ResetOriginTimestamp added in v1.0.2

func ResetOriginTimestamp(newTimestamp uint64)

ResetOriginTimestamp allows to force a new setting of the begin of the time. NB: Use with caution as all previous calculations might go wrong.

func SetTestEnvironment added in v1.0.1

func SetTestEnvironment(isTest bool) bool

func Span

func Span() uint64

Span returns the period span (in milliseconds)

Types

type AlreadyInitializedError added in v1.0.1

type AlreadyInitializedError struct {
	// contains filtered or unexported fields
}

AlreadyInitializedError ...

func NewAlreadyInitializedError added in v1.0.1

func NewAlreadyInitializedError() *AlreadyInitializedError

NewAlreadyInitializedError ...

func (*AlreadyInitializedError) Error added in v1.0.1

func (e *AlreadyInitializedError) Error() string

type Period

type Period struct {
	ID uint64
	// contains filtered or unexported fields
}

Period ...

func Current

func Current() (Period, error)

Current ...

func Get

func Get(timestampMillis uint64) (p Period, err error)

Get ...

func Now

func Now() Period

Now returns the current period (with an ID of 0 if an error occurred)

func (*Period) EndTimestampMillis

func (p *Period) EndTimestampMillis() uint64

EndTimestampMillis ...

func (*Period) Next

func (p *Period) Next() *Period

Next ...

func (*Period) StartTimestampMillis

func (p *Period) StartTimestampMillis() uint64

StartTimestampMillis ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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