# cTime

## Types and Definitions

#### cTime

```struct cTime
{
int64_t _s;
int32_t _n;
int32_t _z;
};

typedef struct cTime cTime;```

The cTime struct has just the attriibutes _s, _n and _z that represent the time.

### Generated

#### cTimeSlice

```struct cTimeSlice
{
int64_t s;
cTime const* v;
};
typedef struct cTimeSlice cTimeSlice;```

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

```/* init */
cTimeSlice time_slice_c( int64_t s, cTime const* v );
cTimeSlice make_time_slice_c( cTime const* beg, cTime const* end );
cTimeSlice empty_time_slice_c( void );
/* sub */
cTimeSlice left_time_slice_c( cTimeSlice slice, int64_t maxLen );
cTimeSlice mid_time_slice_c( cTimeSlice slice, int64_t index );
cTimeSlice right_time_slice_c( cTimeSlice slice, int64_t maxLen );
cTimeSlice sub_time_slice_c( cTimeSlice slice,
int64_t begIdx,
int64_t endIdx);```

#### cVarTimeSlice

```struct cVarTimeSlice
{
int64_t s;
cTime* v;
};
typedef struct cVarTimeSlice cVarTimeSlice;```

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

```/* init */
cVarTimeSlice var_time_slice_c( int64_t s, cTime* v );
cVarTimeSlice make_var_time_slice_c( cTime* beg, cTime* end );
cVarTimeSlice empty_var_time_slice_c( void );
/* sub */
cVarTimeSlice left_var_time_slice_c( cVarTimeSlice slice, int64_t maxLen );
cVarTimeSlice mid_var_time_slice_c( cVarTimeSlice slice, int64_t index );
cVarTimeSlice right_var_time_slice_c( cVarTimeSlice slice, int64_t maxLen );
cVarTimeSlice sub_var_time_slice_c( cVarTimeSlice slice,
int64_t begIdx,
int64_t endIdx);
/* var slice */
cVarTimeSlice as_var_time_slice_c( cVarTimeSlice slice );
cVarTimeSlice cast_var_time_slice_c( cVarTimeSlice slice, cTimeSlice sub );
int64_t set_time_slice_c( cVarTimeSlice dst, cTimeSlice src );```

## Functions

### init

#### local_time_c

`cTime local_time_c( void );`

Returns the current local time, as reported by the system clock.

#### make_time_c

```cTime make_time_c( int64_t year, int64_t month, int64_t day,
int64_t hour, int64_t min, int64_t sec, int64_t nsec,
cTzOffset tz );```

Initialse all time components.

#### null_time_c

`cTime null_time_c( void );`

Returns a null time, null times are invalid.

#### time_c

`cTime time_c( cDate d, cDaytime dt, cTzOffset tz );`

Initialise a time with a date, daytime and time zone.

#### utc_time_c

`cTime utc_time_c( void );`

Returns the current UTC time, as reported by the system clock.

### timestamp

#### as_timestamp_c

`cTimestamp as_timestamp_c( cTime time );`

Returns the cTime value as timestamp.

#### from_timestamp_c

`cTime from_timestamp_c( cTimestamp ts );`

Creates a cTime value from a timestamp.

### get

#### get_date_c

`cDate get_date_c( cTime time );`

Returns the date of a time value.

#### get_daytime_c

`cDaytime get_daytime_c( cTime time );`

Returns the daytime of a time value.

#### get_hmsn_c

`cHmsn get_hmsn_c( cTime time );`

Returns the daytime of a time value as cHmsn value.

#### get_ordinal_date_c

`cOrdinalDate get_ordinal_date_c( cTime time );`

Returns the date of a time value as ordinal date.

#### get_tz_offset_c

`cTzOffset get_tz_offset_c( cTime time );`

Returns the time zone of a time value.

#### get_week_date_c

`cWeekDate get_week_date_c( cTime time );`

Returns the date of a time value as week date.

#### get_ymd_c

`cYmd get_ymd_c( cTime time );`

Returns the date of a time value as cYmd value.

`cTime add_to_time_c( cTime time, cDuration dur );`

Adds a duration to a time value.

#### between_times_c

`cDuration between_times_c( cTime a, cTime b );`

Returns the duration between two time values.

#### shift_time_c

`cTime shift_time_c( cTime time, cTzOffset tz );`

Shifts the time to another time zone.

### cmp

#### cmp_time_c

`int cmp_time_c( cTime a, cTime b );`

Compares two time values and returns the three possible results:

<0

if a is earlier as b

0

if both times are equal

>0

if a is later as b

#### early_time_c

`cTime early_time_c( cTime a, cTime b );`

Returns the earlier of both times. An invalid time is allways earlier as a valid time.

#### eq_time_c

`bool eq_time_c( cTime a, cTime b );`

Returns true if a is equal to b, otherwise false.

#### late_time_c

`cTime late_time_c( cTime a, cTime b );`

Return the later of both times. A valid time is allways later as a invalid time.

#### time_is_valid_c

`bool time_is_valid_c( cTime time );`

Returns true if the time value is valid, otherwise false.

### io

The functions read_time_c and write_time_c are using the following format:

```#define read_time_c_( Sca, Date )                                              \
cTime time[static 1],
char const fmt[static 1] );```

Reads a cTime value from a text with a scanner. The function will use C_TimeFormat( "YYYY.MM.DD hh:mm:ss oooo" ) as default format.

Example
```#include "clingo/lang/expect.h"
#include "clingo/time/C_TimeFormats.h"
#include "clingo/time/cTime.h"

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

int main( void )
{
init_tap_c_();

testSlice tests = slice_c_( test,
t_( "2006-01-02T15:04:05Z", C_Rfc3339Time,
make_time_c( 2006, c_Jan, 2, 15, 4, 5, 0, utc_c() ) ),
t_( "21.06.12", "YY.MM.DD",
make_time_c( 2021, c_Jun, 12, 0, 0, 0, 0, utc_c() ) )
);

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

cTime time;
bool res = read_time_c( sca, &time, t->fmt );
res = eq_time_c( time, t->exp );

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

return finish_tap_c_();
}```

#### write_time_c

```#define write_time_c_( Rec, Date )                                             \
write_time_c( (Rec), (Date), "" )
bool write_time_c( cRecorder rec[static 1],
cTime time,
char const fmt[static 1] );```

Writes a cTime value into the recorder. The function will use C_TimeFormat( "YYYY.MM.DD hh:mm:ss oooo" ) as default format.

Example
```#include "clingo/lang/expect.h"
#include "clingo/time/C_TimeFormats.h"
#include "clingo/time/cTime.h"

TEMP_SLICE_DEF_C_(
test,
{
cTime time;
char const* fmt;
char const* exp;
}
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
init_tap_c_();

testSlice tests = slice_c_( test,
t_( make_time_c( 2006, c_Jan, 2, 15, 4, 5, 0, utc_c() ),
C_Rfc3339Time, "2006-01-02T15:04:05Z" ),
t_( make_time_c( 2021, c_Jun, 12, 0, 0, 0, 0, utc_c() ),
"YY.MM.DD", "21.06.12" )
);

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

bool res = write_time_c( rec, t->time, t->fmt );
res &= recorded_is_c( rec, t->exp );

tap_descf_c( res, "%s -> %s", t->fmt, turn_into_cstr_c( rec ) );
}

return finish_tap_c_();
}```
{"ft_lang_label":"__label__en","ft_lang_prob":0.59974986,"math_prob":0.9787115,"size":9338,"snap":"2022-27-2022-33","text_gpt3_token_len":2913,"char_repetition_ratio":0.17859438,"word_repetition_ratio":0.29397142,"special_character_ratio":0.32812166,"punctuation_ratio":0.1581121,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9738224,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-04T06:13:57Z\",\"WARC-Record-ID\":\"<urn:uuid:d658d68f-b400-43c0-9b9a-d3df1552471f>\",\"Content-Length\":\"66946\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:13002993-d402-4a28-a24e-d419965bae71>\",\"WARC-Concurrent-To\":\"<urn:uuid:b074f56d-7288-48b4-9c0d-b3010830ed88>\",\"WARC-IP-Address\":\"138.201.248.18\",\"WARC-Target-URI\":\"https://clingo.aiq.dk/time/cTime.html\",\"WARC-Payload-Digest\":\"sha1:74WQVLNJ254IDV5Z42DSM6DS5LBYDIQK\",\"WARC-Block-Digest\":\"sha1:TP5XGSLQQMB5P5D5IJ2MNTPKDIZUOI7U\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104354651.73_warc_CC-MAIN-20220704050055-20220704080055-00304.warc.gz\"}"}