qLibc
qtime.c File Reference

Time handling APIs. More...

Go to the source code of this file.

Functions

long qtime_current_milli (void)
 Returns the current time in milliseconds.
char * qtime_localtime_strf (char *buf, int size, time_t utctime, const char *format)
 Format a local time string with a custom format.
char * qtime_localtime_str (time_t utctime)
 Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
const char * qtime_localtime_staticstr (time_t utctime)
 Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
char * qtime_gmt_strf (char *buf, int size, time_t utctime, const char *format)
 Format a GMT time string with a custom format.
char * qtime_gmt_str (time_t utctime)
 Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
const char * qtime_gmt_staticstr (time_t utctime)
 Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
time_t qtime_parse_gmtstr (const char *gmtstr)
 Parse a GMT or timezone-formatted time string and return UTC time.

Detailed Description

Time handling APIs.

Definition in file qtime.c.

Macro Definition Documentation

◆ __USE_XOPEN

#define __USE_XOPEN

Definition at line 33 of file qtime.c.

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Definition at line 34 of file qtime.c.

◆ _BSD_SOURCE

#define _BSD_SOURCE

Definition at line 35 of file qtime.c.

Function Documentation

◆ qtime_current_milli()

long qtime_current_milli ( void )

Returns the current time in milliseconds.

Returns
current time in milliseconds.

Definition at line 49 of file qtime.c.

◆ qtime_localtime_strf()

char * qtime_localtime_strf ( char * buf,
int size,
time_t utctime,
const char * format )

Format a local time string with a custom format.

Parameters
bufoutput buffer
sizebuffer size
utctime0 for the current time, or a specific UTC time
formatformat string for strftime()
Returns
pointer to buf
char *timestr = qtime_localtime_strf(0, "%H:%M:%S"); // HH:MM:SS
free(timestr);
char *timestr = qtime_localtime_strf(0, "%Y%m%d%H%M%S"); // YYMMDDhhmmss
free(timestr);
char * qtime_localtime_strf(char *buf, int size, time_t utctime, const char *format)
Format a local time string with a custom format.
Definition qtime.c:73

Definition at line 73 of file qtime.c.

◆ qtime_localtime_str()

char * qtime_localtime_str ( time_t utctime)

Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.

Parameters
utctime0 for the current time, or a specific UTC time
Returns
allocated time string.
char *timestr;
timestr = qtime_localtime_str(0); // now
free(timestr);
timestr = qtime_localtime_str(time(NULL)); // same as above
free(timestr);
timestr = qtime_localtime_str(time(NULL) - 86400)); // 1 day before
free(timestr);
char * qtime_localtime_str(time_t utctime)
Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
Definition qtime.c:103

Definition at line 103 of file qtime.c.

◆ qtime_localtime_staticstr()

const char * qtime_localtime_staticstr ( time_t utctime)

Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.

Parameters
utctime0 for the current time, or a specific UTC time
Returns
internal static time string.
printf("%s", qtime_localtime_staticstr(0)); // now
printf("%s", qtime_localtime_staticstr(time(NULL) + 86400)); // 1 day later
const char * qtime_localtime_staticstr(time_t utctime)
Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
Definition qtime.c:125

Definition at line 125 of file qtime.c.

◆ qtime_gmt_strf()

char * qtime_gmt_strf ( char * buf,
int size,
time_t utctime,
const char * format )

Format a GMT time string with a custom format.

Parameters
bufoutput buffer
sizebuffer size
utctime0 for the current time, or a specific UTC time
formatformat string for strftime()
Returns
pointer to buf
char timestr[8+1];
qtime_gmt_strf(buf, sizeof(buf), 0, "%H:%M:%S"); // HH:MM:SS
char * qtime_gmt_strf(char *buf, int size, time_t utctime, const char *format)
Format a GMT time string with a custom format.
Definition qtime.c:148

Definition at line 148 of file qtime.c.

◆ qtime_gmt_str()

char * qtime_gmt_str ( time_t utctime)

Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.

Parameters
utctime0 for the current time, or a specific UTC time
Returns
allocated GMT time string.
char *timestr;
timestr = qtime_gmt_str(0); // now
free(timestr);
timestr = qtime_gmt_str(time(NULL)); // same as above
free(timestr);
timestr = qtime_gmt_str(time(NULL) - 86400)); // 1 day before
free(timestr);
char * qtime_gmt_str(time_t utctime)
Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
Definition qtime.c:174

Definition at line 174 of file qtime.c.

◆ qtime_gmt_staticstr()

const char * qtime_gmt_staticstr ( time_t utctime)

Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.

Parameters
utctime0 for the current time, or a specific UTC time
Returns
internal static GMT time string.
printf("%s", qtime_gmt_staticstr(0)); // now
printf("%s", qtime_gmt_staticstr(time(NULL) + 86400)); // 1 day later
const char * qtime_gmt_staticstr(time_t utctime)
Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
Definition qtime.c:197

Definition at line 197 of file qtime.c.

◆ qtime_parse_gmtstr()

time_t qtime_parse_gmtstr ( const char * gmtstr)

Parse a GMT or timezone-formatted time string and return UTC time.

Examples: 'Sun, 04 May 2008 18:50:39 GMT' 'Mon, 05 May 2008 03:50:39 +0900'

Parameters
gmtstrGMT/timezone-formatted time string
Returns
UTC time. Returns -1 on conversion error.
time_t t = time(NULL);
char *s = qtime_parse_gmtstr(t);
printf("%d\n", t);
printf("%s\n", s);
printf("%d\n", qtime_parse_gmtstr(s)); // this must be same as t
free(s);
time_t qtime_parse_gmtstr(const char *gmtstr)
Parse a GMT or timezone-formatted time string and return UTC time.
Definition qtime.c:225

Definition at line 225 of file qtime.c.