improve dir list for ghttp; add ISO8601/RFC822 functions for gtime

This commit is contained in:
John
2019-07-25 23:25:30 +08:00
parent b714f7db69
commit 65e06b12ae
6 changed files with 70 additions and 18 deletions

View File

@ -7,15 +7,16 @@
package ghttp
import (
"github.com/gogf/gf/g/encoding/ghtml"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gspath"
"github.com/gogf/gf/g/os/gtime"
"net/http"
"os"
"reflect"
"sort"
"strings"
"github.com/gogf/gf/g/encoding/ghtml"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gspath"
"github.com/gogf/gf/g/os/gtime"
)
// 默认HTTP Server处理入口http包底层默认使用了gorutine异步处理请求所以这里不再异步执行
@ -259,13 +260,13 @@ func (s *Server) listDir(r *Request, f http.File) {
r.Response.Write(`<html>`)
r.Response.Write(`<head></head>`)
r.Response.Write(`<body>`)
r.Response.Writef(`<h1>Index of %s</h1>`, r.URL.RequestURI())
r.Response.Writef(`<h1>Index of %s</h1>`, r.URL.Path)
r.Response.Writef(`<hr />`)
r.Response.Write(`<table>`)
if r.URL.Path != "/" {
r.Response.Write("<tr>")
r.Response.Write("<td><a href=\"..\">..</a></td>")
r.Response.Write("</tr>")
r.Response.Write(`<tr>`)
r.Response.Writef(`<td><a href="%s">..</a></td>`, gfile.Dir(r.URL.Path))
r.Response.Write(`</tr>`)
}
name := ""
size := ""
@ -277,9 +278,9 @@ func (s *Server) listDir(r *Request, f http.File) {
size = "-"
}
r.Response.Write(`<tr>`)
r.Response.Writef(`<td><a href="%s">%s</a></td>`, name, ghtml.SpecialChars(name))
r.Response.Writef(`<td><a href="%s/%s">%s</a></td>`, r.URL.Path, name, ghtml.SpecialChars(name))
r.Response.Writef(`<td style="width:80px;text-align:center;">%s</td>`, size)
r.Response.Writef(`<td>%s</td>`, gtime.New(file.ModTime()).String())
r.Response.Writef(`<td>%s</td>`, gtime.New(file.ModTime()).ISO8601())
r.Response.Write(`</tr>`)
}
r.Response.Write(`</table>`)

View File

@ -9,11 +9,12 @@ package gtime
import (
"errors"
"github.com/gogf/gf/g/text/gregex"
"regexp"
"strconv"
"strings"
"time"
"github.com/gogf/gf/g/text/gregex"
)
const (
@ -120,6 +121,16 @@ func Datetime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// 获得当前时间ISO8601格式
func ISO8601() string {
return time.Now().Format("2006-01-02T15:04:05-07:00")
}
// 获得当前时间RFC822格式
func RFC822() string {
return time.Now().Format("Mon, 02 Jan 06 15:04 MST")
}
// 解析日期字符串(日期支持'-'或'/'或'.'连接符号)
func parseDateStr(s string) (year, month, day int) {
array := strings.Split(s, "-")

View File

@ -149,6 +149,16 @@ func (t *Time) UTC() *Time {
return t
}
// 时间输出为ISO8601格式
func (t *Time) ISO8601() string {
return t.Layout("2006-01-02T15:04:05-07:00")
}
// 时间输出为RFC822格式
func (t *Time) RFC822() string {
return t.Layout("Mon, 02 Jan 06 15:04 MST")
}
// 时区转换为当前设定的Local时区
func (t *Time) Local() *Time {
t.Time = t.Time.Local()

View File

@ -64,6 +64,20 @@ func Test_Datetime(t *testing.T) {
})
}
func Test_ISO8601(t *testing.T) {
gtest.Case(t, func() {
iso8601 := gtime.ISO8601()
gtest.Assert(iso8601, time.Now().Format("c"))
})
}
func Test_RFC822(t *testing.T) {
gtest.Case(t, func() {
rfc822 := gtime.RFC822()
gtest.Assert(rfc822, time.Now().Format("r"))
})
}
func Test_StrToTime(t *testing.T) {
gtest.Case(t, func() {
//正常日期列表

View File

@ -64,41 +64,55 @@ func Test_NewFromTimeStamp(t *testing.T) {
})
}
func Test_tSecond(t *testing.T) {
func Test_Time_Second(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()
gtest.Assert(timeTemp.Second(), timeTemp.Time.Unix())
})
}
func Test_tNanosecond(t *testing.T) {
func Test_Time_Nanosecond(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()
gtest.Assert(timeTemp.Nanosecond(), timeTemp.Time.UnixNano())
})
}
func Test_tMicrosecond(t *testing.T) {
func Test_Time_Microsecond(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()
gtest.Assert(timeTemp.Microsecond(), timeTemp.Time.UnixNano()/1e3)
})
}
func Test_tMillisecond(t *testing.T) {
func Test_Time_Millisecond(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()
gtest.Assert(timeTemp.Millisecond(), timeTemp.Time.UnixNano()/1e6)
})
}
func Test_String(t *testing.T) {
func Test_Time_String(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()
gtest.Assert(timeTemp.String(), timeTemp.Time.Format("2006-01-02 15:04:05"))
})
}
func Test_Time_ISO8601(t *testing.T) {
gtest.Case(t, func() {
now := gtime.Now()
gtest.Assert(now.ISO8601(), now.Time.Format("c"))
})
}
func Test_Time_RFC822(t *testing.T) {
gtest.Case(t, func() {
now := gtime.Now()
gtest.Assert(now.RFC822(), now.Time.Format("r"))
})
}
func Test_Clone(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()

View File

@ -1,9 +1,11 @@
package main
import (
"github.com/gogf/gf/g/os/gproc"
"fmt"
"github.com/gogf/gf/g/os/gtime"
)
func main() {
gproc.ShellRun("sleep 5; echo 1")
fmt.Println(gtime.Now().ISO8601())
}