cMonthDay

Overview

Types and Definitions

cMonthDay

struct cMonthDay
{
   c_Month month;
   int8_t day;
};
typedef struct cMonthDay cMonthDay;

The cMonthDay struct represents a day in a specific year that is not part of the struct.

Generated

cMonthDaySlice

struct cMonthDaySlice
{
   int64_t s;
   cMonthDay const* v;
};
typedef struct cMonthDaySlice cMonthDaySlice;

Via the macro SLICES_C_ generated struct.

cVarMonthDaySlice

struct cVarMonthDaySlice
{
   int64_t s;
   cMonthDay* v;
};
typedef struct cVarMonthDaySlice cVarMonthDaySlice;

Via the macro SLICES_C_ generated struct.

Functions

overall

cmp_month_day_c

int cmp_month_day_c( cMonthDay a, cMonthDay b );

Compares two cMonthDay values and returns the three possible results:

<0

if a is earlier as b

0

if both dates are equal

>0

if a is later as b

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

int main( void )
{
   init_tap_c_();

   cMonthDay a = month_day_c( c_Aug, 6 );
   cMonthDay b = a;
   expect_c_( eq_c( cmp_month_day_c( a, b ) ) );

   b.day = 7;
   expect_c_( diff_c( cmp_month_day_c( a, b ) ) );

   return finish_tap_c_();
}

month_day_c

cMonthDay month_day_c( int64_t month, int64_t day );

Initialise a cMonthDay value with a month and day value.

io

The functions read_month_day_c and write_month_day_c are using the following format:

Table 1. format

D

The day as a number without a leading character

1 to 31

_D

The day as a number with a leading space

1 to 31

DD

The day as a number with a leading zero

01 to 31

M

The month as a number without a leading character

1 to 12

_M

The month as a number with a leading space

1 to 12

MM

The month as a number with a leading zero

01 to 12

MMM

The abbreviated localized month name

Jan' to Dec

read_month_day_c

#define read_month_day_c_( Sca, MonthDay )                                     \
   read_month_day_c( (Sca), (MonthDay), "" )
bool read_month_day_c( cScanner sca[static 1],
                       cMonthDay md[static 1],
                       char const fmt[static 1] );

Reads a cMonthDay value from a text with a scanner. The function will use "MMM._D" as default format.

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

#include <locale.h>

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      char const* fmt;
      cMonthDay exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "12 01", "MM DD", month_day_c( 12, 1 ) )
   );

   for_each_c_( test const*, t, tests )
   {
      cScanner* sca = &cstr_scanner_c_( t->inp );

      cMonthDay md;
      bool res = read_month_day_c( sca, &md, t->fmt );
      res &= eq_c( cmp_month_day_c( md, t->exp ) );

      tap_descf_c( res, "res: %s, with %s", t->inp, t->fmt );
   }

   return finish_tap_c_();
}

write_month_day_c

#define write_month_day_c_( Rec, MonthDay )                                    \
   write_month_day_c( (Rec), (MonthDay), "" )
bool write_month_day_c( cRecorder rec[static 1],
                        cMonthDay md,
                        char const fmt[static 1] );

Writes a cMonthDay value into the recorder. The function will use "MMM._D" as default format.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/locale.h"
#include "clingo/time/cMonthDay.h"

TEMP_SLICE_C_(
   test,
   {
      cMonthDay md;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   set_locale_c( LC_TIME, "C" ); // For every system the same output

   cMonthDay md = month_day_c( 12, 1 );
   testSlice tests = slice_c_( test,
      t_( md, "MM DD", "12 01" ),
      t_( md, "MMM D", "Dec 1" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_month_day_c( rec, t->md, t->fmt );
      res &= recorded_is_c( rec, t->exp );

      tap_descf_c( res, "test: %s", t->exp );
   }

   return finish_tap_c_();
}