From 914efc63626c414b9cb19ea1f5b5ca4c0836535e Mon Sep 17 00:00:00 2001 From: Roman Bataev Date: Thu, 8 Mar 2018 21:32:05 -0800 Subject: [PATCH] joker.time/format --- std/time.joke | 10 ++++++++++ std/time/a_time.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/std/time.joke b/std/time.joke index 39a7a5865..43fd9a8bd 100644 --- a/std/time.joke +++ b/std/time.joke @@ -61,6 +61,16 @@ :go "int(time.Until(t))"} [^Time t]) +(defn ^String format + "Returns a textual representation of the time value formatted according to layout, + which defines the format by showing how the reference time, defined to be + Mon Jan 2 15:04:05 -0700 MST 2006 + would be displayed if it were the value; it serves as an example of the desired output. + The same display rules will then be applied to the time value.." + {:added "1.0" + :go "t.Format(layout)"} + [^Time t ^String layout]) + (defn ^Double hours "Returns the duration (passed as a number of nanoseconds) as a floating point number of hours." {:added "1.0" diff --git a/std/time/a_time.go b/std/time/a_time.go index 9819b4ec3..4c7b33a33 100644 --- a/std/time/a_time.go +++ b/std/time/a_time.go @@ -25,6 +25,22 @@ var add_ Proc = func(args []Object) Object { return NIL } +var format_ Proc = func(args []Object) Object { + c := len(args) + switch { + case c == 2: + + t := ExtractTime(args, 0) + layout := ExtractString(args, 1) + res := t.Format(layout) + return MakeString(res) + + default: + PanicArity(c) + } + return NIL +} + var from_unix_ Proc = func(args []Object) Object { c := len(args) switch { @@ -249,6 +265,15 @@ timeNamespace.InternVar("add", add_, NewListFrom(NewVectorFrom(MakeSymbol("t"), MakeSymbol("d"))), `Returns the time t+d.`, "1.0")) +timeNamespace.InternVar("format", format_, + MakeMeta( + NewListFrom(NewVectorFrom(MakeSymbol("t"), MakeSymbol("layout"))), + `Returns a textual representation of the time value formatted according to layout, + which defines the format by showing how the reference time, defined to be + Mon Jan 2 15:04:05 -0700 MST 2006 + would be displayed if it were the value; it serves as an example of the desired output. + The same display rules will then be applied to the time value..`, "1.0")) + timeNamespace.InternVar("from-unix", from_unix_, MakeMeta( NewListFrom(NewVectorFrom(MakeSymbol("sec"), MakeSymbol("nsec"))),