LCOV - code coverage report
Current view: top level - src - day-delta.c (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 68 68 100.0 %
Date: 2017-04-30 Functions: 29 29 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Present - Date/Time Library
       3             :  *
       4             :  * Implementation of the DayDelta methods
       5             :  *
       6             :  * Licensed under the MIT License.
       7             :  * For details, see LICENSE.
       8             :  */
       9             : 
      10             : #include <assert.h>
      11             : #include <stddef.h>
      12             : 
      13             : #include "present.h"
      14             : 
      15             : #include "utils/constants.h"
      16             : #include "utils/impl-utils.h"
      17             : #include "utils/time-utils.h"
      18             : 
      19             : /** Initialize a new DayDelta based on the number of days. */
      20             : static void
      21          95 : init_day_delta(struct DayDelta * const result, int_delta days)
      22             : {
      23          95 :     assert(result != NULL);
      24          95 :     CLEAR(result);
      25          95 :     result->data_.delta_days = days;
      26          95 : }
      27             : 
      28             : 
      29             : struct DayDelta
      30           9 : DayDelta_from_days(int_delta days)
      31             : {
      32             :     struct DayDelta result;
      33           9 :     init_day_delta(&result, days);
      34           9 :     return result;
      35             : }
      36             : 
      37             : void
      38          54 : DayDelta_ptr_from_days(struct DayDelta * const result, int_delta days)
      39             : {
      40          54 :     init_day_delta(result, days);
      41          54 : }
      42             : 
      43             : struct DayDelta
      44           5 : DayDelta_from_weeks(int_delta weeks)
      45             : {
      46             :     struct DayDelta result;
      47           5 :     init_day_delta(&result, weeks * DAYS_IN_WEEK);
      48           5 :     return result;
      49             : }
      50             : 
      51             : void
      52          16 : DayDelta_ptr_from_weeks(struct DayDelta * const result, int_delta weeks)
      53             : {
      54          16 :     init_day_delta(result, weeks * DAYS_IN_WEEK);
      55          16 : }
      56             : 
      57             : struct DayDelta
      58           1 : DayDelta_zero(void)
      59             : {
      60             :     struct DayDelta result;
      61           1 :     init_day_delta(&result, 0);
      62           1 :     return result;
      63             : }
      64             : 
      65             : void
      66          10 : DayDelta_ptr_zero(struct DayDelta * const result)
      67             : {
      68          10 :     init_day_delta(result, 0);
      69          10 : }
      70             : 
      71             : int_delta
      72          97 : DayDelta_days(const struct DayDelta * const self)
      73             : {
      74          97 :     assert(self != NULL);
      75             : 
      76          97 :     return self->data_.delta_days;
      77             : }
      78             : 
      79             : int_delta
      80           9 : DayDelta_weeks(const struct DayDelta * const self)
      81             : {
      82           9 :     assert(self != NULL);
      83             : 
      84           9 :     return self->data_.delta_days / DAYS_IN_WEEK;
      85             : }
      86             : 
      87             : double
      88           5 : DayDelta_weeks_decimal(const struct DayDelta * const self)
      89             : {
      90           5 :     assert(self != NULL);
      91             : 
      92           5 :     return ((double)self->data_.delta_days) / (double)DAYS_IN_WEEK;
      93             : }
      94             : 
      95             : struct TimeDelta
      96          42 : DayDelta_to_TimeDelta(const struct DayDelta * const self)
      97             : {
      98          42 :     assert(self != NULL);
      99             : 
     100          42 :     return TimeDelta_from_days(self->data_.delta_days);
     101             : }
     102             : 
     103             : present_bool
     104           7 : DayDelta_is_negative(const struct DayDelta * const self)
     105             : {
     106           7 :     assert(self != NULL);
     107             : 
     108           7 :     return self->data_.delta_days < 0;
     109             : }
     110             : 
     111             : void
     112          13 : DayDelta_negate(struct DayDelta * const self)
     113             : {
     114          13 :     assert(self != NULL);
     115             : 
     116          13 :     self->data_.delta_days = -self->data_.delta_days;
     117          13 : }
     118             : 
     119             : void
     120           6 : DayDelta_multiply_by(struct DayDelta * const self, long scale_factor)
     121             : {
     122           6 :     assert(self != NULL);
     123             : 
     124           6 :     self->data_.delta_days *= scale_factor;
     125           6 : }
     126             : 
     127             : void
     128           6 : DayDelta_divide_by(struct DayDelta * const self, long scale_factor)
     129             : {
     130           6 :     assert(self != NULL);
     131             : 
     132           6 :     self->data_.delta_days /= scale_factor;
     133           6 : }
     134             : 
     135             : void
     136           4 : DayDelta_add(
     137             :         struct DayDelta * const self,
     138             :         const struct DayDelta * const other)
     139             : {
     140           4 :     assert(self != NULL);
     141           4 :     assert(other != NULL);
     142             : 
     143           4 :     self->data_.delta_days += other->data_.delta_days;
     144           4 : }
     145             : 
     146             : void
     147           4 : DayDelta_subtract(
     148             :         struct DayDelta * const self,
     149             :         const struct DayDelta * const other)
     150             : {
     151           4 :     assert(self != NULL);
     152           4 :     assert(other != NULL);
     153             : 
     154           4 :     self->data_.delta_days -= other->data_.delta_days;
     155           4 : }
     156             : 
     157             : short
     158          43 : DayDelta_compare(
     159             :         const struct DayDelta * const lhs,
     160             :         const struct DayDelta * const rhs)
     161             : {
     162          43 :     assert(lhs != NULL);
     163          43 :     assert(rhs != NULL);
     164             : 
     165          43 :     return STRUCT_COMPARE(delta_days, 0);
     166             : }
     167             : 
     168             : short
     169          37 : DayDelta_compare_to_TimeDelta(
     170             :         const struct DayDelta * const lhs,
     171             :         const struct TimeDelta * const rhs)
     172             : {
     173          37 :     return -TimeDelta_compare_to_DayDelta(rhs, lhs);
     174             : }
     175             : 
     176          37 : STRUCT_COMPARISON_OPERATORS(DayDelta)
     177             : 
     178          25 : STRUCT_COMPARISON_OPERATORS_WITH_OTHER_STRUCT(DayDelta, TimeDelta)
     179             : 

Generated by: LCOV version 1.10