Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Date to PosixDate #2436

Merged
merged 6 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/net/http/common_log.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CommonLog is Logger
list.push(" - ")
list.push(_entry(request.url.user))

let time = Date(Time.seconds()).format("%d/%b/%Y:%H:%M:%S +0000")
let time = PosixDate(Time.seconds()).format("%d/%b/%Y:%H:%M:%S +0000")
list.push(" [")
list.push(time)
list.push("] \"")
Expand Down
19 changes: 19 additions & 0 deletions packages/time/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ actor Main is TestList

fun tag tests(test: PonyTest) =>
test(_TestNanos)
test(_TestPosixDate)

class iso _TestNanos is UnitTest
fun name(): String => "time/Nanos"
Expand All @@ -18,3 +19,21 @@ class iso _TestNanos is UnitTest
h.assert_eq[U64](1_230_000_000, Nanos.from_seconds_f(1.23))
h.assert_eq[U64](1_230_000, Nanos.from_millis_f(1.23))
h.assert_eq[U64](1_230, Nanos.from_micros_f(1.23))

class iso _TestPosixDate is UnitTest
fun name(): String => "time/PosixDate"

fun apply(h: TestHelper) =>
// time is in seconds
h.assert_eq[I64](0, PosixDate(0, 0).time())
h.assert_eq[I64](1, PosixDate(1, 0).time())
// small nanoseconds can not affect time
h.assert_eq[I64](0, PosixDate(0, 900_000_000).time())
// big nanoseconds can affect time
h.assert_eq[I64](1, PosixDate(0, 1_000_000_000).time())
h.assert_eq[I64](2, PosixDate(1, 1_000_000_000).time())
// negaive seconds should be changed to zero
h.assert_eq[I64](0, PosixDate(-1, 0).time())
h.assert_eq[I64](1, PosixDate(-1, 1_000_000_000).time())
// negative nanoseconds cannot not affect time
h.assert_eq[I64](1, PosixDate(1, -1_000_000_000).time())
15 changes: 12 additions & 3 deletions packages/time/date.pony
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Date
class PosixDate
"""
Represents a proleptic Gregorian date and time, without specifying a
time zone. The day of month, month, day of week, and day of year are all
Expand All @@ -16,9 +16,11 @@ class Date

new create(seconds: I64 = 0, nanoseconds: I64 = 0) =>
"""
Create a date from a POSIX time.
Create a date from a POSIX time. Negative arguments will be changed to zero.
"""
@ponyint_gmtime[None](this, seconds, nanoseconds)
@ponyint_gmtime[None](this,
_negative_to_zero(seconds),
_negative_to_zero(nanoseconds))

fun time(): I64 =>
"""
Expand All @@ -43,3 +45,10 @@ class Date
String.from_cstring(@ponyint_formattime[Pointer[U8]](this,
fmt.cstring()))
end

fun _negative_to_zero(value: I64): I64 =>
if value > 0 then
value
else
0
end