rotime

package module
v0.0.0-...-a6ee939 Latest Latest
Warning

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

Go to latest
Published: Jan 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

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).

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).

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".

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).

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).

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).

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).

Types

This section is empty.

Jump to

Keyboard shortcuts

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