Used the remaining 2 bits from SilcTime structure.
[silc.git] / lib / silcutil / silctime.h
1 /*
2
3   silctime.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2005 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Time Interface
21  *
22  * DESCRIPTION
23  *
24  * This interface provides various utility functions for getting current
25  * time and converting different time representations into the SilcTime
26  * representation.
27  *
28  ***/
29
30 #ifndef SILCTIME_H
31 #define SILCTIME_H
32
33 /****s* silcutil/SilcTimeAPI/SilcTime
34  *
35  * NAME
36  *
37  *    typedef struct { ... } *SilcTime, SilcTimeStruct;
38  *
39  * DESCRIPTION
40  *
41  *    This context represents time value.  It includes date and time
42  *    down to millisecond precision.
43  *
44  * SOURCE
45  *
46  ***/
47 typedef struct {
48   unsigned int year       : 15;    /* Year,     0 - 32768 */
49   unsigned int month      : 4;     /* Month,    1 - 12 */
50   unsigned int day        : 5;     /* Day,      1 - 31 */
51   unsigned int hour       : 5;     /* Hour,     0 - 23 */
52   unsigned int minute     : 6;     /* Minute,   0 - 59 */
53   unsigned int second     : 6;     /* Second,   0 - 61 */
54   unsigned int msecond    : 10;    /* Millisec, 0 - 1000 */
55   unsigned int utc_hour   : 5;     /* Offset to Zulu (UTC) hours */
56   unsigned int utc_minute : 6;     /* Offset to Zulu (UTC) minutes */
57   unsigned int utc_east   : 1;     /* Offset, 1 east (+), 0 west (-) */
58   unsigned int dst        : 1;     /* Set if daylight saving time */
59 } *SilcTime, SilcTimeStruct;
60 /***/
61
62 /****f* silcutil/SilcTimeAPI/silc_time
63  *
64  * SYNOPSIS
65  *
66  *    SilcInt64 silc_time(void);
67  *
68  * DESCRIPTION
69  *
70  *    Returns the current time of the system since Epoch.  The returned
71  *    value is seconds since Epoch (1.1.1970).  Returns -1 on error.
72  *
73  ***/
74 SilcInt64 silc_time(void);
75
76 /****f* silcutil/SilcTimeAPI/silc_time_string
77  *
78  * SYNOPSIS
79  *
80  *    const char *silc_time_string(SilcInt64 timeval);
81  *
82  * DESCRIPTION
83  *
84  *    Returns time and date as string.  The caller must not free the string
85  *    and next call to this function will delete the old string.  If the
86  *    `timeval' is zero (0) returns current time as string, otherwise the
87  *    `timeval' as string.  Returns NULL on error.
88  *
89  ***/
90 const char *silc_time_string(SilcInt64 timeval);
91
92 /****f* silcutil/SilcTimeAPI/silc_time_value
93  *
94  * SYNOPSIS
95  *
96  *   SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
97  *
98  * DESCRIPTION
99  *
100  *    Returns time and date as SilcTime.  If the `timeval' is zero (0)
101  *    returns current time as SilcTime, otherwise the `timeval' as SilcTime.
102  *    Returns FALSE on error, TRUE otherwise.
103  *
104  ***/
105 SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
106
107 /****f* silcutil/SilcTimeAPI/silc_time_universal
108  *
109  * SYNOPSIS
110  *
111  *    SilcBool silc_time_universal(const char *universal_time,
112  *                                 SilcTime ret_time);
113  *
114  * DESCRIPTION
115  *
116  *    Returns time and date as SilcTime from `universal_time' string which
117  *    format is "YYMMDDhhmmssZ", where YY is year, MM is month, DD is day,
118  *    hh is hour, mm is minutes, ss is seconds and Z is timezone, which
119  *    by default is Zulu (UTC).  Universal time is defined in ISO/EIC 8824-1.
120  *
121  *    Returns FALSE on error, TRUE otherwise.
122  *
123  * EXAMPLE
124  *
125  *    SilcTimeStruct ret_time;
126  *
127  *    time is 03/02/19 19:04:03 Zulu (UTC)
128  *    silc_time_universal("030219190403Z", &ret_time);
129  *
130  ***/
131 SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
132
133 /****f* silcutil/SilcTimeAPI/silc_time_universal_string
134  *
135  * SYNOPSIS
136  *
137  *    SilcBool silc_time_universal_string(SilcTime timeval, char *ret_string,
138  *                                        SilcUInt32 ret_string_size);
139  *
140  * DESCRIPTION
141  *
142  *    Encodes the SilcTime `time' into the universal time format into the
143  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
144  *
145  ***/
146 SilcBool silc_time_universal_string(SilcTime timeval, char *ret_string,
147                                     SilcUInt32 ret_string_size);
148
149 /****f* silcutil/SilcTimeAPI/silc_time_generalized
150  *
151  * SYNOPSIS
152  *
153  *    SilcBool silc_time_generalized(const char *generalized_time,
154  *                                   SilcTime ret_time);
155  *
156  * DESCRIPTION
157  *
158  *    Returns time and date as SilcTime from `generalized_time' string which
159  *    format is "YYYYMMDDhhmmss.ppZ", where YYYY is year, MM is month, DD
160  *    is day, hh is hour, mm is minutes, ss is seconds which may have optional
161  *    precision pp, and Z is timezone, which by default is Zulu (UTC).
162  *    Generalized time is defined in ISO/EIC 8824-1.
163  *
164  *    Returns FALSE on error, TRUE otherwise.
165  *
166  * EXAMPLE
167  *
168  *    SilcTimeStruct ret_time;
169  *
170  *    time is 2003/02/19 19:04:03 Zulu (UTC)
171  *    silc_time_generalized("20030219190403Z", &ret_time);
172  *
173  *    time is 2003/02/19 19:05:10.212 Zulu (UTC)
174  *    silc_time_generalized("20030219190510.212Z", &ret_time);
175  *
176  ***/
177 SilcBool
178 silc_time_generalized(const char *generalized_time, SilcTime ret_time);
179
180 /****f* silcutil/SilcTimeAPI/silc_time_generalized_string
181  *
182  * SYNOPSIS
183  *
184  *    SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
185  *                                          SilcUInt32 ret_string_size);
186  *
187  * DESCRIPTION
188  *
189  *    Encodes the SilcTime `time' into the generalized time format into the
190  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
191  *
192  ***/
193 SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
194                                       SilcUInt32 ret_string_size);
195
196 #endif /* SILCTIME_H */