Giter Site home page Giter Site logo

go-anytime's People

Contributors

danielhouston avatar ijt avatar sinchang avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-anytime's Issues

Replace* funcs don't preserve spaces

For example

s := "next week I'd like to set up a Japan trip from Jan 21st to Feb 15th just like the one from last year."
got, err := ReplaceRangesByFunc(s, now, func(r Range) string { return "RANGE" })

gives back an answer with incorrect whitespace.

A Partition() func could be useful

It could look something like this:

anytime.PartitionTimes("Let's meet Thursday or Friday if you're free") => []any{"Let's meet", time.Time{...}, "or", time.Time{...}, "if you're free"}

Alternatively there could be something based on callbacks:

anytime.PartitionTimesByFunc("Let's meet Thursday or Friday", func(s string) {/* non-time */}, func(t time.Time, s string) {/* time */})

That would be more type safe.

mar-24 dates not getting default direction

For example, for now=time.Date(2022, 11, 1, 0, 0, 0, 0, time.UTC), and direction=future, we expect time.Date(2023, 3, 24, 0, 0, 0, 0, time.UTC), but get time.Date(2022, 3, 24, 0, 0, 0, 0, time.UTC).

Need awesome badge

This project is listed in Go awesome, so it could have a badge to say that.

`ReplaceTimesByFunc()` and `ReplaceRangesByFunc()`

It would be useful to have functions that replace the times or time ranges within a string, typically to put them into a standardized format or just to label them. For example,

ReplaceTimesByFunc("Let's meet on Tuesday at 11am UTC", time.Now(), func(t time.Time) string { return t.String() })

would return something like

"Let's meet 2022-10-25 12:00:00 +0000 UTC"

noon

Of course we want to handle "noon".

ReplaceTimesByFunc gets called on bare integers because it thinks they are years

For example, if you run ReplaceTimesByFunc on the string "1000 widgets" the current version of the date parser thinks 1000 is a year, so ReplaceTimesByFunc would replace that number with whatever is returned by the passed-in func(time.Time) string.

There are at least three possible ways to deal with this:

  1. Don't allow bare years to be treated as dates. Require either a month like "September 1000" or an epoch like "1000AD".
  2. Don't bother with years before 2000 since that will probably be enough for most use cases.
  3. Let the caller figure it out by passing in the raw string that was parsed to get the time.Time.

Option 2 would mean ints that happen to match 2\d{3} would always get treated as years. That seems problematic.

Option 3 would have been nice to have included in the original signature. However, it's not backwards compatible with the existing release, so it would require a major version bump. Also, it would not necessarily provide enough context for the caller to determine whether a year really is meant.

Option 1 seems like the best of the three since it is more likely to produce the intended result.

`ReplaceTimesByFunc` takes 1.5ms on a longish string on an M1 Studio Mac

func BenchmarkReplaceTimesByFunc(b *testing.B) {
	s := `'Twas brillig and the slithy toves did gyre and gimble in 50 wabe. All mimsy were the borogroves and the mome raths outgrabe. Beware the jabberwock my son, the jaws that bite January 2020 the claws that catch. Avoid the jubjub bird and shun the frumious bandersnatch. He took his vorpal sword in hand, longtime...`
	want := strings.ReplaceAll(s, "January 2020", "<time>")
	for i := 0; i < b.N; i++ {
		got, err := ReplaceTimesByFunc(s, now, func(t time.Time) string {
			return "<time>"
		})
		if err != nil {
			b.Fatal(err)
		}
		if got != want {
			b.Fatalf("\ngot  %q\nwant %q", got, want)
		}
	}
}

Output:

/private/var/folders/b7/h46kx5zx4m985db8s__9trdc0000gn/T/GoLand/___BenchmarkReplaceTimesByFunc_in_github_com_ijt_go_anytime.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkReplaceTimesByFunc\E$ -test.run ^$
goos: darwin
goarch: arm64
pkg: github.com/ijt/go-anytime
BenchmarkReplaceTimesByFunc
BenchmarkReplaceTimesByFunc-10    	     738	   1495768 ns/op
PASS

This leaves a lot to be desired for heavy use.

Handle tricky implicit ranges

Description

I'm currently working on implicit date ranges like "today", "last week", and the like. For example, "today" begins at 0:0:0 today and ends at 0:0:0 tomorrow, exclusive, so that defines a range.

One way to detect those ranges in many cases is to find the first time field that is a default (1 for date fields, 0 for time fields) and then go up a level and add one unit of that. So for example with "today" you typically find that the first defaulted field is the hours (truncated to 0) and so you add a day (24h) to today and get tomorrow at 0:0:0. However, that fails in cases like when the user specifies Jan 2022 since Jan is month 1. They don't mean all of the year 2022. They mean the month of January in 2022.

So the resolution info has to come through somehow, though it's not currently available in the parser generated by RangeParser(). One way to solve the problem would be to have the datetime Parser() func make a parser returning a Range. Probably to make it more explicit, the Range would be changed to have a start time and a duration. The duration would only be used when the datetime is used as an implicit range.

That affects the API, though the change can be kept to a minimum. The result of Parser() can return a Range, so that is different. However, Parse() can still extract the time.Time and return that as before.

How do we do this in a way that keeps code changes and risk to a minimum? One way would be to embed the start time in the range so you can use it as a time, keeping a lot of code unchanged:

type Range struct {
    time.Time
    Duration time.Duration
}

func (r Range) Start() time.Time {
    return r.time.Time  // not sure about the exact syntax here
}

func (r Range) End() time.Time {
    return r.Start().Add(r.Duration)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.