cWeekDate

Overview

Types and Definitions

cWeekDate

struct cWeekDate
{
   int32_t year;
   int8_t week;
   c_Weekday day;
};
typedef struct cWeekDate cWeekDate;

A cWeekDate struct represents a week date.

Generated

cWeekDateSlice

struct cWeekDateSlice
{
   int64_t s;
   cWeekDate const* v;
};
typedef struct cWeekDateSlice cWeekDateSlice;

Via the macro SLICES_C_ generated struct.

cVarWeekDateSlice

struct cVarWeekDateSlice
{
   int64_t s;
   cWeekDate* v;
};
typedef struct cVarWeekDateSlice cVarWeekDateSlice;

Via the macro SLICES_C_ generated struct.

Functions

overall

cmp_week_date_c

int cmp_week_date_c( cWeekDate a, cWeekDate b );

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

int main( void )
{
   init_tap_c_();

   cWeekDate a = week_date_c( 2014, 31, c_Mon );
   cWeekDate b = week_date_c( 2014, 31, c_Mon );
   cWeekDate c = week_date_c( 2014, 31, c_Fri );

   expect_eq_c_( cmp_week_date_c( a, b ) );
   expect_lt_c_( cmp_week_date_c( a, c ) );

   return finish_tap_c_();
}

week_date_c

cWeekDate week_date_c( int64_t year, int64_t week, int64_t day );

Initialise a week date with a year, week and weekday value.

week_date_is_valid_c

bool week_date_is_valid_c( cWeekDate wd );

Returns true if the specified week date is valid, otherwise false.

from

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

TEMP_SLICE_C_(
   test,
   {
      cYmd ymd;
      cWeekDate wd;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // 1976 - 1977
      t_( ymd_c( 1977, c_Jan,  1 ), week_date_c( 1976, 53, c_Sat ) ),
      t_( ymd_c( 1977, c_Jan,  2 ), week_date_c( 1976, 53, c_Sun ) ),
   // 1977 - 1978
      t_( ymd_c( 1977, c_Dec, 31 ), week_date_c( 1977, 52, c_Sat ) ),
      t_( ymd_c( 1978, c_Jan,  1 ), week_date_c( 1977, 52, c_Sun ) ),
      t_( ymd_c( 1978, c_Jan,  2 ), week_date_c( 1978,  1, c_Mon ) ),
   // 1978 - 1979
      t_( ymd_c( 1978, c_Dec, 31 ), week_date_c( 1978, 52, c_Sun ) ),
      t_( ymd_c( 1979, c_Jan,  1 ), week_date_c( 1979,  1, c_Mon ) ),
   // 1979 - 1980
      t_( ymd_c( 1979, c_Dec, 30 ), week_date_c( 1979, 52, c_Sun ) ),
      t_( ymd_c( 1979, c_Dec, 31 ), week_date_c( 1980,  1, c_Mon ) ),
      t_( ymd_c( 1980, c_Jan,  1 ), week_date_c( 1980,  1, c_Tue ) ),
   // 1980 - 1981
      t_( ymd_c( 1980, c_Dec, 28 ), week_date_c( 1980, 52, c_Sun ) ),
      t_( ymd_c( 1980, c_Dec, 29 ), week_date_c( 1981,  1, c_Mon ) ),
      t_( ymd_c( 1980, c_Dec, 30 ), week_date_c( 1981,  1, c_Tue ) ),
      t_( ymd_c( 1980, c_Dec, 31 ), week_date_c( 1981,  1, c_Wed ) ),
      t_( ymd_c( 1981, c_Jan,  1 ), week_date_c( 1981,  1, c_Thu ) ),
   // 2008
      t_( ymd_c( 2008, c_Sep, 26 ), week_date_c( 2008, 39, c_Fri ) )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 32 );
      write_week_date_c_( rec, t->wd );
      record_chars_c_( rec, " / " );
      write_ymd_c_( rec, t->ymd );
      tap_note_c( turn_into_cstr_c( rec ) );

      {  //----------------------------------------------- week_date_from_ymd_c
         cWeekDate wd = week_date_from_ymd_c( t->ymd );

         write_week_date_c_( rec, wd );

         bool res = eq_c( cmp_week_date_c( wd, t->wd ) );
         tap_desc_c( res, turn_into_cstr_c( rec ) );
      }
      {  //----------------------------------------------- ymd_from_week_date_c
         cYmd ymd = ymd_from_week_date_c( t->wd );

         write_ymd_c_( rec, ymd );

         bool res = eq_c( cmp_ymd_c( ymd, t->ymd ) );
         tap_desc_c( res, turn_into_cstr_c( rec ) );
      }
   }

   return finish_tap_c_();
}

week_date_from_ymd_c

cWeekDate week_date_from_ymd_c( cYmd ymd );

Creates a cWeekDate value from a cYmd value. An invalid cYmd value leads to an invalid cWeekDate value.

ymd_from_week_date_c

cYmd ymd_from_week_date_c( cWeekDate wd );

Creates a cYmd value from a cWeekDate value. An invalid cWeekDate value leads to an invalid cYmd value.

io

The functions read_week_date_c and write_week_date_c are using the following format:

Table 1. format

E

The weekday as a number

1 to 7

EEE

The abbreviated localized weekday name

Mon to Sun

EEEE

The localized weekday name

Monday to Sunday

WW

The ISO week without a leading zero

W1 to W53

WWW

The ISO week with a leading zero

W01 to W53

XX

The ISO week date year as a two digit number

00 to 99

XXXX

The ISO week date year as a four digit number

like 1878

read_week_date_c

#define read_week_date_c_( Sca, Wd )                                           \
   read_week_date_c( (Sca), (Wd), "" )
bool read_week_date_c( cScanner sca[static 1],
                       cWeekDate wd[static 1],
                       char const fmt[static 1] );

Reads a cWeekDate value from a text with a scanner. The function will use C_IsoWeekDate( "XXXX-WWW-E" ) as default format.

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

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

int main( void )
{
   init_tap_c_();

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

   cWeekDate wd = week_date_c( 2014, 7, c_Fri );
   testSlice tests = slice_c_( test,
      t_( "2014-W07-5", "XXXX-WWW-E", wd, cNoError_ ),
      t_( "14W7 Fri", "XXWW EEE", wd, cNoError_ )
   );

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

      cWeekDate wd = week_date_c( 1970, 1, c_Mon );
      bool res = read_week_date_c( sca, &wd, t->fmt );
      res &= eq_c( cmp_week_date_c( wd, t->exp ) );

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

   return finish_tap_c_();
}

write_week_date_c

#define write_week_date_c_( Rec, Wd )                                          \
   write_week_date_c( (Rec), (Wd), "" )
bool write_week_date_c( cRecorder rec[static 1],
                        cWeekDate wd,
                        char const fmt[static 1] );

Writes a cWeekDate value into the recorder. The function will use C_IsoWeekDate( "XXXX-WWW-E" ) as default format.

Example
#include "clingo/io/c_ImpExpError.h"
#include "clingo/io/write.h"
#include "clingo/lang/error.h"
#include "clingo/lang/expect.h"
#include "clingo/lang/locale.h"
#include "clingo/time/cWeekDate.h"

TEMP_SLICE_C_(
   test,
   {
      cWeekDate wd;
      char const* fmt;
      char const* exp;
      int err;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

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

   //********************************************************************* tests
   cWeekDate wd = week_date_c( 2014, 7, c_Fri );
   testSlice tests = slice_c_( test,
      t_( wd, "XXWW EEE", "14W7 Fri", cNoError_ ),
      t_( wd, "XXXX-WWW-E", "2014-W07-5", cNoError_ ),
      t_( wd, "XX/WW/PPP", "14/W7/PPP", cNoError_ )
   );

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

      bool res = write_week_date_c( rec, t->wd, t->fmt );
      res &= recorded_is_c( rec, t->exp );

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

   //**************************************************************** withErrors
   testSlice withErrors = slice_c_( test,
      t_( wd, "XX/W", "", c_InvalidFormatString )
   );

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

      bool res = not write_week_date_c( rec, t->wd, t->fmt );
      res &= ( rec->err == t->err );
      res &= recorded_is_c( rec, t->exp );

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

   return finish_tap_c_();
}