cHmsn
Overview
Types and Definitions
cHmsn
struct cHmsn
{
int32_t hour;
int8_t min;
int8_t sec;
int32_t nsec;
};
typedef struct cHmsn cHmsn;
The cHmsn struct can store a daytime or a duration separated into the attributes hour, min, sec and nsec.
Functions
overall
cmp_hmsn_c
int cmp_hmsn_c( cHmsn a, cHmsn b );
Compares two cHmsn 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/cHmsn.h"
int main( void )
{
init_tap_c_();
cHmsn hmsn = hms_c( 12, 4, 24 );
cHmsn before = hms_c( 8, 13, 0 );
cHmsn after = hms_c( 22, 3, 14 );
expect_eq_c_( cmp_hmsn_c( hmsn, hmsn ) );
expect_gt_c_( cmp_hmsn_c( hmsn, before ) );
expect_lt_c_( cmp_hmsn_c( hmsn, after ) );
return finish_tap_c_();
}
hms_c
cHmsn hms_c( int64_t hour, int64_t min, int64_t sec );
Initialise a cHmsn value with hour, minute and second.
hmsn_c
cHmsn hmsn_c( int64_t hour, int64_t min, int64_t sec, int64_t nsec );
Initialise a cHmsn value with hour, minute, second and nanosecond.
hmsn_is_valid_c
bool hmsn_is_valid_c( cHmsn hmsn );
Returns true if the cHmsn value is valid, otherwise false.
#include "clingo/lang/expect.h"
#include "clingo/time/cHmsn.h"
int main( void )
{
init_tap_c_();
expect_c_( hmsn_is_valid_c( hms_c( 1, 20, 0 ) ) );
expect_c_( !hmsn_is_valid_c( hms_c( 1, 65, 9 ) ) );
expect_c_( hmsn_is_valid_c( hmsn_c( 0, 15, 9, 5 ) ) );
return finish_tap_c_();
}
io
The functions read_hmsn_c and write_hmsn_c are using the following format:
h |
The hour without a leading character |
0 to 23 |
_h |
The hour with a leading space |
0 to 23 |
hh |
The hour with a leading zero |
00 to 23 |
k |
The kitchen hour without a leading character |
|
_k |
The kitchen hour with a leading space |
|
kk |
The kitchen hour with a leading zero |
|
m |
The minute without a leading character |
0 to 59 |
_m |
The minute with a leading space |
0 to 59 |
mm |
The minute with a leading zero |
00 to 59 |
s |
The whole second, without a leading character |
0 to 59 |
_s |
The whole second, with a leading space |
0 to 59 |
ss |
The whole second, with a leading zero |
00 to 59 |
i |
The fractional part of the second in millisecond whithout trainling zeros |
000 to 999 |
iii |
The fractional part of the second in millisecond with trailing zeros |
000 to 999 |
u |
The fractional part of the second in microsecond without trailing zeros |
000000 to 999999 |
uuu |
The fractional part of the second in microsecond with trailing zeros |
000000 to 999999 |
n |
The fractional part of the second in nanosecond without trailing zeros |
000000000 to 999999999 |
nnn |
The fractional part of the second in nanosecond with trailing zeros |
000000000 to 999999999 |
ap |
Read or write AM or PM dependent on the hour value |
|
AP |
Read or write am or pm dependent on the hour value |
read_hmsn_c
#define read_hmsn_c_( Sca, Hmsn ) \
read_hmsn_c( (Sca), (Hmsn), "" )
bool read_hmsn_c( cScanner sca[static 1],
cHmsn hmsn[static 1],
char const fmt[static 1] );
Reads a cHmsn value from a text with a scanner. The function will use C_DaytimeFormat( "hh:mm:ss.n" ) as default format.
#include "clingo/io/read.h"
#include "clingo/lang/expect.h"
#include "clingo/time/cHmsn.h"
TEMP_SLICE_C_(
test,
{
char const* str;
char const* fmt;
cHmsn exp;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
testSlice tests = slice_c_( test,
t_( "180312", "hhmmss", hmsn_c( 18, 3, 12, 0 ) ),
t_( "18:03:12.1", "hh:mm:ss.i", hmsn_c( 18, 3, 12, 100000000 ) ),
t_( "18:03:12.001", "hh:mm:ss.iii", hmsn_c( 18, 3, 12, 1000000 ) ),
t_( "03:04:15.0 pm", "kk:mm:ss.u ap", hmsn_c( 15, 4, 15, 0 ) )
);
for_each_c_( test const*, t, tests )
{
cScanner* sca = &cstr_scanner_c_( t->str );
cHmsn hmsn;
bool res = read_hmsn_c( sca, &hmsn, t->fmt );
res &= eq_c( cmp_hmsn_c( hmsn, t->exp ) );
cRecorder* rec = &recorder_c_( 32 );
write_hmsn_c_( rec, hmsn );
tap_descf_c( res, "%s -> %s", t->fmt, turn_into_cstr_c( rec ) );
}
return finish_tap_c_();
}
write_hmsn_c
#define write_hmsn_c_( Rec, Hmsn ) \
write_hmsn_c( (Rec), (Hmsn), "" )
bool write_hmsn_c( cRecorder rec[static 1],
cHmsn hmsn,
char const fmt[static 1] );
Writes a cHmsn value into the recorder. The function will use C_DaytimeFormat( "hh:mm:ss.n" ) as default format.
#include "clingo/lang/expect.h"
#include "clingo/io/write.h"
#include "clingo/time/cHmsn.h"
TEMP_SLICE_C_(
test,
{
cHmsn hmsn;
char const* fmt;
char const* exp;
}
)
#define t_( ... ) ((test){__VA_ARGS__})
int main( void )
{
init_tap_c_();
cHmsn hmsn = hmsn_c( 18, 3, 12, 10234567 );
testSlice tests = slice_c_( test,
t_( hmsn, "hhmmss", "180312" ),
t_( hmsn, "hh:mm:ss.i", "18:03:12.01" ),
t_( hmsn, "hh:mm:ss.iii", "18:03:12.010" ),
t_( hmsn, "+kk:mm:ss.u ap", "+06:03:12.010234 pm" ),
t_( hmsn, "_k:_m AP", " 6: 3 PM" )
);
for_each_c_( test const*, t, tests )
{
cRecorder* rec = &recorder_c_( 32 );
bool res = write_hmsn_c( rec, t->hmsn, t->fmt );
res &= recorded_is_c( rec, t->exp );
tap_descf_c( res, "test: '%s' -> '%s'", t->fmt, turn_into_cstr_c( rec ) );
}
return finish_tap_c_();
}