Skip to content
Permalink
Browse files

Remove dep on github.com/jeffjen/datefmt by creating simpler datefmt

  • Loading branch information...
frioux committed Apr 7, 2019
1 parent dd53022 commit d23a644765300cdd37fd26afdb490a3cbd1ea816
1 go.mod
@@ -10,7 +10,6 @@ require (
github.com/headzoo/ut v0.0.0-20181013193318-a13b5a7a02ca // indirect
github.com/icza/backscanner v0.0.0-20180226082541-a77511ef4f0f
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 // indirect
github.com/jeffjen/datefmt v0.0.0-20181218074549-3f3666754541
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0
github.com/mmcdole/gofeed v1.0.0-beta2
2 go.sum
@@ -21,8 +21,6 @@ github.com/icza/backscanner v0.0.0-20180226082541-a77511ef4f0f h1:EEBVjzvzsiUwgW
github.com/icza/backscanner v0.0.0-20180226082541-a77511ef4f0f/go.mod h1:mxjJy4NhAIQcnIf3X9PQBSjLMy+cKlTMgXsSNmyOtMA=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
github.com/jeffjen/datefmt v0.0.0-20181218074549-3f3666754541 h1:a6ZngAhh0srJBJd3PkLxerdpY0PSEOHt7qH/wHgQqFM=
github.com/jeffjen/datefmt v0.0.0-20181218074549-3f3666754541/go.mod h1:9DenzL2pEjSREEHXX/L3Wxg9MlhBMx05LmfQD88uyno=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -7,8 +7,10 @@ import (
"io"
"os"
"strconv"
"strings"
"time"

"github.com/frioux/leatherman/pkg/datefmt"
"github.com/pkg/errors"
)

@@ -33,6 +35,20 @@ func parseArgs(args []string) error {
return nil
}

func parseDate(format, input string) (time.Time, error) {
if strings.ContainsRune(format, '%') {
format = datefmt.TranslateFormat(format)
}
return time.Parse(format, input)
}

func formatDate(format string, date time.Time) (string, error) {
if strings.ContainsRune(format, '%') {
format = datefmt.TranslateFormat(format)
}
return date.Format(format), nil
}

// Run takes dates on stdin in format -i, will group them by format -g,
// and write them in format -o.
func Run(args []string, stdin io.Reader) error {

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,69 @@
package datefmt

import "strings"

type replacement struct {
from, to string
}

// Mon Jan 2 15:04:05 -0700 MST 2006
var replacements = []replacement{
{"%a", "Mon"},
{"%A", "Monday"},
{"%b", "Jan"},
{"%B", "January"},
// %c The preferred date and time representation for the current locale.
{"%C", "06"},
{"%d", "02"},
{"%D", "01/02/06"},
// %e Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU) (Calculated from tm_mday.)
// %E Modifier: use alternative format, see below. (SU)
{"%F", "2006-01-02"},
// %G The ISO 8601 week-based year (see NOTES) with century as a decimal number. The 4-digit year corresponding to the ISO week number (see %V). This has the same format and
// value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ) (Calculated from tm_year, tm_yday, and tm_wday.)
// %g Like %G, but without century, that is, with a 2-digit year (00–99). (TZ) (Calculated from tm_year, tm_yday, and tm_wday.)
{"%h", "Jan"},
{"%H", "15"},
{"%I", "03"},
// %j The day of the year as a decimal number (range 001 to 366). (Calculated from tm_yday.)
// %k The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also %H.) (Calculated from tm_hour.) (TZ)
// %l The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also %I.) (Calculated from tm_hour.) (TZ)
{"%m", "01"},
{"%M", "04"},
{"%n", "\n"},
// %O Modifier: use alternative format, see below. (SU)
{"%p", "PM"},
{"%P", "pm"},
{"%r", "03:04:05 p.m."},
{"%R", "15:03"},
// %s The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ) (Calculated from mktime(tm).)
{"%S", "05"},
{"%t", "\t"},
{"%T", "15:04:05"},
{"%u", "1"},
// %U The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also %V and %W. (Calculated from
// tm_yday and tm_wday.)
// %V The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year. See
// also %U and %W. (Calculated from tm_year, tm_yday, and tm_wday.) (SU)
// %w The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u. (Calculated from tm_wday.)
// %W The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01. (Calculated from tm_yday and tm_wday.)
// %x The preferred date representation for the current locale without the time.
// %X The preferred time representation for the current locale without the date.
{"%y", "06"},
{"%Y", "2006"},
{"%z", "-7000"},
{"%Z", "MST"},
// %+ The date and time in date(1) format. (TZ) (Not supported in glibc2.)
// %% A literal '%' character.
}

// TranslateFormat replaces many of the most common strftime(3) formats to go
// formats. See source for supported and unsupported formats.
func TranslateFormat(in string) string {
ret := in
for _, r := range replacements {
ret = strings.ReplaceAll(ret, r.from, r.to)
}

return ret
}
@@ -0,0 +1,12 @@
package datefmt

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTranslateFormat(t *testing.T) {
assert.Equal(t, "2006-01-02", TranslateFormat("%F"))
assert.Equal(t, "2006-01-02T15:04:05", TranslateFormat("%FT%T"))
}

0 comments on commit d23a644

Please sign in to comment.
You can’t perform that action at this time.