-
Notifications
You must be signed in to change notification settings - Fork 18
/
arpadate.c
90 lines (77 loc) · 2.75 KB
/
arpadate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* arpadate.c - get_arpadate() is a function returning the date in the
* ARPANET format (see RFC822 and RFC1123)
* Copyright (C) 1998 Hugo Haas
*
* Inspired by smail source code by Ronald S. Karr and Landon Curt Noll
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define ARPADATE_LENGTH 32
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void
get_arpadate (char *d_string)
{
struct tm *date;
#ifdef USE_OLD_ARPADATE
static char *week_day[] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char *month[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"};
static char timezone[3];
time_t current;
int offset, gmt_yday, gmt_hour, gmt_min;
/* Get current time */
(void) time (¤t);
/* Get GMT and then local dates */
date = gmtime ((const time_t *) ¤t);
gmt_yday = date->tm_yday;
gmt_hour = date->tm_hour;
gmt_min = date->tm_min;
date = localtime ((const time_t *) ¤t);
/* Calculates offset */
offset = (date->tm_hour - gmt_hour) * 60 + (date->tm_min - gmt_min);
/* Be careful, there can be problems if the day has changed between the
evaluation of local and gmt's one */
if (date->tm_yday != gmt_yday)
{
if (date->tm_yday == (gmt_yday + 1))
offset += 1440;
else if (date->tm_yday == (gmt_yday - 1))
offset -= 1440;
else
offset += (date->tm_yday > gmt_yday) ? -1440 : 1440;
}
if (offset >= 0)
sprintf (timezone, "+%02d%02d", offset / 60, offset % 60);
else
sprintf (timezone, "-%02d%02d", -offset / 60, -offset % 60);
sprintf (d_string, "%s, %d %s %04d %02d:%02d:%02d %s",
week_day[date->tm_wday],
date->tm_mday, month[date->tm_mon], date->tm_year + 1900,
date->tm_hour, date->tm_min, date->tm_sec, timezone);
#else
time_t now;
/* RFC822 format string borrowed from GNU shellutils date.c */
/* Using %d instead of %_d, the second one isn't portable */
const char *format = "%a, %d %b %Y %H:%M:%S %z";
now = time(NULL);
date = localtime((const time_t *)&now);
(void)strftime(d_string, ARPADATE_LENGTH, format, date);
#endif
}