cYmd

Overview

Types and Definitions

cYmd

struct cYmd
{
   int32_t year;
   c_Month month;
   int8_t day;
};
typedef struct cYmd cYmd;

A cYmd struct represents a date via a year, month and day value.

Generated

cYmdSlice

struct cYmdSlice
{
   int64_t s;
   cYmd const* v;
};
typedef struct cYmdSlice cYmdSlice;

Via the macro SLICES_C_ generated struct.

cVarYmdSlice

struct cVarYmdSlice
{
   int64_t s;
   cYmd* v;
};
typedef struct cVarYmdSlice cVarYmdSlice;

Via the macro SLICES_C_ generated struct.

Functions

overall

cmp_ymd_c

int cmp_ymd_c( cYmd a, cYmd b );

Compares two cYmd 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/cYmd.h"

int main( void )
{
   init_tap_c_();

   cYmd ymd = ymd_c( 2013, c_Apr,  1 );
   cYmd bef = ymd_c( 2013, c_Feb, 18 );
   cYmd aft = ymd_c( 2013, c_Apr,  2 );

   expect_eq_c_( cmp_ymd_c( ymd, ymd ) );
   expect_gt_c_( cmp_ymd_c( ymd, bef ) );
   expect_lt_c_( cmp_ymd_c( ymd, aft ) );

   return finish_tap_c_();
}

ymd_c

cYmd ymd_c( int64_t year, int64_t month, int64_t day );

Initialise a ymd with year, month and day. If the specified date is invalid, the date is not set and ymd_is_valid_c returns false.

ymd_is_valid_c

bool ymd_is_valid_c( cYmd ymd );

Returns true if the specified cYmd value is valid, otherwise false.

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

int main( void )
{
   init_tap_c_();

   expect_c_(  ymd_is_valid_c( ymd_c( 2018, c_Jul,  8 ) ) );
   expect_c_( !ymd_is_valid_c( ymd_c( 2018, c_Dec+1,  8 ) ) );
   expect_c_( !ymd_is_valid_c( ymd_c( 1983, c_Dec, 64 ) ) );

   return finish_tap_c_();
}

io

The functions read_ymd_c and write_ymd_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

MMMM

The localized month name

January to December

YY

The year as a two digit number

00 to 99

YYYY

The year as a four digit number

like 1878

read_ymd_c

#define read_ymd_c_( Sca, Ymd )                                                \
   read_ymd_c( (Sca), (Ymd), "" )
bool read_ymd_c( cScanner sca[static 1],
                 cYmd ymd[static 1],
                 char const fmt[static 1] );

Reads a cYmd value from a text with a scanner. The function will use C_IsoDate( "YYYY-MM-DD" ) as default format.

Example
#include "clingo/io/read.h"
#include "clingo/lang/expect.h"
#include "clingo/time/cYmd.h"

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

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "20130401", "YYYYMMDD", ymd_c( 2013, 4, 1 ) ),
      t_( "5-7-2001", "D-M-YYYY", ymd_c( 2001, 7, 5 ) )
   );

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

      cYmd ymd = ymd_c( 1970, 1, 1 );
      bool res = read_ymd_c( sca, &ymd, t->fmt );
      res &= eq_c( cmp_ymd_c( ymd, t->exp ) );

      cRecorder* rec = &recorder_c_( 32 );
      write_ymd_c_( rec, ymd );
      tap_descf_c( res, "%s -> %s", t->fmt, turn_into_cstr_c( rec ) );
   }

   return finish_tap_c_();
}

write_ymd_c

#define write_ymd_c_( Rec, Ymd )                                               \
   write_ymd_c( (Rec), (Ymd), "" )
bool write_ymd_c( cRecorder rec[static 1],
                  cYmd ymd,
                  char const fmt[static 1] );

Writes a cYmd value into the recorder. The function will use C_IsoDate( "YYYY-MM-DD" ) as default format.

Example
#include "clingo/io/write.h"
#include "clingo/lang/expect.h"
#include "clingo/lang/locale.h"
#include "clingo/time/cYmd.h"

TEMP_SLICE_C_(
   test,
   {
      cYmd ymd;
      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

   cYmd ymd = ymd_c( 2013, c_Apr, 1 );
   cYmd yxd = { 2022, c_Dec+1, 7 };
   testSlice tests = slice_c_( test,
      t_( ymd, "YYYYMMDD", "20130401" ),
      t_( ymd, "YY-M-D", "13-4-1" ),
      t_( yxd, "YY-M-D", "22-13-7" )
   );

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

      bool res = write_ymd_c( rec, t->ymd, t->fmt );
      res &= recorded_is_c( rec, t->exp );

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

   return finish_tap_c_();
}