rotime

package module
v0.0.0-...-281ffd2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

Time Plugin

The time plugin provides operators for manipulating dates/time object in reactive streams.

Installation

go get github.com/samber/ro/plugins/time

Operators

Add

Add a duration to a date

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
        time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC),
        time.Date(0, time.January, 1, 23, 59, 59, 0, time.UTC),
    ),
    rotime.Add(2 * time.Hour),
)

// Output:
// Next: time.Date(2026, time.January, 7, 16, 30, 0, 0, time.UTC)
// Next: time.Date(0, time.January, 2, 1, 59, 59, 0, time.UTC)
// Completed
AddDate

Add a duration defined by years, months, days to a date.

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
        time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC),
    ),
    rotime.AddDate(0, 1, 0),
)

// Output:
// Next: time.Date(2026, time.February, 7, 14, 30, 0, 0, time.UTC)
// Completed
Format

Transform an observable time.Time into a string, formatted according to the provided layout.

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
        time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC),
    ),
    rotime.Format("2006-01-02 15:04:05"),
)

// Output:
// Next: "2026-01-07 14:30:00"
// Completed
In

Transform an observable time.Time into a string, formatted according to the provided layout.

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
       time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC),
    ),
    rotime.In(time.LoadLocation("Europe/Paris")),
)

// Output:
// Next: time.Date(2026, time.January, 7, 16, 30, 0, 0, time.CET),
// Completed
Parse

Transform an observable string into an observable of time.Time.

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
      "2026-01-07 14:30:00",
    ),
    rotime.Parse("2006-01-02 15:04:05"),
)

// Output:
// Next: time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
// Completed
ParseInLocation

Transform an observable string into an observable of time.Time, using location.

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
      "2026-01-07 14:30:00",
    ),
    rotime.ParseInLocation("2006-01-02 15:04:05", time.UTC),
)

// Output:
// Next: time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
// Completed
StartOfDay

Truncates the time to the beginning of its day in the local time zone

import (
    "github.com/samber/ro"
    rotime "github.com/samber/ro/plugins/time"
)

observable := ro.Pipe1(
    ro.Just(
      time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC),
    ),
    rotime.StartOfDay(),
)

// Output:
// Next: time.Date(2026, time.January, 7, 0, 0, 0, 0, time.UTC)
// Completed

Performance Considerations

  • The time plugin uses Go's standard time package for operations

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(d time.Duration) func(destination ro.Observable[time.Time]) ro.Observable[time.Time]

Add returns an operator that adds a fixed duration to each time value.

Example:

obs := ro.Pipe1(
    ro.Just(time.Now()),
    rotime.Add(2*time.Hour),
)

The observable then emits: time.Now().Add(2 * time.Hour). Play: https://go.dev/play/p/XWgGO-93YPK

Example
t := time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
observable := ro.Pipe2(
	ro.Just(t),
	Add(2*time.Hour),
	Format("2006-01-02 15:04:05"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 16:30:00
Completed

func AddDate

func AddDate(years int, months int, days int) func(destination ro.Observable[time.Time]) ro.Observable[time.Time]

AddDate returns an operator that adds a date offset (years, months, days) to each time value.

Example:

obs := ro.Pipe1(
    ro.Just(time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)),
    rotime.AddDate(0, 1, 0),
)

The observable then emits: time.Date(2026, time.February, 7, 14, 30, 0, 0, time.UTC).

Example
t := time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
observable := ro.Pipe2(
	ro.Just(t),
	AddDate(0, 1, 0),
	Format("2006-01-02 15:04:05"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-02-07 14:30:00
Completed

func Format

func Format(format string) func(destination ro.Observable[time.Time]) ro.Observable[string]

Format returns an operator that formats each time value using the given layout.

Example:

obs := ro.Pipe1(
    ro.Just(time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)),
    rotime.Format("2006-01-02 15:04:05"),
)

The observable then emits: "2026-01-07 14:30:00". Play: https://go.dev/play/p/ZOq1b1z2P1z

Example
t := time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
observable := ro.Pipe1(
	ro.Just(t),
	Format("2006-01-02 15:04:05"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 14:30:00
Completed

func In

func In(loc *time.Location) func(destination ro.Observable[time.Time]) ro.Observable[time.Time]

In returns an operator that converts each time value to the given location.

Example:

loc, _ := time.LoadLocation("Europe/Paris")

obs := ro.Pipe1(
    ro.Just(time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)),
    rotime.In(loc),
)

The observable then emits: time.Date(2026, time.January, 7, 15, 30, 0, 0, loc). Play: https://go.dev/play/p/4XKgzoqfH4H

Example
loc := time.FixedZone("UTC+2", 2*60*60)
t := time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)
observable := ro.Pipe2(
	ro.Just(t),
	In(loc),
	Format("2006-01-02 15:04:05 -0700"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 16:30:00 +0200
Completed

func Parse

func Parse[T ~string](layout string) func(ro.Observable[T]) ro.Observable[time.Time]

Parse returns an operator that parses time strings using the given layout.

Example:

obs := ro.Pipe[string, time.Time](
    ro.Just("2026-01-07 14:30:00"),
    rotime.Parse("2006-01-02 15:04:05"),
)

The observable then emits: time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC). Play: https://go.dev/play/p/rsbNtg6Xr_d

Example
observable := ro.Pipe2(
	ro.Just("2026-01-07 14:30:00"),
	Parse[string]("2006-01-02 15:04:05"),
	Format("2006-01-02 15:04:05"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 14:30:00
Completed

func ParseInLocation

func ParseInLocation[T ~string](layout string, loc *time.Location) func(ro.Observable[T]) ro.Observable[time.Time]

ParseInLocation returns an operator that parses time strings in the given location.

Example:

obs := ro.Pipe[string, time.Time](
    ro.Just("2026-01-07 14:30:00"),
    rotime.ParseInLocation("2006-01-02 15:04:05", time.UTC),
)

The observable then emits: time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC).

Example
loc := time.FixedZone("UTC+5", 5*60*60)
observable := ro.Pipe2(
	ro.Just("2026-01-07 14:30:00"),
	ParseInLocation[string]("2006-01-02 15:04:05", loc),
	Format("2006-01-02 15:04:05 -0700"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 14:30:00 +0500
Completed

func StartOfDay

func StartOfDay() func(ro.Observable[time.Time]) ro.Observable[time.Time]

StartOfDay returns an operator that truncates each time value to the start of its day.

Example:

obs := ro.Pipe1(
    ro.Just(time.Date(2026, time.January, 7, 14, 30, 0, 0, time.UTC)),
    rotime.StartOfDay(),
)

The observable then emits: time.Date(2026, time.January, 7, 0, 0, 0, 0, time.UTC). Play: https://go.dev/play/p/Rp_5REv3dMK

Example
t := time.Date(2026, time.January, 7, 14, 30, 45, 0, time.UTC)
observable := ro.Pipe2(
	ro.Just(t),
	StartOfDay(),
	Format("2006-01-02 15:04:05"),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:
Next: 2026-01-07 00:00:00
Completed

Types

This section is empty.

Jump to

Keyboard shortcuts

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