gostrftime

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: ISC Imports: 5 Imported by: 53

README

gostrftime

Build Status GoDoc Go Report Card License

About

A Go pkg for formatting time.Time in an strftime(3) like way. Basically, strftime for Go, with a couple of additions.

Installing

$ go get github.com/cactus/gostrftime

Supported formats:

code example Description
%A Sunday full weekday name
%a Sun abbreviated weekday name
%B September full month name
%b Sep abbreviated month name
%C 20 (year / 100) as number. Single digits are preceded by zero
%D 09/21/14 equivalent to %m/%d/%y
%d 21 day of month as number. Single digits are preceded by zero
%e 21 day of month as number. Single digits are preceded by a blank
%f 001234 microsecond as a six digit decimal number, zero-padded on the left
%F 2014-09-21 equivalent to %Y-%m-%d
%H 15 the hour (24 hour clock) as a number. Single digits are preceded by zero
%h Sep same as %b
%I 03 the hour (12 hour clock) as a number. Single digits are preceded by zero
%j 264 the day of the year as a decimal number. Single digits are preced by zeros
%k 15 the hour (24 hour clock) as a number. Single digits are preceded by a blank
%L 001 millisecond as a three digit decimal number, zero-padded on the left
%l 11 replaced by the hour (12 hour clock) as a number. Single digits are preceded by blank
%M 32 replaced by the minute as a decimal number. Single digits are preceded by a zero
%m 09 replaced by the month as a decimal number. Single digits are preceded by a zero
%N 001234567 nanosecond as a 9 digit decimal number, zero-padded on the left
%n \n a newline
%P am am or pm as appropriate
%p AM AM or PM as appropriate
%R 15:32 equivalent to %H:%M
%r 03:32:05 AM equivalent to %I:%M:%S %p
%S 05 the second as a number. Single digits are preceded by a zero
%s 1461497457 the number of seconds since the Epoch, UTC
%T 15:32:05 equivalant to %H:%M:%S
%t \t a tab
%v 21-Sep-2014 equivalent to %e-%b-%Y
%w 0 the weekday (Sunday as first day of the week) as a number
%Y 2014 replaced by the year with century as a number
%y 14 year without century as a number. Single digits are preceded by zero
%Z UTC time zone name
%z -0700 the time zone offset from UTC

Using

import (
    "fmt"
    "time"
    "github.com/cactus/gostrftime"
)

func main() {
    now := time.Now()
    fmt.Println(gostrftime.Format("%Y-%m-%d", now))
}

License

Released under an ISC license. See LICENSE.md file for details.

Documentation

Overview

Package gostrftime formats time.Time using strftime(3) conventions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

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

Format returns a textual representation of the time value formatting according to format. format supports most of the formatting methods defined in strftime(3), minus the GNU libc extensions and POSIX locale extensions.

List of accepted format expansion values:

%A  full weekday name (Sunday)
%a  abbreviated weekday name (Sun)
%B  full month name (September)
%b  abbreviated month name (Sep)
%C  (year / 100) as number. Single digits are preceded by zero (20)
%D  equivalent to %m/%d/%y (09/21/14)
%d  day of month as number. Single digits are preceded by zero (21)
%e  day of month as number. Single digits are preceded by a blank (21)
%f  microsecond as a six digit decimal number, zero-padded on the left (001234)
%F  equivalent to %Y-%m-%d (2014-09-21)
%H  the hour (24 hour clock) as a number. Single digits are preceded by zero (15)
%h  same as %b
%I  the hour (12 hour clock) as a number. Single digits are preceded by zero (03)
%j  the day of the year as a decimal number. Single digits are preced by zeros (264)
%k  the hour (24 hour clock) as a number. Single digits are preceded by a blank (15)
%L  millisecond as a three digit decimal number, zero-padded on the left (001)
%l  the hour (12 hour clock) as a number. Single digits are preceded by blank ( 3)
%M  the minute as a decimal number. Single digits are preceded by a zero (32)
%m  the month as a decimal number. Single digits are preceded by a zero (09)
%N  nanosecond as a 9 digit decimal number, zero-padded on the left (001234567)
%n  a newline (\n)
%P  am or pm as appropriate
%p  AM or PM as appropriate
%R  equivalent to %H:%M
%r  equivalent to %I:%M:%S %p
%S  the second as a number. Single digits are preceded by a zero (05)
%s  the number of seconds since the Epoch, UTC
%T  equivalent to %H:%M:%S
%t  a tab (\t)
%v  equivalent to %e-%b-%Y
%w  the weekday (Sunday as first day of the week) as a number. (0)
%Y  the year with century as a number (2014)
%y  year without century as a number. Single digits are preceded by zero (14)
%Z  time zone name (UTC)
%z  the time zone offset from UTC (-0700)
Example
package main

import (
	"fmt"
	"time"

	"github.com/cactus/gostrftime"
)

func main() {
	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
	fmt.Println(gostrftime.Format("%Y-%m-%d", t))
}
Output:

2009-11-10

func Strftime

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

Strftime is an alias for Format

Types

This section is empty.

Jump to

Keyboard shortcuts

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