Duration

this is a complementary module to be used with Datetime and is concerned with adding and subtracting time values of the scale from seconds to weeks from Datetime values.

Example:

(load "git@github.com:carpentry-org/time.carp@0.5.0")

(defn main []
  (println*
    &(Datetime.strftime
      &(Duration.sub &(Datetime.now) &(Duration.days 2))
      "%Y-%m-%d %I:%M:%S.%n %p %z")))

<

defn

(Fn [(Ref Duration a), (Ref Duration b)] Bool)

                        (< a b)
                    

checks whether the delta a is shorter than the delta b.

=

defn

(Fn [(Ref Duration a), (Ref Duration b)] Bool)

                        (= a b)
                    

checks whether the deltas a and b represent the same duration.

>

defn

(Fn [(Ref Duration a), (Ref Duration b)] Bool)

                        (> a b)
                    

checks whether the delta a is longer than the delta b.

add

defn

(Fn [(Ref Datetime a), (Ref Duration b)] Datetime)

                        (add dt delta)
                    

adds a Duration delta to a Datetime dt.

between

defn

(Fn [(Ref Datetime a), (Ref Datetime b)] Duration)

                        (between a b)
                    

returns the signed difference between the Datetimes a and b as a Duration.

The result is positive when a is later than b, and negative when a is earlier, mirroring Datetime.diff.

copy

instantiate

(Fn [(Ref Duration a)] Duration)

copies a Duration.

days

defn

(Fn [Int] Duration)

                        (days n)
                    

creates a delta representing a number of days n.

delete

instantiate

(Fn [Duration] ())

deletes a Duration. Should usually not be called manually.

hours

defn

(Fn [Int] Duration)

                        (hours n)
                    

creates a delta representing a number of hours n.

minus

defn

(Fn [(Ref Duration a), (Ref Duration b)] Duration)

                        (minus a b)
                    

returns the difference of the deltas a and b.

minutes

defn

(Fn [Int] Duration)

                        (minutes n)
                    

creates a delta representing a number of minutes n.

mul

defn

(Fn [(Ref Duration a), Int] Duration)

                        (mul d n)
                    

returns the delta d scaled by the integer factor n. An alias for scale.

neg?

defn

(Fn [(Ref Duration a)] Bool)

                        (neg? d)
                    

checks whether the delta d represents a negative duration.

negate

defn

(Fn [(Ref Duration a)] Duration)

                        (negate d)
                    

returns the delta d with its sign flipped.

plus

defn

(Fn [(Ref Duration a), (Ref Duration b)] Duration)

                        (plus a b)
                    

returns the sum of the deltas a and b.

pos?

defn

(Fn [(Ref Duration a)] Bool)

                        (pos? d)
                    

checks whether the delta d represents a positive duration.

prn

instantiate

(Fn [(Ref Duration a)] String)

converts a Duration to a string.

scale

defn

(Fn [(Ref Duration a), Int] Duration)

                        (scale d n)
                    

returns the delta d scaled by the integer factor n.

seconds

defn

(Fn [Int] Duration)

                        (seconds n)
                    

creates a delta representing a number of seconds n.

str

defn

(Fn [(Ref Duration a)] String)

                        (str d)
                    

renders the delta d in a human-readable form such as 1d 2h 3m 4s.

Components that are zero are omitted, a negative delta is prefixed with a -, and the zero delta renders as 0s.

sub

defn

(Fn [(Ref Datetime a), (Ref Duration b)] Datetime)

                        (sub dt delta)
                    

subtracts a Duration delta from a Datetime dt.

to-days

defn

(Fn [(Ref Duration a)] Int)

                        (to-days d)
                    

returns the total number of whole days represented by the delta d, truncating any remainder toward zero.

to-hours

defn

(Fn [(Ref Duration a)] Int)

                        (to-hours d)
                    

returns the total number of whole hours represented by the delta d, truncating any remainder toward zero.

to-minutes

defn

(Fn [(Ref Duration a)] Int)

                        (to-minutes d)
                    

returns the total number of whole minutes represented by the delta d, truncating any remaining seconds toward zero.

to-seconds

defn

(Fn [(Ref Duration a)] Int)

                        (to-seconds d)
                    

returns the total number of seconds represented by the delta d.

weeks

defn

(Fn [Int] Duration)

                        (weeks n)
                    

creates a delta representing a number of weeks n.

zero?

defn

(Fn [(Ref Duration a)] Bool)

                        (zero? d)
                    

checks whether the delta d represents no elapsed time.