naturaltime

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 5 Imported by: 6 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NaturalTime 🕒

Go Reference Go Report Card

A powerful Go library for parsing natural language time expressions with exceptional support for time ranges! NaturalTime is a wrapper around the excellent chrono-node JavaScript library, providing Go developers with advanced natural language time parsing capabilities.

✨ Features

  • Parse natural language date expressions into time.Time objects
  • Extract specific dates or date ranges
  • Support for multiple time ranges in a single expression
  • Integration with Go's time package
  • Clean, idiomatic Go API

📦 Installation

go get github.com/sho0pi/naturaltime

🚀 Usage

package main

import (
	"fmt"
	"time"

	"github.com/sho0pi/naturaltime"
)

func main() {
	// Create a new parser
	parser, err := naturaltime.New()
	if err != nil {
		panic(err)
	}
	
	now := time.Now()
	
	// Example 1: Parse a simple date expression
	date, err := parser.ParseDate("tomorrow at 3pm", now)
	if err != nil {
		panic(err)
	}
	if date != nil {
		fmt.Println(date)
	}
	
	// Example 2: Parse a time range expression
	timeRange, err := parser.ParseRange("from 2pm to 4pm tomorrow", now)
	if err != nil {
		panic(err)
	}
	fmt.Printf("  Start: %s, End: %s", timeRange.Start(), timeRange.End())
	fmt.Printf("  Duration: %s\n\n", timeRange.Duration)
	
	// Example 3: Parse multiple time ranges
	ranges, err := parser.ParseMulti("Monday and Tuesday from 9am to 5pm", now)
	if err != nil {
		panic(err)
	}
	for i, r := range ranges {
		fmt.Printf("  Range %d: %s to %s (%s)\n", 
			i+1, 
			r.Start(), 
			r.End(),
			r.Duration)
	}
	
}

Supported Expressions

The library can parse a wide variety of natural language time expressions, including:

  • Relative dates: "today", "tomorrow", "next week"
  • Specific dates: "January 15, 2023", "15/01/2023"
  • Time expressions: "3pm", "15:00"
  • Durations: "from 2pm to 4pm", "for 2 hours"
  • Combined expressions: "tomorrow from 9am to 5pm"
  • Recurring times: "every Monday", "every weekday"

How It Works

The naturaltime library embeds the JavaScript code from chrono-node and executes it within a Go application using the goja JavaScript runtime. This approach provides the rich natural language parsing capabilities of chrono-node while maintaining a pure Go API.

License

MIT

Acknowledgments

  • chrono-node - The underlying JavaScript natural language date parser
  • goja - The JavaScript runtime in Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

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

Parser provides natural language parsing capabilities for time expressions. It uses a JavaScript implementation embedded in the Go binary.

func New

func New() (*Parser, error)

New creates a new natural time expression parser. It initializes the JavaScript runtime and prepares the parsing functions.

Returns:

  • An initialized Parser
  • An error if initialization fails

func (*Parser) ParseDate

func (p *Parser) ParseDate(expr string, base time.Time) (*time.Time, error)

ParseDate parses a natural language date expression and returns the corresponding time.

Parameters:

  • expr: The natural language expression to parse
  • base: The reference time for relative expressions

Returns:

  • A pointer to the parsed time.Time, or nil if the expression could not be parsed
  • An error if parsing fails

func (*Parser) ParseMulti

func (p *Parser) ParseMulti(expr string, base time.Time) ([]Range, error)

ParseMulti parses a natural language time expression that may represent multiple time ranges.

Parameters:

  • expr: The natural language expression to parse
  • base: The reference time for relative expressions

Returns:

  • A slice of Range objects
  • An error if parsing fails

func (*Parser) ParseRange

func (p *Parser) ParseRange(expr string, base time.Time) (*Range, error)

ParseRange parses a natural language time expression and returns a single time range.

Parameters:

  • expr: The natural language expression to parse
  • base: The reference time for relative expressions

Returns:

  • A pointer to the parsed Range
  • An error if parsing fails or if multiple ranges are returned

type Range

type Range struct {
	Duration time.Duration
	// contains filtered or unexported fields
}

Range is a time range represented as a half-open interval [start, end). The start time is inclusive, and the end time is exclusive.

func NewRange

func NewRange(start time.Time, duration time.Duration) Range

NewRange creates a new Range with the given start time and duration.

func RangeFromTimes

func RangeFromTimes(start, end time.Time) Range

RangeFromTimes creates a Range from two times. The start time should be before or equal to the end time.

func (Range) Contains

func (r Range) Contains(t time.Time) bool

Contains checks if the given time is within this range. The range is half-open: [start, end), so start <= t < end.

func (Range) ContainsRange

func (r Range) ContainsRange(other Range) bool

ContainsRange checks if another range is fully contained within this range.

func (Range) End

func (r Range) End() time.Time

End returns when the range ends (exclusive).

func (Range) Equal

func (r Range) Equal(other Range) bool

Equal returns true if the two ranges are exactly equal, having the same start time and duration.

func (Range) Intersection

func (r Range) Intersection(other Range) Range

Intersection returns the overlapping range between two ranges, or a zero-duration range at the latest start time if they don't overlap.

func (Range) IsAllDay

func (r Range) IsAllDay() bool

IsAllDay returns true if the range has zero duration.

func (Range) Overlaps

func (r Range) Overlaps(other Range) bool

Overlaps checks if this range overlaps with another range. Two ranges overlap if they share any common time point. Note: If 'other' starts exactly at the same time as r.End(), it is not considered an overlap. Similarly, if r starts exactly at the same time as other.End(), it is also not considered an overlap.

func (Range) Start

func (r Range) Start() time.Time

Start returns when the range begins (inclusive).

func (Range) String

func (r Range) String() string

String returns a human-readable representation of the range.

func (Range) Union

func (r Range) Union(other Range) Range

Union returns the smallest range that contains both ranges. Note: This only makes sense if the ranges overlap or are adjacent.

Jump to

Keyboard shortcuts

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