libsacio  1.0.2
Seismic Analysis Code I/O Library
Data Structures | Enumerations | Functions
time

Long duration, high precision time manipulation. More...

Data Structures

struct  timespec64
 High-precision, long duration time value. More...
 
struct  duration
 time duration with units More...
 

Enumerations

enum  TimeType {
  Duration_None = 0, Duration_Seconds = 1, Duration_Minutes = 2, Duration_Hours = 3,
  Duration_Days = 4, Duration_Weeks = 5, Duration_Months = 6, Duration_Years = 7,
  Duration_Decades = 8, Duration_Centuries = 9
}
 duration units
 

Functions

timespec64 timespec64::timespec64_now ()
 Get now as a timespec64 value. More...
 
timespec64 timespec64::timespec64_undef ()
 Get the time associated with Year 0, Day 1. More...
 
timespec64 timespec64::timespec64_from_yjhmsf (int64_t year, int jday, int hour, int min, int sec, int64_t ns)
 Create a timespec64 value from Year, Day of Year, Hour, Minute, Second, Nanosecond. More...
 
timespec64 timespec64::timespec64_from_ymdhmsf (int64_t year, int month, int day, int hour, int min, int sec, int64_t ns)
 Create a timespec64 value from Year, Month, Day, Hour, Minute, Second, Nanosecond. More...
 
int timespec64::timespec64_parse (const char *buf, timespec64 *t)
 Parse a string into a timespec64 value. More...
 
void timespec64::timespec64_print (timespec64 *t)
 Print out a timespec64 value. More...
 
timespec64 timespec64::timespec64_add_duration (timespec64 a, duration *d)
 Add a duration to a timespec64. More...
 
int timespec64::timespec64_cmp (timespec64 *a, timespec64 *b)
 Compare two timespec64 values. More...
 
char * timespec64::strptime64t (const char *buf, const char *fmt, timespec64 *t)
 Parse a string into a timespec64 value. More...
 
size_t timespec64::strftime64t (char *dst, size_t n, const char *fmt, timespec64 *t)
 Format a struct TM value. More...
 
void duration::duration_init (duration *d)
 Initialize a duration. More...
 
int duration::duration_parse (char *in, duration *d)
 Parse a string into a duration. More...
 
durationduration::duration_from_string (char *in)
 Create a duration from a character stirng. More...
 
void timespec64::timespec64_to_ymd (timespec64 *t, int64_t *year, int *month, int *day, int *oday)
 Get dates values from timespec64. More...
 

Detailed Description

Long duration, high precision time manipulation.

Function Documentation

◆ duration_from_string()

duration * duration_from_string ( char *  in)

Create a duration from a character stirng.

Parameters
incharacter string to parse into a duration
Returns
parsed duration, or NULL on error
Warning
User owns the duration and is responsibile for freeing the underlying memory
assert_eq(d->n, 3000);
assert_eq(d->type, Duration_Seconds);

◆ duration_init()

void duration_init ( duration d)

Initialize a duration.

Parameters
dduration to initialize
duration d = {0,0};
assert_eq(d.n, 0);
assert_eq(d.type, Duration_None);

◆ duration_parse()

int duration_parse ( char *  in,
duration d 
)

Parse a string into a duration.

Parameters
incharacter string to parse
dduration output from string
Returns
1 on success, 0 in failure

Format for a duration is [-+]###{unit} where:

  • ### is a integer
  • {unit} is one of the following
    • s, sec, secs, seconds
    • m, min, mins, minutes
    • h, hrs, hours
    • d, days
    • w, wk, wks, weeks
    • mon, months
    • y, yr, yrs, years
    • dec, decades
    • cent, centuries
  • durations are allowed to be positive or negative
duration d = {0,0};
duration_parse("8w", &d);
assert_eq(d.n, 8);
assert_eq(d.type, Duration_Weeks);
duration_parse("50years", &d);
assert_eq(d.n, 50);
assert_eq(d.type, Duration_Years);
duration_parse("-15days", &d);
assert_eq(d.n, -15);
assert_eq(d.type, Duration_Days);

◆ strftime64t()

size_t strftime64t ( char *  dst,
size_t  n,
const char *  fmt,
timespec64 t 
)

Format a struct TM value.

Format a timespec64 according to the format specifiers

  • %%% character
  • %Y – Year, 4 digits, zero padded
  • %j – Day of the year, 3 digits, zero padded
  • %m – Month number, 2 digits, zero padded
  • %d – Day of the month, 2 digits, zero padded
  • %H – Hour, 2 digits, zero padded
  • %M – Minute, 2 digits, zero padded
  • %S – Seconds, 2 digits, zero padded
  • %f – partial seconds as nanoseconds, prefix digit defines the number of leading digits to show
  • %F – alias for %Y-%m-%d
  • %T – alias for %H:%M:%S
  • %b – Abbreviated Month Name
Parameters
dstOutput character string
nLength of dst
fmtFormat for character string
tinput timespec64 value
Returns
length of output character string, 0 on failure
char out[64] = {0};
timespec64 t = {0,0};
strptime64t("1994 03 01 23:59:47", "%Y %m %d %H:%M:%S", &t);
strftime64t(out, sizeof out, "%F %T", &t);
assert_eq(strcmp(out, "1994-03-01 23:59:47"), 0);

◆ strptime64t()

char * strptime64t ( const char *  buf,
const char *  fmt,
timespec64 t 
)

Parse a string into a timespec64 value.

Parse a timespec64 according to the format specifiers

  • %%% characters
  • %Y – Year
  • %j – Day of the year, 1-366
  • %m – Month number, 1-12
  • %d – Day of the month, 1-31
  • %H – Hour, 0-23
  • %M – Minute, 0-59
  • %S – Seconds, 0-60
  • %f – partial seconds as nanoseconds
Parameters
bufInput character string
fmtFormat to interpret the charater string
toutput timespec64 value to place values into
Returns
first character not read based on format during success, NULL on failure
char out[64] = {0};
timespec64 t = {0,0};
strptime64t("1994 03 01 23:59:47", "%Y %m %d %H:%M:%S", &t);
strftime64t(out, sizeof out, "%F %T", &t);
assert_eq(strcmp(out, "1994-03-01 23:59:47"), 0);

◆ timespec64_add_duration()

timespec64 timespec64_add_duration ( timespec64  a,
duration d 
)

Add a duration to a timespec64.

Parameters
atimespec64 value
dduration to add to to timespec64
Returns
new timespec64 value (a + d)
char out[64] = {0};
timespec64 t = {0,0};
timespec64 t1 = {0,0};
duration d = {0,0};
timespec64_parse("1970/001", &t);
assert_eq(t.tv_sec, 0);
duration_parse("1year", &d);
assert_eq(d.type, Duration_Years);
assert_eq(d.n, 1);
assert_eq(t1.tv_sec, 365*24*60*60);
assert_eq(t1.tv_nsec, 0);
strftime64t(out, sizeof out, "%F %T", &t1);
assert_eq(strcmp(out, "1971-01-01 00:00:00"), 0);

◆ timespec64_cmp()

int timespec64_cmp ( timespec64 a,
timespec64 b 
)

Compare two timespec64 values.

Parameters
aFirst timespec64 value
bSecond timespec64 value
Returns
  • 0 if a == b
  • -1 if a < b
  • +1 if a > b
timespec64 a = {0,0};
timespec64 b = {0,0};
timespec64_parse("1994/01", &a);
timespec64_parse("1994/02", &b);
assert_eq(timespec64_cmp(&a, &b), -1);
assert_eq(timespec64_cmp(&b, &a), +1);
assert_eq(timespec64_cmp(&a, &a), 0);

◆ timespec64_from_yjhmsf()

timespec64 timespec64_from_yjhmsf ( int64_t  year,
int  jday,
int  hour,
int  min,
int  sec,
int64_t  ns 
)

Create a timespec64 value from Year, Day of Year, Hour, Minute, Second, Nanosecond.

Parameters
yearYear
jdayDay of the year
hourHour
minMinute
secSecond
nsNanosecond
Returns
new timespec64 value
int64_t y = 0;
int m = 0, d = 0, j = 0;
timespec64 t = {0,0};
t = timespec64_from_yjhmsf(1999, 60, 10, 34, 59, 0);
timespec64_to_ymd(&t, &y, &m, &d, &j);
assert_eq(y, 1999);
assert_eq(m, 3);
assert_eq(d, 1);
assert_eq(j, 60);

◆ timespec64_from_ymdhmsf()

timespec64 timespec64_from_ymdhmsf ( int64_t  year,
int  month,
int  day,
int  hour,
int  min,
int  sec,
int64_t  ns 
)

Create a timespec64 value from Year, Month, Day, Hour, Minute, Second, Nanosecond.

Parameters
yearYear
monthMonth
dayMonth Day
hourHour
minMinute
secSecond
nsNanosecond
Returns
new timespec64 value
int64_t y = 0;
int m = 0, d = 0, j = 0;
timespec64 t = {0,0};
t = timespec64_from_ymdhmsf(2000, 3, 1, 10, 34, 59, 0);
timespec64_to_ymd(&t, &y, &m, &d, &j);
assert_eq(y, 2000);
assert_eq(m, 3);
assert_eq(d, 1);
assert_eq(j, 61);

◆ timespec64_now()

timespec64 timespec64_now ( )

Get now as a timespec64 value.

Returns
timepsec64 value filled with the current time
Note
Nanoseconds are always set to 0

◆ timespec64_parse()

int timespec64_parse ( const char *  buf,
timespec64 t 
)

Parse a string into a timespec64 value.

Parameters
bufInput character string
ttimespec64 value to parse into
Returns
1 on success, 0 on failure
char out[64] = {0};
timespec64 t = {0,0};
timespec64_parse("1994/160", &t);
strftime64t(out, sizeof out, "%F %T", &t);
assert_eq(strcmp(out, "1994-06-09 00:00:00"), 0);
timespec64_parse("1992-10-31T23:59:59.999", &t);
strftime64t(out, sizeof out, "%F %T", &t);
assert_eq(strcmp(out, "1992-10-31 23:59:59"), 0);

◆ timespec64_print()

void timespec64_print ( timespec64 t)

Print out a timespec64 value.

Parameters
ttimespec64
timespec64 t = {0,0};
timespec64_parse("1994/160", &t);

◆ timespec64_to_ymd()

void timespec64_to_ymd ( timespec64 t,
int64_t *  year,
int *  month,
int *  day,
int *  oday 
)

Get dates values from timespec64.

Get year, month, day and day of the year from a timespec64 value

Parameters
tinput timespec64 value
yearoutput year
monthoutput month
dayoutput day of the month
odayoutput day of the year
int64_t y = 0;
int m = 0, d = 0, j = 0;
timespec64 t = {0,0};
timespec64_parse("1999/365", &t);
timespec64_to_ymd(&t, &y, &m, &d, &j);
assert_eq(y, 1999);
assert_eq(m, 12);
assert_eq(d, 31);
assert_eq(j, 365);

◆ timespec64_undef()

timespec64 timespec64_undef ( )

Get the time associated with Year 0, Day 1.

Get the time associated with non-existant Year 0, Day 1. It is used as a constant for an uninitialized value. This year does not exist, but is actaully 1 BC. See Year zero. The value returned may change in the future.

Returns
timepsec64 value filled with the time of Year 0, Day 1
Note
Nanoseconds are always set to 0
assert_eq(t.tv_sec, -62167219200);
assert_eq(t.tv_nsec, 0);