diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index d30cb342b..dbb9dbe6a 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -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(``) r.Response.Write(``) r.Response.Write(``) - r.Response.Writef(`

Index of %s

`, r.URL.RequestURI()) + r.Response.Writef(`

Index of %s

`, r.URL.Path) r.Response.Writef(`
`) r.Response.Write(``) if r.URL.Path != "/" { - r.Response.Write("") - r.Response.Write("") - r.Response.Write("") + r.Response.Write(``) + r.Response.Writef(``, gfile.Dir(r.URL.Path)) + r.Response.Write(``) } name := "" size := "" @@ -277,9 +278,9 @@ func (s *Server) listDir(r *Request, f http.File) { size = "-" } r.Response.Write(``) - r.Response.Writef(``, name, ghtml.SpecialChars(name)) + r.Response.Writef(``, r.URL.Path, name, ghtml.SpecialChars(name)) r.Response.Writef(``, size) - r.Response.Writef(``, gtime.New(file.ModTime()).String()) + r.Response.Writef(``, gtime.New(file.ModTime()).ISO8601()) r.Response.Write(``) } r.Response.Write(`
..
..
%s%s%s%s%s
`) diff --git a/g/os/gtime/gtime.go b/g/os/gtime/gtime.go index 0773a72f5..f27c82b1b 100644 --- a/g/os/gtime/gtime.go +++ b/g/os/gtime/gtime.go @@ -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, "-") diff --git a/g/os/gtime/gtime_time.go b/g/os/gtime/gtime_time.go index 1d7efee29..03912a96e 100644 --- a/g/os/gtime/gtime_time.go +++ b/g/os/gtime/gtime_time.go @@ -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() diff --git a/g/os/gtime/gtime_z_unit_basic_test.go b/g/os/gtime/gtime_z_unit_basic_test.go index c3159b560..236797672 100644 --- a/g/os/gtime/gtime_z_unit_basic_test.go +++ b/g/os/gtime/gtime_z_unit_basic_test.go @@ -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() { //正常日期列表 diff --git a/g/os/gtime/gtime_z_unit_time_test.go b/g/os/gtime/gtime_z_unit_time_test.go index f00b2d760..71af39768 100644 --- a/g/os/gtime/gtime_z_unit_time_test.go +++ b/g/os/gtime/gtime_z_unit_time_test.go @@ -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() diff --git a/geg/other/test.go b/geg/other/test.go index 23dda6ae3..94e255909 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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()) }