]> pd.if.org Git - pdclib/blob - includes/time.h
POSIX interfacing time retrieval
[pdclib] / includes / time.h
1 /* Date and time <time.h>
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #ifndef _PDCLIB_TIME_H
8 #define _PDCLIB_TIME_H _PDCLIB_TIMEH
9
10 #include "_PDCLIB_int.h"
11
12 #ifndef _PDCLIB_SIZE_T_DEFINED
13 #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
14 typedef _PDCLIB_size_t size_t;
15 #endif
16
17 #ifndef _PDCLIB_NULL_DEFINED
18 #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED
19 #define NULL _PDCLIB_NULL
20 #endif
21
22 /* These are defined to be "real types capable of representing types", with
23    "range and precision of times representable in [them being] implementation-
24    defined".
25    As part of struct timespec (see below), time_t is further defined as "a
26    linear count of seconds", with potentially different semantics from a
27    "normal" time_t.
28    For sake of simplicity, we used just that (common) definition of "seconds
29    since epoch" as integer.
30 */
31 typedef _PDCLIB_time_t time_t;
32 typedef _PDCLIB_clock_t clock_t;
33
34 #define CLOCKS_PER_SEC _PDCLIB_CLOCKS_PER_SEC
35 #define TIME_UTC _PDCLIB_TIME_UTC
36
37 struct timespec
38 {
39     time_t tv_sec;
40     long tv_nsec;
41 };
42
43 struct tm
44 {
45     int tm_sec;   /* 0-60 */
46     int tm_min;   /* 0-59 */
47     int tm_hour;  /* 0-23 */
48     int tm_mday;  /* 1-31 */
49     int tm_mon;   /* 0-11 */
50     int tm_year;  /* years since 1900 */
51     int tm_wday;  /* 0-6 */
52     int tm_yday;  /* 0-365 */
53     int tm_isdst; /* >0 DST, 0 no DST, <0 information unavailable */
54 };
55
56 /* Returns the number of "clocks" in processor time since the invocation
57    of the program. Divide by CLOCKS_PER_SEC to get the value in seconds.
58    Returns -1 if the value cannot be represented in the return type or is
59    not available.
60 */
61 clock_t clock( void );
62
63 /* Returns the difference between two calendar times in seconds. */
64 double difftime( time_t time1, time_t time0 );
65
66 /* Normalizes the values in the broken-down time pointed to by timeptr.
67    Returns the calender time specified by the broken-down time.
68 */
69 time_t mktime( struct tm * timeptr );
70
71 /* Returns the current calender time. If timer is not a NULL pointer, stores
72    the current calender time at that address as well.
73 */
74 time_t time( time_t * timer );
75
76 /* Sets the interval pointed to by ts to the current calender time, based
77    on the specified base.
78    Returns base, if successful, otherwise zero.
79 */
80 int timespec_get( struct timespec * ts, int base );
81
82 /* Converts the broken-down time pointed to by timeptr into a string in the
83    form "Sun Sep 16 01:03:52 1973\n\0".
84 */
85 char * asctime( const struct tm * timeptr );
86
87 /* Equivalent to asctime( localtime( timer ) ). */
88 char * ctime( const time_t * timer );
89
90 /* Converts the calender time pointed to by timer into a broken-down time
91    expressed as UTC.
92    Returns a pointer to the broken-down time, or a NULL pointer if it
93    cannot be represented.
94 */
95 struct tm * gmtime( const time_t * timer );
96
97 /* Converts the calender time pointed to by timer into a broken-down time
98    expressed as local time.
99    Returns a pointer to the broken-down time, or a NULL pointer if if
100    cannot be represented.
101 */
102 struct tm * localtime( const time_t * timer );
103
104 /* Writes the broken-down time pointed to by timeptr into the character
105    array pointed to by s. The string pointed to by format controls the
106    exact output. No more than maxsize charactrs will be written.
107    Returns the number of characters written (excluding the terminating
108    null character), or zero on failure.
109 */
110 size_t strftime( char * _PDCLIB_restrict s, size_t maxsize, const char * _PDCLIB_restrict format, const struct tm * _PDCLIB_restrict timeptr );
111
112 #endif