1 /*
2 * Copyright (C) 2004-2008 Christos Tsantilas
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA.
18 */
19
20 #include "common.h"
21 #include "c-icap.h"
22 #include <stdio.h>
23 #include "util.h"
24 #include <time.h>
25
26 static const char *days[] = {
27 "Sun",
28 "Mon",
29 "Tue",
30 "Wed",
31 "Thu",
32 "Fri",
33 "Sat"
34 };
35
36 static const char *months[] = {
37 "Jan",
38 "Feb",
39 "Mar",
40 "Apr",
41 "May",
42 "Jun",
43 "Jul",
44 "Aug",
45 "Sep",
46 "Oct",
47 "Nov",
48 "Dec"
49 };
50
ci_strtime(char * buf)51 void ci_strtime(char *buf)
52 {
53 struct tm br_tm;
54 time_t tm;
55 time(&tm);
56 asctime_r(localtime_r(&tm, &br_tm), buf);
57 buf[STR_TIME_SIZE - 1] = '\0';
58 buf[strlen(buf) - 1] = '\0';
59 }
60
ci_strtime_rfc822(char * buf)61 void ci_strtime_rfc822(char *buf)
62 {
63 struct tm br_tm;
64 time_t tm;
65 time(&tm);
66 gmtime_r(&tm, &br_tm);
67
68 snprintf(buf, STR_TIME_SIZE, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
69 days[br_tm.tm_wday],
70 br_tm.tm_mday,
71 months[br_tm.tm_mon],
72 br_tm.tm_year + 1900, br_tm.tm_hour, br_tm.tm_min, br_tm.tm_sec);
73
74 buf[STR_TIME_SIZE - 1] = '\0';
75 }
76
ci_mktemp_file(char * dir,char * template,char * filename)77 int ci_mktemp_file(char *dir, char *template, char *filename)
78 {
79 snprintf(filename, CI_FILENAME_LEN, "%s%s",dir,template);
80 return mkstemp(filename);
81 }
82
83
ci_usleep(unsigned long usec)84 int ci_usleep(unsigned long usec)
85 {
86 struct timespec us, ur;
87 us.tv_sec = 0;
88 us.tv_nsec = usec * 1000;
89 nanosleep(&us, &ur);
90 return 0;
91 }
92