cyucelen / marker

🖍️ Marker is the easiest way to match and mark strings for colorful terminal outputs!
MIT License
47 stars 13 forks source link

Match time format #8

Open cyucelen opened 4 years ago

cyucelen commented 4 years ago

Create a matcher which takes one of the time layouts of golang standard time library and matches them in given string.

const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // Handy time stamps.
    Stamp      = "Jan _2 15:04:05"
    StampMilli = "Jan _2 15:04:05.000"
    StampMicro = "Jan _2 15:04:05.000000"
    StampNano  = "Jan _2 15:04:05.000000000"
)

An example:

strWithTimestamp := "Current timestamp is 2019-10-07T23:45:05Z03:00"
match := MatchTimestamp(time.RFC3339)(strWithTimestamp)
// expected match => Match{Template: "Current timestamp is %s", Patterns: []string{"2019-10-07T23:45:05Z03:00"}}

Distinct implementations can be written for each format. We can discuss to split the issue to fine grained pieces on the way with respect to complexity.

marco-silveira commented 4 years ago

Hi @cyucelen. What you're asking for is a function that given some string returns witch layout corresponds to it, or a func that converts one format to another?

cyucelen commented 4 years ago

Hi @MarcoAntonioSilveira. I just updated the description with some detail about what are we expecting from this feature. I suggest you to review existing matchers to have an idea about how marker works. Feel free to ask more questions.

marco-silveira commented 4 years ago

@cyucelen I must pay attention to the date being valid or only match the layout without worrying about the correctness of the date? (Mon Feb 31 23:59:59 2006 with ANSIC layout would be marked in this case)

cyucelen commented 4 years ago

You don't need to check if its a valid date or not. Just match for the layout.

Validation can be another feature for another issue and can be implemented later.

marco-silveira commented 4 years ago

@cyucelen Could you assing me this issue? I'm working on it

cyucelen commented 4 years ago

@MarcoAntonioSilveira looks like time.Stamp matches more than one layouts' parts (i.e. ANSIC, UnixDate, StampMilli, StampMicro, StampNano). I created a new branch match-timestamp and added a test for it. Take a look and lets discuss can we do something about it.

marco-silveira commented 4 years ago

@cyucelen maybe we can filter those cases with regexps. The conflicts with other stamps (StampMilli, etc) looks like it's easy to solve, some time soon I will try some ideas.

But in the ANSIC and UnixDate cases, is it really a wrong match? The pattern is there. In case you do think those cases should be treated, maybe it's possible to do it with regexps to avoid matching when preceded by the abbreviations of days of the week, since these doesn't conflict with other real worlds (I guess).

What do you think?

cyucelen commented 4 years ago

Lets fix the overlap with StampMilli, StampMicro and Stampnano first. Then we can discuss ANSIC and UnixDate cases whether its a problem or not.

cyucelen commented 4 years ago

Any updates @MarcoAntonioSilveira, do you need help?

marco-silveira commented 4 years ago

@cyucelen sorry, I've been really busy lately. My solution to the overlap with the other stamps was using negative lookahead to filter out the ".[0-9]" part, but golang's regexp package doesn't support negative lookaheads.

To date I couldn't think of some way to do it using regexp without including the whitespace after the layout in the match. Take a look: https://regex101.com/r/tDMEIN/1

If you have any suggestions, it would be great.