Time and Date Conversions

Time and Date Conversions — Converting from timestamps to date structures and back

Functions

Types and Values

Includes

#include <libchimara/glk.h>

Description

This section describes functions for converting timestamps to more human-readable date structures and back.

Functions

glk_time_to_date_utc ()

void
glk_time_to_date_utc (glktimeval_t *time,
                      glkdate_t *date);

Convert the given timestamp (as returned by glk_current_time()) to a broken-out structure. This function returns a date and time in universal time (GMT).

The seconds value may be 60 because of a leap second.

Parameters

time

A glktimeval_t structure as returned by glk_current_time().

 

date

An empty glkdate_t structure to fill in.

 

glk_time_to_date_local ()

void
glk_time_to_date_local (glktimeval_t *time,
                        glkdate_t *date);

Does the same thing as glk_time_to_date_utc(), but this function returns local time.

Parameters

time

A glktimeval_t structure as returned by glk_current_time().

 

date

An empty glkdate_t structure to fill in.

 

glk_simple_time_to_date_utc ()

void
glk_simple_time_to_date_utc (glsi32 time,
                             glui32 factor,
                             glkdate_t *date);

Convert the given timestamp (as returned by glk_current_simple_time()) to a broken-out structure in universal time. The time argument is multiplied by factor to produce a Unix timestamp.

Since the resolution of glk_simple_time_to_date_utc() and glk_simple_time_to_date_local() is no better than seconds, they will return zero for the microseconds value.

Parameters

time

Timestamp as returned by glk_current_simple_time().

 

factor

Factor by which to multiply time in order to get seconds.

 

date

An empty glkdate_t structure to fill in.

 

glk_simple_time_to_date_local ()

void
glk_simple_time_to_date_local (glsi32 time,
                               glui32 factor,
                               glkdate_t *date);

Does the same thing as glk_simple_time_to_date_utc(), but fills in the date structure in local time.

Parameters

time

Timestamp as returned by glk_current_simple_time().

 

factor

Factor by which to multiply time in order to get seconds.

 

date

An empty glkdate_t structure to fill in.

 

glk_date_to_time_utc ()

void
glk_date_to_time_utc (glkdate_t *date,
                      glktimeval_t *time);

Convert the broken-out structure (interpreted as universal time) to a timestamp. The weekday value in date is ignored. The other values need not be in their normal ranges; they will be normalized.

If the time cannot be represented by the platform's time library, this may return -1 for the seconds value. (I.e., the high_sec and low_sec fields both $FFFFFFFF. The microseconds field is undefined in this case.)

Parameters

date

A date in the form of a glkdate_t structure.

 

time

An empty glktimeval_t structure to fill in.

 

glk_date_to_time_local ()

void
glk_date_to_time_local (glkdate_t *date,
                        glktimeval_t *time);

Does the same thing as glk_date_to_time_utc(), but interprets the broken-out structure as local time.

The glk_date_to_time_local() function may not be smart about Daylight Saving Time conversions.

If implemented with the mktime() libc function, it should use the negative tm_isdst flag to “attempt to divine whether summer time is in effect”.

Parameters

date

A date in the form of a glkdate_t structure.

 

time

An empty glktimeval_t structure to fill in.

 

glk_date_to_simple_time_utc ()

glsi32
glk_date_to_simple_time_utc (glkdate_t *date,
                             glui32 factor);

Convert the broken-out structure (interpreted as universal time) to a timestamp divided by factor . The weekday value in date is ignored. The other values need not be in their normal ranges; they will be normalized.

If the time cannot be represented by the platform's time library, this may return -1.

Parameters

date

A date in the form of a glkdate_t structure.

 

factor

Factor by which to divide the time value.

 

Returns

a timestamp divided by factor , and truncated to 32 bits, or -1 on error.


glk_date_to_simple_time_local ()

glsi32
glk_date_to_simple_time_local (glkdate_t *date,
                               glui32 factor);

Does the same thing as glk_date_to_simple_time_utc(), but interprets the broken-out structure as local time.

Parameters

date

A date in the form of a glkdate_t structure.

 

factor

Factor by which to divide the time value.

 

Returns

a timestamp divided by factor , and truncated to 32 bits, or -1 on error.

Types and Values

glkdate_t

typedef struct {
    glsi32 year;     /* full (four-digit) year */
    glsi32 month;    /* 1-12, 1 is January */
    glsi32 day;      /* 1-31 */
    glsi32 weekday;  /* 0-6, 0 is Sunday */
    glsi32 hour;     /* 0-23 */
    glsi32 minute;   /* 0-59 */
    glsi32 second;   /* 0-59, maybe 60 during a leap second */
    glsi32 microsec; /* 0-999999 */
} glkdate_t;

This structure represents a human-readable date in a specific timezone.

Members

glsi32 year;

The full (four-digit) year

 

glsi32 month;

The month number, ranging from 1-12, 1 is January

 

glsi32 day;

The day of the month, ranging from 1-31

 

glsi32 weekday;

The day of the week, ranging from 0-6, 0 is Sunday

 

glsi32 hour;

The hour of the day, ranging from 0-23

 

glsi32 minute;

The minute of the hour, ranging from 0-59

 

glsi32 second;

The second of the minute, ranging from 0-59; may be 60 during a leap second

 

glsi32 microsec;

The fraction of the second in microseconds, ranging from 0-999999