Giter Club home page Giter Club logo

sft's Introduction

sft

Go code generator for converting a time.Time value to a string representation.

Description

This program is designed to be run from the command line with a time formatting string, and it will emit Go source code that formats time.Time values according to the specified formatting string.

$ sft -m -o formatTime.go -f formatTime '%F %T'
$ go run formatTime.go
$ less formatTime.go

The resultant code can be copied and pasted into another Go source file, or the file can simply be compiled into another project. The name of the function it creates and the name of the package it uses can be changed on the command line. Removing the -m command line skips creating a main function, and outputs a single function with the specified function and package name.

The program could also be invoked from a Go generate statement in other Go source code.

//go:generate sft -f formatTime -o formatTime.go '%F %T'

Then when the time format spec changes, simply type go generate at the command line to regenerate the time formatting function.

Performance

It is a bit faster than the Go standard library time formatting functionality.

$ karrick@promethium sft % make clean bench
rm -f append copy sft append.go copy.go append_test.go copy_test.go
go build -o sft main.go
./sft -extra -f appendTime -append -o append_test.go RFC3339Nano
./sft -extra -f copyTime -o copy_test.go RFC3339Nano
go test -bench=. -benchmem main_test.go append_test.go copy_test.go
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkAppendTime-16         	19457082	        55.87 ns/op	       0 B/op	       0 allocs/op
BenchmarkCopyTime-16           	21264758	        54.54 ns/op	       0 B/op	       0 allocs/op
BenchmarkStandardLibrary-16    	 5772063	       203.4 ns/op	      32 B/op	       1 allocs/op
PASS
ok  	command-line-arguments	3.764s

sft's People

Watchers

Karrick McDermott avatar James Cloos avatar  avatar

sft's Issues

Optimize placement of values

For many time formats there is little ambiguity for where specific portions of the output will be placed in the final slice of bytes. This is especially true when there is no variability in the output format, such as fixed width representations of portions of the time and date stamp. In these circumstances, we can know the exact offset of the data to be placed. Furthermore, if there is variable width data in the output, after a variable width field, we know relative offsets of follow on fixed width fields with respect to the end of the variable width field.

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.