cOrdinalDate
Overview
Types and Definitions
cOrdinalDate
struct cOrdinalDate
{
int32_t year;
int16_t day;
};
typedef struct cOrdinalDate cOrdinalDate;
A cOrdinalDate struct represents a ordinal date.
Generated
cOrdinalDateSlice
struct cOrdinalDateSlice
{
int64_t s;
cOrdinalDate const* v;
};
typedef struct cOrdinalDateSlice cOrdinalDateSlice;
Via the macro SLICES_C_ generated struct.
cVarOrdinalDateSlice
struct cVarOrdinalDateSlice
{
int64_t s;
cOrdinalDate* v;
};
typedef struct cVarOrdinalDateSlice cVarOrdinalDateSlice;
Via the macro SLICES_C_ generated struct.
Functions
overall
cmp_ordinal_date_c
int cmp_ordinal_date_c( cOrdinalDate a, cOrdinalDate b );
Compares two cOrdinalDate 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
#include "clingo/lang/expect.h"
#include "clingo/time/cOrdinalDate.h"
int main( void )
{
init_tap_c_();
cOrdinalDate od = ordinal_date_c( 2014, 125 );
cOrdinalDate before = ordinal_date_c( 2013, 256 );
cOrdinalDate after = ordinal_date_c( 2014, 126 );
expect_eq_c_( cmp_ordinal_date_c( od, od ) );
expect_gt_c_( cmp_ordinal_date_c( od, before ) );
expect_lt_c_( cmp_ordinal_date_c( od, after ) );
return finish_tap_c_();
}
ordinal_date_c
cOrdinalDate ordinal_date_c( int64_t year, int64_t day );
Initialise a ordinal date with a year and day value.
ordinal_date_is_valid_c
bool ordinal_date_is_valid_c( cOrdinalDate od );
Returns true if the specified ordinal date is valid, otherwise false.
#include "clingo/lang/expect.h"
#include "clingo/time/cOrdinalDate.h"
int main( void )
{
init_tap_c_();
expect_c_( ordinal_date_is_valid_c( ordinal_date_c( 2014, 365 ) ) );
expect_c_( !ordinal_date_is_valid_c( ordinal_date_c( 2014, 366 ) ) );
expect_c_( ordinal_date_is_valid_c( ordinal_date_c( 2012, 366 ) ) );
expect_c_( !ordinal_date_is_valid_c( ordinal_date_c( 2012, 367 ) ) );
return finish_tap_c_();
}
from
#include "clingo/lang/expect.h"
#include "clingo/time/cOrdinalDate.h"
TEMP_SLICE_C_(
test,
{
cYmd ymd;
cOrdinalDate od;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
testSlice tests = slice_c_( test,
t_( ymd_c( 2021, 4, 29 ), ordinal_date_c( 2021, 119 ) )
);
for_each_c_( test const*, t, tests )
{
cRecorder* rec = &recorder_c_( 32 );
write_ordinal_date_c_( rec, t->od );
record_chars_c_( rec, " / " );
write_ymd_c_( rec, t->ymd );
tap_note_c( turn_into_cstr_c( rec ) );
{ //-------------------------------------------- ordinal_date_from_ymd_c
cOrdinalDate od = ordinal_date_from_ymd_c( t->ymd );
write_ordinal_date_c_( rec, od );
bool res = eq_c( cmp_ordinal_date_c( od, t->od ) );
tap_desc_c( res, turn_into_cstr_c( rec ) );
}
{ //-------------------------------------------- ymd_from_ordinal_date_c
cYmd ymd = ymd_from_ordinal_date_c( t->od );
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_();
}
ordinal_date_from_ymd_c
cOrdinalDate ordinal_date_from_ymd_c( cYmd ymd );
Creates a cOrdinalDate value from a cYmd value. An invalid cYmd value leads to an invalid cOrdinalDate value.
ymd_from_ordinal_date_c
cYmd ymd_from_ordinal_date_c( cOrdinalDate od );
Creates a cYmd valud from a cOrdinalDate value. An invalid cOrdinalDate value leads to an invalid cYmd value.
io
The functions read_ordinal_date_c and write_ordinal_date_c are using the following format:
C |
The day of year as a number without a leading character |
1 to 365 |
__C |
The day of year as a number with leading spaces |
1 to 365 |
CCC |
The day of year as a number with leading zeros |
001 to 365 |
YY |
The year as a two digit number |
00 to 99 |
YYYY |
The year as a four digit number |
like 1878 |
read_ordinal_date_c
#define read_ordinal_date_c_( Sca, Od ) \
read_ordinal_day_c( (Sca), (Od), "" )
bool read_ordinal_date_c( cScanner sca[static 1],
cOrdinalDate od[static 1],
char const fmt[static 1] );
Reads a cOrdinalDate value from a text with a scanner. The function will use C_IsoOrdinalDate( "YYYY-CCC" ) as default format.
#include "clingo/io/c_ImpExpError.h"
#include "clingo/io/read.h"
#include "clingo/lang/expect.h"
#include "clingo/time/cOrdinalDate.h"
TEMP_SLICE_C_(
test,
{
char const* inp;
char const* fmt;
cOrdinalDate exp;
int err;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
//********************************************************************* tests
testSlice tests = slice_c_( test,
t_( "2014-023", "YYYY-CCC", ordinal_date_c( 2014, 23 ), cNoError_ ),
t_( "14/23", "YY/C", ordinal_date_c( 2014, 23 ), cNoError_ ),
t_( "2014-DDD.1", "YYYY-DDD.C", ordinal_date_c( 2014, 1 ), cNoError_ )
);
for_each_c_( test const*, t, tests )
{
cScanner* sca = &cstr_scanner_c_( t->inp );
cOrdinalDate od = ordinal_date_c( 1970, 1 );
bool res = read_ordinal_date_c( sca, &od, t->fmt );
res &= eq_c( cmp_ordinal_date_c( od, t->exp ) );
cRecorder* rec = &recorder_c_( 32 );
write_ordinal_date_c_( rec, od );
tap_descf_c( res, "%s -> %s", t->fmt, turn_into_cstr_c( rec ) );
}
//**************************************************************** withErrors
testSlice withErrors = slice_c_( test,
t_( "", "YYYY-CC", ordinal_date_c( 1970, 1 ), c_NotAbleToReadValue )
);
for_each_c_( test const*, t, withErrors )
{
cScanner* sca = &cstr_scanner_c_( t->inp );
cOrdinalDate od = ordinal_date_c( 1970, 1 );
bool res = not read_ordinal_date_c( sca, &od, t->fmt );
res &= ( sca->err == t->err );
res &= eq_c( cmp_ordinal_date_c( od, t->exp ) );
cRecorder* rec = &recorder_c_( 32 );
write_ordinal_date_c_( rec, od );
tap_descf_c( res, "%s -> %s", t->fmt, turn_into_cstr_c( rec ) );
}
return finish_tap_c_();
}
write_ordinal_date_c
#define write_ordinal_date_c_( Rec, Od ) \
write_ordinal_date_c( (Rec), (Od), "" )
bool write_ordinal_date_c( cRecorder rec[static 1],
cOrdinalDate od,
char const fmt[static 1] );
Writes a cOrdinalDate value into the recorder. The function will use C_IsoOrdinalDate( "YYYY-CCC" ) as default format.
#include "clingo/io/c_ImpExpError.h"
#include "clingo/io/write.h"
#include "clingo/lang/expect.h"
#include "clingo/time/cOrdinalDate.h"
TEMP_SLICE_C_(
test,
{
cOrdinalDate od;
int err;
char const* fmt;
char const* exp;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
cOrdinalDate od = ordinal_date_c( 2014, 23 );
testSlice tests = slice_c_( test,
t_( od, cNoError_, "YYYY-CCC", "2014-023" ),
t_( od, cNoError_, "YY/C", "14/23" ),
t_( od, c_InvalidFormatString, "YYYY-CC", "" ),
t_( od, cNoError_, "YYYY-DDD", "2014-DDD" )
);
for_each_c_( test const*, t, tests )
{
cRecorder* rec = &recorder_c_( 32 );
write_ordinal_date_c( rec, t->od, t->fmt );
bool 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_();
}