strftime

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 4 Imported by: 0

README

strftime

License Build Status GoDoc Coverage Status Go Report Card

A Go package for strftime formatting

Package strftime provides functionality for formatting time based on the C strftime(3) function.

The Go standard library time package uses layout strings based on the reference time Mon Jan 2 15:04:05 MST 2006 (AKA 01/02 03:04:05PM ‘06 -0700). The purpose of the strftime package is not to replace the time package layout based formatting or encourage Go developers to punt on learning to use the reference time. Instead this package exists for those cases where it's necessary to use strftime conversion specifications, such as when receiving timestamps from non Go systems which provide a format description using strftime conventions.

License

This source code of this package is released under the MIT License. Please see the LICENSE for the full content of the license.

Usage

The API if the strftime package is meant to be simple and aligned with the API of the Go standard library time package. It has two functions, Parse() for converting a string into a time.Time using a strftime format string, and Format() for creating a string representation of a time.Time using the provided strftime format string.

package main

import (
	"fmt"
	"github.com/csotherden/strftime"
	"time"
)

func main() {
	t, _ := strftime.Parse("%a %b  %e %H:%M:%S %Y", "Wed Feb  4 21:00:57 2009")
	fmt.Println(t.Format(time.ANSIC))

	fmt.Println(strftime.Format("%a %b  %e %H:%M:%S %Y", t))
}
Conversion Specifications

This package attempts to comply with the C strftime(3) function closely as reasonably possible. The format specification strings contain special character sequences called conversion specifications. Conversion specifications are prefixed by the % character. At this time all conversion specifications are supported with the exception of modifiers.

Localization

Support for localization is currently mixed. Conversion specifications for individual time fields should be fully localized. However, conversion specifications that map to different timestamps formats depending on locale (for example %c, %x, and %X) only support the en_US localization.

Documentation

Overview

Package strftime provides functionality for formatting time based on the C strftime(3) function. http://man7.org/linux/man-pages/man3/strftime.3.html

The Go standard library time package uses layout strings based on the reference time Mon Jan 2 15:04:05 MST 2006 (AKA 01/02 03:04:05PM ‘06 -0700). The purpose of the strftime package is not to replace the time package layout based formatting or encourage Go developers to punt on learning to use the reference time. Instead this package exists for those cases where it's necessary to use strftime conversion specifications, such as when receiving timestamps from non Go systems which provide a format description using strftime conventions.

Conversion Specifications

This package attempts to comply with the C strftime(3) function closely as reasonably possible. The format specification strings contain special character sequences called conversion specifications. Conversion specifications are prefixed by the % character. At this time all conversion specifications are supported with the exception of modifiers.

Localization

Support for localization is currently mixed. Conversion specifications for individual time fields should be fully localized. However, conversion specifications that map to different timestamps formats depending on locale (for example %c, %x, and %X) only support the en_US localization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

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

Format returns the provided time.Time formatted according to the strftime(3) based format string

func Parse

func Parse(format, value string) (time.Time, error)

Parse parses a formatted string and returns the time.Time value it represents. The format defines the input value format using C strftime(3) conversion specifications.

Types

This section is empty.

Jump to

Keyboard shortcuts

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