Categorygithub.com/knz/strtime
repositorypackage
1.0.0
Repository: https://github.com/knz/strtime.git
Documentation: pkg.go.dev

# README

strtime: stable strptime / strftime for Go

strftime and strptime are functions found in nearly all C library implementations but with slightly different behaviors on each platform.

To provide identical behavior on all platforms where Go is supported, this strtime package provides two functions Strftime and Strptime which can be used in lieu of the platform's native C implementation.

To achieve this strtime:

  • embeds and extends a Strftime function in Go, using an implementation originally from leekchan's timeutil package, which performs the format conversion natively in Go and is licensed under the terms of the standard MIT license; and
  • embeds the standard FreeBSD implementation of strptime in C. FreeBSD's implementation is mature, robust, has the widest format specifier coverage, and its code is reusable under the terms of the permissive BSD license.

strtime is itself offered under the terms of the 2-clause BSD license.

How to use

func Strptime(value string, layout string) (time.Time, error)
func Strftime(t time.Time, layout string) (string, error)

Examples:

   start := "2016-10-20"
   t, _ := strtime.Strptime(start, "%Y-%m-%d")
   end, _ := strtime.Strftime(t, "%Y-%m-%d")
   Fmt.Println(start, end)

Supported format specifiers

FormatDescriptionNotes
%aShort week day ("mon", "tue", etc)
%ALong week day ("monday", "tuesday", etc)
%bShort month name ("jan", "feb" etc)
%BLong month name ("january", "february" etc)
%cEquivalent to %a %b %e %H:%M:%S %Y
%CCenturyOnly reliable for years -9999 to 9999
%dDay of month 01-31
%DEquivalent to %m/%d/%y
%eLike %d but leading zeros are replaced by a space.
%fFractional part of a second with nanosecond precision, e.g. "123" is 123ms; "123456" is 123456µs, etc.Strftime always formats using 6 digits.
%FEquivalent to %Y-%m-%d
%hEquivalent to %b
%HHours 00-23See also %k
%IHours 01-12See also %p, %l
%jDay of year 000-366
%kHours 0-23 (padded with spaces)See also %H
%lHours 1-12 (padded with spaces)See also %I
%mMonth 01-12
%MMinutes 00-59
%nA newline character
%pAM/PMOnly valid when placed after hour-related specifiers. See also %I, %l
%rEquivalent to %I:%M:%S %p
%REquivalent to %H:%MSee also %T
%sNumber of seconds since 1970-01-01 00:00:00 +0000 (UTC)
%SSeconds 00-59
%tA tab character
%TEquivalent to %H:%M:%SSee also %R
%uThe day of the week as a decimal, range 1 to 7, Monday being 1See also %w
%UThe 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 01See also %W
%wThe day of the week as a decimal, range 0 to 6, Sunday being 1See also %u
%WThe 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 01See also %U
%xEquivalent to %D
%XEquivalent to %T
%yYear without a century 00-99Years 00-68 are 2000-2068
%YYear including the century
%zTime zone offset +/-NNNNStrftime always prints +0000
%ZUTC or GMTStrftime always prints UTC