lctime: github.com/variadico/lctime Index | Examples | Files | Directories

package lctime

import "github.com/variadico/lctime"

Package lctime provides a way to format dates and times using strftime directives. More importantly, it does so in a locale-aware fashion. This allows developers to format time based on a user's locale.

An initial locale is loaded at import time. It's determined by the first, non-empty locale identifier from these environment variables.

1. LC_TIME
2. LC_ALL
3. LANG

If all of the previous variables are empty, then POSIX will be the initial locale used. All locales use UTF-8 character encoding.

The formats used are loosely based on glibc locale files.

These are the supported strftime directives. They're loosely based on The Open Group Base Specifications Issue 7, http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html.

%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
%C  year divided by 100 and truncated to an integer
%d  day of the month as a decimal number [01,31]
%D  %m/%d/%y
%e  day of the month as a decimal number [1,31]
%F  %Y/%m/%d
%g  last 2 digits of the week-based year as a decimal number
%G  week-based year as a decimal number (for example, 1977)
%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 the year as a decimal number [001,366]
%m  month as a decimal number [01,12]
%M  minute as a decimal number [00,59]
%n  returns a newline
%p  locale's equivalent of either a.m. or p.m.
%r  time in a.m. and p.m. notation.
%R  time in 24-hour notation %H:%M
%S  second as a decimal number [00,60]
%t  returns a tab
%T  %H:%M:%S
%u  weekday as a decimal number [1,7]
%U  week number of the year as a decimal number [00,53]
%V  week number of the year
%w  weekday as a decimal number [0,6]
%W  week number of the year as a decimal number [00,53]
%x  locale's appropriate date representation
%X  locale's appropriate time representation
%y  last two digits of the year as a decimal number [00,99]
%Y  year as a decimal number (for example, 1997)
%z  offset from UTC in the ISO 8601:2000 standard format
%Z  timezone name or abbreviation
%%  %

Index

Examples

Package Files

directives.go lctime.go parse.go

Variables

var (
    // ErrNoLocale is returned when a given locale was not found.
    ErrNoLocale = errors.New("Locale not found")
    // ErrCorruptLocale is returned if a given locale file is corrupted.
    ErrCorruptLocale = errors.New("Corrupted locale")
)

func GetLocale Uses

func GetLocale() string

GetLocale returns the currently active locale.

func GetLocales Uses

func GetLocales() []string

GetLocales returns a slice of available locales.

func SetLocale Uses

func SetLocale(id string) error

SetLocale activates the given locale.

func Strftime Uses

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

Strftime formats a time.Time. It's locale-aware, so make sure you call SetLocale if needed.

Code:

// Initial locale based on env vars. If not set, then POSIX is used.
t := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
fmt.Println(Strftime("%c", t))

Output:

Sun 02 Jan 2000 03:04:05 AM UTC

Code:

SetLocale("es_MX")
t := time.Date(2015, 12, 25, 3, 2, 1, 0, time.UTC)
fmt.Println(Strftime("%A, %d de %B de %Y", t))

Output:

viernes, 25 de diciembre de 2015

Directories

PathSynopsis
internal/locales

Package lctime imports 9 packages (graph). Updated 2017-02-19. Refresh now. Tools for package owners.