• Stars
    star
    1,328
  • Rank 35,391 (Top 0.7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A natural language date/time parser with pluggable rules

when godoc

when is a natural language date/time parser with pluggable rules and merge strategies

Examples

  • tonight at 11:10 pm
  • at Friday afternoon
  • the deadline is next tuesday 14:00
  • drop me a line next wednesday at 2:25 p.m
  • it could be done at 11 am past tuesday

Check EN rules and tests of them, for more examples.

Needed rule not found? Open an issue with the case and it will be added asap.

How it works

Usually, there are several rules added to the parser's instance for checking. Each rule has its own borders - length and offset in provided string. Meanwhile, each rule yields only the first match over the string. So, the library checks all the rules and extracts a cluster of matched rules which have distance between each other less or equal to options.Distance, which is 5 by default. For example:

on next wednesday at 2:25 p.m.
   └──────┬─────┘    └───┬───┘
       weekday      hour + minute

So, we have a cluster of matched rules - "next wednesday at 2:25 p.m." in the string representation.

After that, each rule is applied to the context. In order of definition or in match order, if options.MatchByOrder is set to true(which it is by default). Each rule could be applied with given merge strategy. By default, it's an Override strategy. The other strategies are not implemented yet in the rules. Pull requests are welcome.

Supported Languages

  • EN - English
  • RU - Russian
  • BR - Brazilian Portuguese
  • ZH - Chinese
  • NL - Dutch

Install

The project follows the official release workflow. It is recommended to refer to this resource for detailed information on the process.

To install the latest version:

$ go get github.com/olebedev/when@latest

Usage

w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)

text := "drop me a line in next wednesday at 2:25 p.m"
r, err := w.Parse(text, time.Now())
if err != nil {
	// an error has occurred
}
if  r == nil {
 	// no matches found
}

fmt.Println(
	"the time",
	r.Time.String(),
	"mentioned in",
	text[r.Index:r.Index+len(r.Text)],
)

Distance Option

w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)

text := "February 23, 2019 | 1:46pm"

// With default distance (5):
// February 23, 2019 | 1:46pm
//            └───┬───┘
//           distance: 9 (1:46pm will be ignored)

r, _ := w.Parse(text, time.Now())
fmt.Printf(r.Time.String())
// "2019-02-23 09:21:21.835182427 -0300 -03"
// 2019-02-23 (correct)
//   09:21:21 ("wrong")

// With custom distance (10):
w.SetOptions(&rules.Options{
	Distance:     10,
	MatchByOrder: true})

r, _ = w.Parse(text, time.Now())
fmt.Printf(r.Time.String())
// "2019-02-23 13:46:21.559521554 -0300 -03"
// 2019-02-23 (correct)
//   13:46:21 (correct)

State of the project

The project is in a more-or-less complete state. It's used for one project already. Bugs will be fixed as soon as they will be found.

TODO

  • readme: describe all the existing rules
  • implement missed rules for these examples
  • add cli and simple rest api server(#2)

LICENSE

http://www.apache.org/licenses/LICENSE-2.0

More Repositories

1

go-starter-kit

[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Go
2,826
star
2

go-duktape

[abandoned] Duktape JavaScript engine bindings for Go
Go
777
star
3

emitter

Emits events in Go way, with wildcard, predicates, cancellation possibilities and many other good wins
Go
483
star
4

config

JSON or YAML configuration wrapper with convenient access methods.
Go
266
star
5

cdn

[abandoned] Content Delivery Network on the top of MongoDb GridFs with on-the-fly image crop/resize
Go
132
star
6

go-tgbot

Golang telegram bot API wrapper, session-based router and middleware
Go
117
star
7

srlt

Simple tool for save and restore states of all existing repositories on the given path
Go
53
star
8

staticbin

Gin middleware/handler for serving static files from binary data
Go
43
star
9

hook-to-trello

Github & Bitbucket web hooks handler for Trello
LiveScript
28
star
10

gojax

A set of extensions for goja javascript engine
Go
27
star
11

swarm

A CRDT-backed reactive real-time data with no merge conflicts, with offline mode. For business-critical data-driven apps on intermittently connected devices.
JavaScript
27
star
12

go-gamp

Google Analytics Measurement Protocol in Golang
Go
16
star
13

gin-cache

Tiny and simple cache middleware for gin framework
Go
15
star
14

rest

REST interface over MongoDB, as middlware for martini framework.
Go
14
star
15

go-duktape-fetch

Server side fetch polyfill for go-duktape
Go
12
star
16

on

CLI for fsnotify
Go
8
star
17

chat

SwarmDB Example Chat Application
JavaScript
4
star
18

node-mystem

Node.js wrapper for `MyStem` morphology text analyzer by Yandex.ru
LiveScript
4
star
19

todo

SwarmDB Example TodoMVC Application. Demo 👉
JavaScript
3
star
20

pinentry-mac-keychain

A pinentry program for macOs that stores entered PINs in the macOS KeyChain. Convenient when use with smart cards, like Yubikey
Go
2
star
21

var

tool to fill in json/yaml stdin stream from the environment variables
Go
2
star
22

t2s

Text-To-Speech tool for Russian language, written in Golang
Go
1
star
23

pickle.js

JavaScript implementation of the Python pickle format. Fork.
JavaScript
1
star
24

dotfiles

$HOME sweet home
Vim Script
1
star
25

mice

SwarmDB Example Mice Application
JavaScript
1
star
26

go-googl

A Golang library for https://goo.gl URL shortener API
Go
1
star