Skip to content

Commit

Permalink
Refactoring and now method in Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
slayful committed Jan 5, 2018
1 parent 702532b commit 575e48e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
10 changes: 10 additions & 0 deletions sagittarius/clock.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use "time"

interface Clock
fun now(): (I64 /*sec*/, U32 /*nsec*/)

class SystemClock is Clock

fun now(): (I64 /*sec*/, U32 /*nsec*/) =>
let n = Time.now()
(n._1, n._2.u32())
18 changes: 9 additions & 9 deletions sagittarius/duration.pony
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class val Duration is (Equatable[Duration] & Stringable)

let _seconds: I32 val
let _seconds: I64 val
let _nanos: U32 val

new val create(seconds: I32 val = 0, nanos: U32 val = 0) =>
new val create(seconds: I64 val = 0, nanos: U32 val = 0) =>
_seconds = seconds
_nanos = nanos

new val from_millis(millis: I64 val) =>
let nanos_adjustment = millis % MillisPerSecond().i64()
if nanos_adjustment < 0 then
_seconds = (millis / MillisPerSecond().i64()).i32() - 1
_seconds = (millis / MillisPerSecond().i64()) - 1
_nanos = (nanos_adjustment + MillisPerSecond().i64()).u32() * NanosPerMilli().u32()
else
_seconds = (millis / MillisPerSecond().i64()).i32()
_seconds = millis / MillisPerSecond().i64()
_nanos = nanos_adjustment.u32() * NanosPerMilli().u32()
end

new val _create(seconds: I32 val, nanos: I64 val) =>
new val _create(seconds: I64 val, nanos: I64 val) =>
if nanos >= 0 then
_seconds = seconds
_nanos = (nanos % NanosPerSecond().i64()).u32()
Expand All @@ -26,7 +26,7 @@ class val Duration is (Equatable[Duration] & Stringable)
_nanos = (NanosPerSecond().i64() + (nanos % NanosPerSecond().i64())).u32()
end

fun get_seconds(): I32 val =>
fun get_seconds(): I64 val =>
_seconds

fun get_nanos(): U32 val =>
Expand All @@ -49,15 +49,15 @@ class val Duration is (Equatable[Duration] & Stringable)
fun val sub(that: Duration val): Duration val =>
sub_seconds_and_nanos(that.get_seconds(), that.get_nanos().i64())

fun val sub_seconds_and_nanos(seconds: I32, nanos: I64): Duration val =>
fun val sub_seconds_and_nanos(seconds: I64, nanos: I64): Duration val =>
add_seconds_and_nanos(-1 * seconds, -1 * nanos)

fun val add_seconds_and_nanos(seconds: I32, nanos: I64): Duration val =>
fun val add_seconds_and_nanos(seconds: I64, nanos: I64): Duration val =>
if (seconds != 0) or (nanos != 0) then
let nanos_sum = get_nanos().i64() + nanos
let seconds_in_nanos = nanos_sum / NanosPerSecond().i64()
let seconds_sum = get_seconds().i64() + seconds.i64() + seconds_in_nanos
Duration._create(seconds_sum.i32(), nanos_sum)
Duration._create(seconds_sum, nanos_sum)
else
this
end
Expand Down
12 changes: 8 additions & 4 deletions sagittarius/instant.pony
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class val Instant is (Equatable[Instant] & Stringable)
/** Duration since 1970-01-01:00:00:00 */
let _duration : Duration val

new val create(seconds: I32 val, nanos: U32 val) =>
new val create(seconds: I64 val, nanos: U32 val) =>
_duration = Duration(seconds, nanos)

new val from_millis(millis: I64 val) =>
Expand All @@ -12,6 +12,10 @@ class val Instant is (Equatable[Instant] & Stringable)
new val from_duration(data: Duration val) =>
_duration = data

new val now(clock: Clock val = SystemClock) =>
let n = clock.now()
_duration = Duration(n._1, n._2)

fun string(): String iso^ =>
String.join([
"Instance of "
Expand All @@ -21,7 +25,7 @@ class val Instant is (Equatable[Instant] & Stringable)
" nanoseconds since 1970-01-01T00:00:00Z."
].values())

fun get_seconds(): I32 val =>
fun get_seconds(): I64 val =>
_duration.get_seconds()

fun get_nanos(): U32 val =>
Expand All @@ -30,7 +34,7 @@ class val Instant is (Equatable[Instant] & Stringable)
fun to_millis(): I64 val =>
_duration.to_millis()

fun val add_seconds_and_nanos(seconds: I32 val, nanos: I64 val): Instant val =>
fun val add_seconds_and_nanos(seconds: I64 val, nanos: I64 val): Instant val =>
if (seconds != 0) or (nanos != 0) then
Instant.from_duration(_duration.add_seconds_and_nanos(seconds, nanos))
else
Expand All @@ -43,7 +47,7 @@ class val Instant is (Equatable[Instant] & Stringable)
fun val sub(duration: Duration val): Instant val =>
add_seconds_and_nanos(-duration.get_seconds(), -duration.get_nanos().i64())

fun val sub_seconds_and_nanos(seconds: I32, nanos: I64): Instant val =>
fun val sub_seconds_and_nanos(seconds: I64, nanos: I64): Instant val =>
add_seconds_and_nanos(-seconds, -nanos)

fun box eq(that: Instant box): Bool val =>
Expand Down
21 changes: 21 additions & 0 deletions sagittarius/time_constants.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
primitive MillisPerSecond fun apply(): U32 => 1_000
primitive MillisPerMinute fun apply(): U32 => SecondsPerMinute().u32() * MillisPerSecond()
primitive MillisPerHour fun apply(): U32 => MillisPerMinute() * MinutesPerHour().u32()
primitive NanosPerSecond fun apply(): U32 => 1_000_000_000
primitive NanosPerMilli fun apply(): U32 => 1_000_000
primitive NanosPerMinute fun apply(): U64 => NanosPerSecond().u64() * SecondsPerMinute().u64()
primitive NanosPerHour fun apply(): U64 => NanosPerSecond().u64() * SecondsPerHour().u64()
primitive SecondsPerMinute fun apply(): U16 => 60
primitive SecondsPerHour fun apply(): U16 => SecondsPerMinute() * MinutesPerHour()
primitive MinutesPerHour fun apply(): U16 => 60
primitive HoursPerDay fun apply(): U16 => 24
primitive MinutesPerDay fun apply(): U16 => MinutesPerHour() * HoursPerDay()
primitive SecondsPerDay fun apply(): U32 => SecondsPerMinute().u32() * MinutesPerDay().u32()

/**
* The number of days from year zero to year 1970.
* There are five 400 year cycles from year zero to 2000.
* There are 7 leap years from 1970 to 2000.
*/
primitive DaysSinceEpoch fun apply(): U32 => (DaysPer400YearCycle() * 5) - ((30 * 365) + 7)
primitive DaysPer400YearCycle fun apply(): U32 => 146097
29 changes: 0 additions & 29 deletions sagittarius/time_utilities.pony

This file was deleted.

1 comment on commit 575e48e

@d-led
Copy link

@d-led d-led commented on 575e48e Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as Pony currently lacks constructing dates from parts, this library is cool. Thanks! Planning to use it vendored here for now

Please sign in to comment.