cDatePeriod

Overview

Types and Definitions

cDatePeriod

struct cDatePeriod
{
   cDate first;
   cDate last;
};
typedef struct cDatePeriod cDatePeriod;

Struct that represents a date period.

Generated

cDatePeriodSlice

struct cDatePeriodSlice
{
   int64_t s;
   cDatePeriod const* v;
};
typedef struct cDatePeriodSlice cDatePeriodSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cDatePeriodSlice date_period_slice_c( int64_t s, cDatePeriod const* v );
cDatePeriodSlice make_date_period_slice_c( cDatePeriod const* beg,
                                           cDatePeriod const* end );
cDatePeriodSlice empty_date_period_slice_c( void );
/* sub */
cDatePeriodSlice left_date_period_slice_c( cDatePeriodSlice slice,
                                           int64_t maxLen );
cDatePeriodSlice mid_date_period_slice_c( cDatePeriodSlice slice,
                                          int64_t index );
cDatePeriodSlice right_date_period_slice_c( cDatePeriodSlice slice,
                                            int64_t maxLen );
cDatePeriodSlice sub_date_period_slice_c( cDatePeriodSlice slice,
                                          int64_t begIdx,
                                          int64_t endIdx);

cVarDatePeriodSlice

struct cVarDatePeriodSlice
{
   int64_t s;
   cDatePeriod* v;
};
typedef struct cVarDatePeriodSlice cVarDatePeriodSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cVarDatePeriodSlice var_date_period_slice_c( int64_t s, cDatePeriod* v );
cVarDatePeriodSlice make_var_date_period_slice_c( cDatePeriod* beg,
                                                  cDatePeriod* end );
cVarDatePeriodSlice empty_var_date_period_slice_c( void );
/* sub */
cVarDatePeriodSlice left_var_date_period_slice_c( cVarDatePeriodSlice slice,
                                                  int64_t maxLen );
cVarDatePeriodSlice mid_var_date_period_slice_c( cVarDatePeriodSlice slice,
                                                 int64_t index );
cVarDatePeriodSlice right_var_date_period_slice_c( cVarDateSlice slice,
                                                   int64_t maxLen );
cVarDatePeriodSlice sub_var_date_period_slice_c( cVarDatePeriodSlice slice,
                                                 int64_t begIdx,
                                                 int64_t endIdx);
/* var slice */
cVarDatePeriodSlice as_var_date_slice_c( cVarDatePeriodSlice slice );
cVarDatePeriodSlice cast_var_date_slice_c( cVarDatePeriodSlice slice,
                                           cDatePeriodSlice sub );
int64_t set_date_slice_c( cVarDatePeriodSlice dst, cDatePeriodSlice src );

Functions

init

date_period_c

cDatePeriod date_period_c( cDate first, cDate last );

Creates a date period.

determine_date_period_c

cDatePeriod determine_date_period_c( cDateSlice slice );

Creates a date period with the earliest date in the slice as first and the latest date in the slice as last.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      cDateSlice inp;
      cDatePeriod exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( empty_date_slice_c(), null_date_period_c() ),
      t_(
         slice_c_( cDate, date_c( 2020, 11, 30 ) ),
         single_date_period_c( date_c( 2020, 11, 30 ) )
      ),
      t_(
         slice_c_( cDate, date_c( 2020, 11, 30 ),
                          date_c( 2020, 10, 26 ),
                          date_c( 2019, 12, 13 ) ),
         make_date_period_c( 2019, 12, 13, 2020, 11, 30 )
      )
   );

   for_each_c_( test const*, t, tests )
   {
      cDatePeriod period = determine_date_period_c( t->inp );

      bool res = eq_date_period_c( period, t->exp );

      cRecorder* rec = &recorder_c_( 32 );
      {
         write_date_c_( rec, period.first );
         record_chars_c_( rec, " -> " );
         write_date_c_( rec, period.last );
      }
      tap_descf_c( res, turn_into_cstr_c( rec ) );
   }

   return finish_tap_c_();
}

cDatePeriod make_date_period_c

cDatePeriod make_date_period_c( int64_t y1, int64_t m1, int64_t d1,
                                int64_t y2, int64_t m2, int64_t d2 );

Creates a date period and the required dates.

null_date_period_c

cDatePeriod null_date_period_c();

Creates an invalid date period.

single_date_period_c

cDatePeriod single_date_period_c( cDate date );

Creates a date period that contains only one day.

sized_date_period_c

cDatePeriod sized_date_period_c( cDate first, int64_t days );

Creates a date period that contains the following days, starting at first.

length

date_period_days_c

int64_t date_period_days_c( cDatePeriod dp );

Returns the length of the date period dp in number of days.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod p1 = sized_date_period_c( date_c( 2013, 2, 17 ), 10 );
   expect_c_( date_period_days_c( p1 ) == 10 );

   cDatePeriod p2 = single_date_period_c( date_c( 2013, 2, 17 ) );
   expect_c_( date_period_days_c( p2 ) == 1 );

   cDatePeriod p3 = date_period_c( date_c( 2015, 12, 24 ),
                                   date_c( 2015, 12, 26 ) );
   expect_c_( date_period_days_c( p3 ) == 3 );

   return finish_tap_c_();
}

date_period_weeks_c

int64_t date_period_weeks_c( cDatePeriod dp );

Returns the length of the date period dp in number of complete weeks.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod p1 = date_period_c( date_c( 2014, 2, 27 ),
                                   date_c( 2014, 3, 25 ) );
   expect_c_( date_period_weeks_c( p1 ) == 3 );

   cDatePeriod p2 = date_period_c( date_c( 2014, 2, 27 ),
                                   date_c( 2014, 3, 27 ) );
   expect_c_( date_period_weeks_c( p2 ) == 4 );

   cDatePeriod p3 = date_period_c( date_c( 2014, 2, 27 ),
                                   date_c( 2014, 3, 29 ) );
   expect_c_( date_period_weeks_c( p3 ) == 4 );

   cDatePeriod p4 = single_date_period_c( date_c( 2014, 3, 2 ) );
   expect_c_( date_period_weeks_c( p4 ) == 0 );

   return finish_tap_c_();
}

overall

date_period_is_valid_c

bool date_period_is_valid_c( cDatePeriod dp );

Returns true if the date period is valid, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod period = date_period_c( date_c( 2014, 3, 3 ),
                                       date_c( 2014, 3, 9 ) );
   expect_c_( date_period_is_valid_c( period ) );

   cDatePeriod invalid = date_period_c( null_date_c(),
                                        date_c( 2014, 3, 6 ) );
   expect_c_( not date_period_is_valid_c( invalid ) );

   return finish_tap_c_();
}

eq_date_period_c

bool eq_date_period_c( cDatePeriod a, cDatePeriod b );

Returns true if both date periods are equal, otherwise false.

in_date_period_c

bool in_date_period_c( cDatePeriod dp, cDate date );

Returns true if the date period contains date, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod period = date_period_c( date_c( 2014, 3, 3 ),
                                       date_c( 2014, 3, 9 ) );

   expect_c_( !in_date_period_c( period, null_date_c() ) );
   expect_c_( !in_date_period_c( period, date_c( 2014, 3,  2 ) ) );
   expect_c_(  in_date_period_c( period, date_c( 2014, 3,  3 ) ) );
   expect_c_(  in_date_period_c( period, date_c( 2014, 3,  5 ) ) );
   expect_c_(  in_date_period_c( period, date_c( 2014, 3,  9 ) ) );
   expect_c_( !in_date_period_c( period, date_c( 2014, 3, 10 ) ) );

   return finish_tap_c_();
}

resize_date_period_c

cDatePeriod resize_date_period_c( cDatePeriod dp, int64_t days );

Increases(days is positve) or decreases(days is negatvie) a date period on both sizes.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod period = make_date_period_c( 2014, 11, 9, 2014, 11, 16 );

   cDatePeriod bigger = make_date_period_c( 2014, 11, 2, 2014, 11, 23 );
   expect_c_( eq_date_period_c( resize_date_period_c( period, 7 ), bigger ) );

   cDatePeriod smaller = make_date_period_c( 2014, 11, 10, 2014, 11, 15 );
   expect_c_( eq_date_period_c( resize_date_period_c( period, -1 ), smaller ) );

   return finish_tap_c_();
}

shift_date_period_c

cDatePeriod shift_date_period_c( cDatePeriod dp, int64_t days );

Shifts the date period forward(postive number of days) or backwards(negative number of days).

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod prev = sized_date_period_c( date_c( 2014, 2, 24 ), 7 );
   cDatePeriod week = sized_date_period_c( date_c( 2014, 3,  3 ), 7 );
   cDatePeriod next = sized_date_period_c( date_c( 2014, 3, 10 ), 7 );

   expect_c_( eq_date_period_c( shift_date_period_c( week,  7 ), next ) );
   expect_c_( eq_date_period_c( shift_date_period_c( week, -7 ), prev ) );

   return finish_tap_c_();
}

set

date_periods_overlap_c

bool date_periods_overlap_c( cDatePeriod a, cDatePeriod b );

Returns true if both date periods share days.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod p1 = date_period_c( date_c( 2014, 3, 3 ),
                                   date_c( 2014, 3, 9 ) );
   cDatePeriod p2 = date_period_c( date_c( 2014, 3, 7 ),
                                   date_c( 2014, 3, 13 ) );
   cDatePeriod p3 = date_period_c( date_c( 2014, 3, 12 ),
                                   date_c( 2014, 3, 20 ) );

   expect_c_(  date_periods_overlap_c( p1, p2 ) );
   expect_c_(  date_periods_overlap_c( p2, p3 ) );
   expect_c_( !date_periods_overlap_c( p1, p3 ) );

   return finish_tap_c_();
}

intersect_date_periods_c

cDatePeriod intersect_date_periods_c( cDatePeriod a, cDatePeriod b );

Returns the days that are in both date periods.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod p1 = date_period_c( date_c( 2014, 3, 3 ),
                                   date_c( 2014, 3, 9 ) );
   cDatePeriod p2 = date_period_c( date_c( 2014, 3, 7 ),
                                   date_c( 2014, 3, 13 ) );

   cDatePeriod ip = intersect_date_periods_c( p1, p2 );
   expect_c_( eq_date_period_c( ip, date_period_c( date_c( 2014, 3, 7 ),
                                                   date_c( 2014, 3, 9 ) ) ) );

   cDatePeriod p3 = single_date_period_c( date_c( 2014, 3, 13 ) );
   cDatePeriod np = intersect_date_periods_c( p1, p3 );
   expect_c_( !date_period_is_valid_c( np ) );

   return finish_tap_c_();
}

is_sub_date_period_c

bool is_sub_date_period_c( cDatePeriod dp, cDatePeriod sub );

Returns true if all days from the date period sub also exist in the date period dp.

unite_date_periods_c

cDatePeriod unite_date_periods_c( cDatePeriod a, cDatePeriod b )

Returns a date period that contains at least all days from a and b.

Example
#include "clingo/lang/expect.h"
#include "clingo/time/cDatePeriod.h"

int main( void )
{
   init_tap_c_();

   cDatePeriod p1 = single_date_period_c( date_c( 2014, 3, 1 ) );
   cDatePeriod p2 = single_date_period_c( date_c( 2014, 3, 10 ) );

   cDatePeriod exp = make_date_period_c( 2014, 3, 1, 2014, 3, 10 );
   expect_c_( eq_date_period_c( unite_date_periods_c( p1, p2 ), exp ) );

   return finish_tap_c_();
}