strftime

package module
v0.0.0-...-d82166e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2015 License: BSD-3-Clause Imports: 5 Imported by: 16

README

go-strftime

go-strftime is a Golang library that implements the Python-like Time Format Function strftime().

According to this discussion Issue 444: Implement strftime, in the eyes of the Golang designer, the strftime() is a bad interface and should be replaced by time.Format() because the later one has a simpler format representation. However the formats could be used in time.Format() is strictly limited to a few pre-defined constants. Sometimes it would become error-prone and tricky to use the format right to produce what format programmers want exactly other than the pre-defineds.

So this library is created to provide a convenient interface for those who like to use strftime() in Golang.

Usage

Get the code down using the standard go tool:

go get github.com/hhkbp2/go-strftime

and write your code like

import "github.com/hhkbp2/go-strftime"

formatted := stftime.Format("%Y-%m-%d %H:%M:%S %9n", time.Now())

Please refer to the Python strftime doc for all the supported format strings. Note that there are two differences:

  1. %c returns RFC1123 which is a bit different from what Python does
  2. %[1-9]n is added for fractional second. e.g. "%3n" means to print millisecond offset with this second. "%6n" means to print microsecond. "%9n" is for nanosecond.
Contributors

This library is originally developed by Miki Tebeka and forked by Dylan Wen to add fractional format support.

The original repository could be found at https://bitbucket.org/tebeka/strftime.

Documentation

Overview

Implementation of Python's strftime in Go

Example:

str, err := strftime.Format("%Y/%m/%d", time.Now()) // 2012/12/07

Directives:

%a - Locale’s abbreviated weekday name
%A - Locale’s full weekday name
%b - Locale’s abbreviated month name
%B - Locale’s full month name
%c - Locale’s appropriate date and time representation
%d - Day of the month as a decimal number [01,31]
%H - Hour (24-hour clock) as a decimal number [00,23]
%I - Hour (12-hour clock) as a decimal number [01,12]
%j - Day of year
%m - Month as a decimal number [01,12]
%M - Minute as a decimal number [00,59]
%p - Locale’s equivalent of either AM or PM
%S - Second as a decimal number [00,61]
%U - Week number of the year
%w - Weekday as a decimal number
%W - Week number of the year
%x - Locale’s appropriate date representation
%X - Locale’s appropriate time representation
%y - Year without century as a decimal number [00,99]
%Y - Year with century as a decimal number
%Z - Time zone name (no characters if no time zone exists)

Note that %c returns RFC1123 which is a bit different from what Python does

Index

Constants

View Source
const (
	WEEK = time.Hour * 24 * 7
)

Variables

This section is empty.

Functions

func Format

func Format(format string, t time.Time) string

Format return string with % directives expanded. Will return error on unknown directive.

func FormatTo

func FormatTo(w io.Writer, format string, t time.Time) (n int, err error)

Types

type FormatFunc

type FormatFunc func(t time.Time) string

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

func NewFormatter

func NewFormatter(format string) *Formatter

func (*Formatter) Format

func (self *Formatter) Format(t time.Time) string

func (*Formatter) FormatTo

func (self *Formatter) FormatTo(w io.Writer, t time.Time) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL