Help us shape the future of Go! Take the 2026 Go Developer Survey

ctimefmt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2020 License: MIT Imports: 4 Imported by: 3

README

ctimefmt

GoDoc

strptime/strftime compatible syntax (e.g. "%Y-%m-%d %H:%M:%S %Z") for Go.

Installation

go get github.com/bluemedora/ctimefmt

Example

package main

import (
    "fmt"
    "time"
    "github.com/Mottl/ctimefmt"
)

func main() {
    now := time.Now()
    // Format() function formats Time struct:
    fmt.Println(ctimefmt.Format("%Y-%m-%d %H:%M:%S.%f %Z", now))

    // ToNative() converts ctime-like format string to Go native layout:
    s := ctimefmt.ToNative("%Y-%m-%d %H:%M:%S.%f %Z")
    fmt.Println(now.Format(s)))

    // Parse() parses ctime-like syntax to Time struct:
    if then, err := ctimefmt.Parse("%Y-%m-%d %H:%M:%S", "2019-02-19 17:25:05"); err == nil {
        fmt.Println(then)
    } else {
        fmt.Println("Error parsing time:", err)
    }
}

License

Use of this package is governed by MIT license that can be found in the LICENSE file.

Documentation

Overview

strptime/strftime compatible syntax support, e.g. "%Y-%m-%d %H:%M:%S %Z".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

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

Format returns a textual representation of the time value formatted according to ctime-like format string. Possible directives are:

%Y - Year, zero-padded (0001, 0002, ..., 2019, 2020, ..., 9999)
%y - Year, last two digits, zero-padded (01, ..., 99)
%m - Month as a decimal number (01, 02, ..., 12)
%o - Month as a space-padded number ( 1, 2, ..., 12)
%q - Month as a unpadded number (1,2,...,12)
%b, %h - Abbreviated month name (Jan, Feb, ...)
%B - Full month name (January, February, ...)
%d - Day of the month, zero-padded (01, 02, ..., 31)
%e - Day of the month, space-padded ( 1, 2, ..., 31)
%g - Day of the month, unpadded (1,2,...,31)
%a - Abbreviated weekday name (Sun, Mon, ...)
%A - Full weekday name (Sunday, Monday, ...)
%H - Hour (24-hour clock) as a zero-padded decimal number (00, ..., 24)
%I - Hour (12-hour clock) as a zero-padded decimal number (00, ..., 12)
%l - Hour (12-hour clock: 0, ..., 12)
%p - Locale’s equivalent of either AM or PM
%P - Locale’s equivalent of either am or pm
%M - Minute, zero-padded (00, 01, ..., 59)
%S - Second as a zero-padded decimal number (00, 01, ..., 59)
%L - Millisecond as a decimal number, zero-padded on the left (000, 001, ..., 999)
%f - Microsecond as a decimal number, zero-padded on the left (000000, ..., 999999)
%s - Nanosecond as a decimal number, zero-padded on the left (000000, ..., 999999)
%z - UTC offset in the form ±HHMM[SS[.ffffff]] or empty(+0000, -0400)
%Z - Timezone name or abbreviation or empty (UTC, EST, CST)
%D, %x - Short MM/DD/YY date, equivalent to %m/%d/%y
%F - Short YYYY-MM-DD date, equivalent to %Y-%m-%d
%T, %X - ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
%r - 12-hour clock time (02:55:02 pm)
%R - 24-hour HH:MM time, equivalent to %H:%M
%n - New-line character ('\n')
%t - Horizontal-tab character ('\t')
%% - A % sign
%c - Date and time representation (Mon Jan 02 15:04:05 2006)

func Parse

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

Parse parses a ctime-like formatted string (e.g. "%Y-%m-%d ...") and returns the time value it represents.

Refer to Format() function documentation for possible directives.

func ToNative

func ToNative(format string) (string, error)

ToNative converts ctime-like format string to Go native layout (which is used by time.Time.Format() and time.Parse() functions).

Types

This section is empty.

Jump to

Keyboard shortcuts

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