1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr_portable.h"
18#include "apr_time.h"
19#include "apr_lib.h"
20#include "apr_private.h"
21/* System Headers required for time library */
22#if APR_HAVE_SYS_TIME_H
23#include <sys/time.h>
24#endif
25#ifdef HAVE_TIME_H
26#include <time.h>
27#endif
28#if APR_HAVE_STRING_H
29#include <string.h>
30#endif
31/* End System Headers */
32
33APR_DECLARE_DATA const char apr_month_snames[12][4] =
34{
35    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36};
37APR_DECLARE_DATA const char apr_day_snames[7][4] =
38{
39    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
40};
41
42apr_status_t apr_rfc822_date(char *date_str, apr_time_t t)
43{
44    apr_time_exp_t xt;
45    const char *s;
46    int real_year;
47
48    apr_time_exp_gmt(&xt, t);
49
50    /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
51    /*           12345678901234567890123456789  */
52
53    s = &apr_day_snames[xt.tm_wday][0];
54    *date_str++ = *s++;
55    *date_str++ = *s++;
56    *date_str++ = *s++;
57    *date_str++ = ',';
58    *date_str++ = ' ';
59    *date_str++ = xt.tm_mday / 10 + '0';
60    *date_str++ = xt.tm_mday % 10 + '0';
61    *date_str++ = ' ';
62    s = &apr_month_snames[xt.tm_mon][0];
63    *date_str++ = *s++;
64    *date_str++ = *s++;
65    *date_str++ = *s++;
66    *date_str++ = ' ';
67    real_year = 1900 + xt.tm_year;
68    /* This routine isn't y10k ready. */
69    *date_str++ = real_year / 1000 + '0';
70    *date_str++ = real_year % 1000 / 100 + '0';
71    *date_str++ = real_year % 100 / 10 + '0';
72    *date_str++ = real_year % 10 + '0';
73    *date_str++ = ' ';
74    *date_str++ = xt.tm_hour / 10 + '0';
75    *date_str++ = xt.tm_hour % 10 + '0';
76    *date_str++ = ':';
77    *date_str++ = xt.tm_min / 10 + '0';
78    *date_str++ = xt.tm_min % 10 + '0';
79    *date_str++ = ':';
80    *date_str++ = xt.tm_sec / 10 + '0';
81    *date_str++ = xt.tm_sec % 10 + '0';
82    *date_str++ = ' ';
83    *date_str++ = 'G';
84    *date_str++ = 'M';
85    *date_str++ = 'T';
86    *date_str++ = 0;
87    return APR_SUCCESS;
88}
89
90apr_status_t apr_ctime(char *date_str, apr_time_t t)
91{
92    apr_time_exp_t xt;
93    const char *s;
94    int real_year;
95
96    /* example: "Wed Jun 30 21:49:08 1993" */
97    /*           123456789012345678901234  */
98
99    apr_time_exp_lt(&xt, t);
100    s = &apr_day_snames[xt.tm_wday][0];
101    *date_str++ = *s++;
102    *date_str++ = *s++;
103    *date_str++ = *s++;
104    *date_str++ = ' ';
105    s = &apr_month_snames[xt.tm_mon][0];
106    *date_str++ = *s++;
107    *date_str++ = *s++;
108    *date_str++ = *s++;
109    *date_str++ = ' ';
110    *date_str++ = xt.tm_mday / 10 + '0';
111    *date_str++ = xt.tm_mday % 10 + '0';
112    *date_str++ = ' ';
113    *date_str++ = xt.tm_hour / 10 + '0';
114    *date_str++ = xt.tm_hour % 10 + '0';
115    *date_str++ = ':';
116    *date_str++ = xt.tm_min / 10 + '0';
117    *date_str++ = xt.tm_min % 10 + '0';
118    *date_str++ = ':';
119    *date_str++ = xt.tm_sec / 10 + '0';
120    *date_str++ = xt.tm_sec % 10 + '0';
121    *date_str++ = ' ';
122    real_year = 1900 + xt.tm_year;
123    *date_str++ = real_year / 1000 + '0';
124    *date_str++ = real_year % 1000 / 100 + '0';
125    *date_str++ = real_year % 100 / 10 + '0';
126    *date_str++ = real_year % 10 + '0';
127    *date_str++ = 0;
128
129    return APR_SUCCESS;
130}
131
132apr_status_t apr_strftime(char *s, apr_size_t *retsize, apr_size_t max,
133                        const char *format, apr_time_exp_t *xt)
134{
135    struct tm tm;
136    memset(&tm, 0, sizeof tm);
137    tm.tm_sec  = xt->tm_sec;
138    tm.tm_min  = xt->tm_min;
139    tm.tm_hour = xt->tm_hour;
140    tm.tm_mday = xt->tm_mday;
141    tm.tm_mon  = xt->tm_mon;
142    tm.tm_year = xt->tm_year;
143    tm.tm_wday = xt->tm_wday;
144    tm.tm_yday = xt->tm_yday;
145    tm.tm_isdst = xt->tm_isdst;
146#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
147    tm.tm_gmtoff = xt->tm_gmtoff;
148#elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
149    tm.__tm_gmtoff = xt->tm_gmtoff;
150#endif
151    (*retsize) = strftime(s, max, format, &tm);
152    return APR_SUCCESS;
153}
154