httpdate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2017 License: MIT Imports: 9 Imported by: 10

README

go-httpdate

Build Status Coverage Status MIT License GoDoc

Description

Well dealing the date formats used by the HTTP protocol (and then some more).
Str2Time is tremendous function which can detect various date formats from string and returns time.Time.

Is is golang porting of perl's HTTP::Date (ported only str2time interface)

Supported Format

  • "Wed, 09 Feb 1994 22:23:32 GMT" -- HTTP format
  • "Thu Feb 3 17:03:55 GMT 1994" -- ctime(3) format
  • "Thu Feb 3 00:00:00 1994", -- ANSI C asctime() format
  • "Tuesday, 08-Feb-94 14:15:29 GMT" -- old rfc850 HTTP format
  • "Tuesday, 08-Feb-1994 14:15:29 GMT" -- broken rfc850 HTTP format
  • "03/Feb/1994:17:03:55 -0700" -- common logfile format
  • "09 Feb 1994 22:23:32 GMT" -- HTTP format (no weekday)
  • "08-Feb-94 14:15:29 GMT" -- rfc850 format (no weekday)
  • "08-Feb-1994 14:15:29 GMT" -- broken rfc850 format (no weekday)
  • "1994-02-03 14:15:29 -0100" -- ISO 8601 format
  • "1994-02-03 14:15:29" -- zone is optional
  • "1994-02-03" -- only date
  • "1994-02-03T14:15:29" -- Use T as separator
  • "19940203T141529Z" -- ISO 8601 compact format
  • "19940203" -- only date
  • "08-Feb-94" -- old rfc850 HTTP format (no weekday, no time)
  • "08-Feb-1994" -- broken rfc850 HTTP format (no weekday, no time)
  • "09 Feb 1994" -- proposed new HTTP format (no weekday, no time)
  • "03/Feb/1994" -- common logfile format (no time, no offset)
  • "Feb 3 1994" -- Unix 'ls -l' format
  • "Feb 3 17:03" -- Unix 'ls -l' format
  • "11-15-96 03:52PM" -- Windows 'dir' format

See Also

Author

Songmu

Documentation

Overview

Package httpdate provides functions that deal the date formats used by the HTTP protocol (and then some more). Only the two functions, Time2Str() and Str2Time(), are provided. Str2Time() is tremendous function which automatically detect various date format and returns `time.Time`.

Is is golang porting of perl's HTTP::Date - https://metacpan.org/pod/HTTP::Date

Index

Examples

Constants

View Source
const Version = "1.0.0"

Version of httpdate

Variables

This section is empty.

Functions

func Run

func Run(argv []string) int

Run the cli

func Str2Time

func Str2Time(origTimeStr string, loc *time.Location) (time.Time, error)

Str2Time will try to convert a date string, and then return it as a `time.Time`. If the date is unrecognized, error will be returned.

The function is able to parse the following formats:

"Wed, 09 Feb 1994 22:23:32 GMT"       -- HTTP format
"Thu Feb  3 17:03:55 GMT 1994"        -- ctime(3) format
"Thu Feb  3 00:00:00 1994",           -- ANSI C asctime() format
"Tuesday, 08-Feb-94 14:15:29 GMT"     -- old rfc850 HTTP format
"Tuesday, 08-Feb-1994 14:15:29 GMT"   -- broken rfc850 HTTP format

"03/Feb/1994:17:03:55 -0700"   -- common logfile format
"09 Feb 1994 22:23:32 GMT"     -- HTTP format (no weekday)
"08-Feb-94 14:15:29 GMT"       -- rfc850 format (no weekday)
"08-Feb-1994 14:15:29 GMT"     -- broken rfc850 format (no weekday)

"1994-02-03 14:15:29 -0100"    -- ISO 8601 format
"1994-02-03 14:15:29"          -- zone is optional
"1994-02-03"                   -- only date
"1994-02-03T14:15:29"          -- Use T as separator
"19940203T141529Z"             -- ISO 8601 compact format
"19940203"                     -- only date

"08-Feb-94"         -- old rfc850 HTTP format    (no weekday, no time)
"08-Feb-1994"       -- broken rfc850 HTTP format (no weekday, no time)
"09 Feb 1994"       -- proposed new HTTP format  (no weekday, no time)
"03/Feb/1994"       -- common logfile format     (no time, no offset)

"Feb  3  1994"      -- Unix 'ls -l' format
"Feb  3 17:03"      -- Unix 'ls -l' format

"11-15-96  03:52PM" -- Windows 'dir' format

The parser ignores leading and trailing whitespace. It also allow the seconds to be missing and the month to be numerical in most formats.

If the year is missing, then we assume that the date is the first matching date I<before> current month. If the year is given with only 2 digits, then parse_date() will select the century that makes the year closest to the current date.

If no timezones are detected from string, location specified by 2nd argument is used. When the location is also nil, time.Local used. Please note that the returned `time.Time` does not always have the location specified. It is used only when the timezone can not be detected. If you want to fix the location in the returned time, please specify the location by yourself as follows.

t = t.In(time.Local)
Example
package main

import (
	"fmt"
	"time"

	httpdate "github.com/Songmu/go-httpdate"
)

func main() {
	t1, _ := httpdate.Str2Time("Thu, 03 Feb 1994 12:33:44 GMT", time.UTC)
	t2, _ := httpdate.Str2Time("2017-11-11", time.UTC)
	t3, _ := httpdate.Str2Time("Thu Nov  9 18:20:31 GMT 2017", time.UTC)
	t4, _ := httpdate.Str2Time("08-Feb-94 14:15:29 GMT", time.UTC)

	fmt.Println(t1)
	fmt.Println(t2)
	fmt.Println(t3)
	fmt.Println(t4)
}
Output:

1994-02-03 12:33:44 +0000 GMT
2017-11-11 00:00:00 +0000 UTC
2017-11-09 18:20:31 +0000 GMT
1994-02-08 14:15:29 +0000 GMT

func Time2Str

func Time2Str(t time.Time) string

Time2Str converts a `time.Time` to a string. Note. This is only an implementation as HTTP:Datet#time2str's porting and may not be very practical.

The string returned is in the format preferred for the HTTP protocol. This is a fixed length subset of the format defined by RFC 1123, represented in Universal Time (GMT). An example of a time stamp in this format is:

Sun, 06 Nov 1994 08:49:37 GMT

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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