File: rfcdate.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (73 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (12)
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
#include <stdio.h>
#include <time.h>

static char *wdays[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
static char *months[]={"Jan","Feb","Mar","Apr","May","Jun",
		"Jul","Aug","Sep","Oct","Nov","Dec"};
char *gmtoffset(time_t);


char *rfcdate(now)
time_t now;
{
	static char buf[40];
	struct tm ptm;

	if (!now) time(&now);
	ptm=*localtime(&now);

	sprintf(buf,"%s, %02d %s %d %02d:%02d:%02d %s",
		wdays[ptm.tm_wday],ptm.tm_mday,months[ptm.tm_mon],
		1900+ptm.tm_year,ptm.tm_hour,ptm.tm_min,ptm.tm_sec,
		gmtoffset(now));
	return(buf);
}

char *gmtoffset(now)
time_t now;
{
	static char buf[6]="+0000";
	struct tm ptm;
#if defined(DONT_HAVE_TM_GMTOFF)
	struct tm gtm;
#endif
	char sign;
	int hr,min;
	long offset;

	if (!now) time(&now);
	ptm=*localtime(&now);

#if defined(DONT_HAVE_TM_GMTOFF)
	/* To get the timezone, compare localtime with GMT. */
	gtm=*gmtime(&now);

	/* Assume we are never more than 24 hours away. */
	offset = gtm.tm_yday - ptm.tm_yday;
	if (offset > 1)
		offset = -24;
	else if (offset < -1)
		offset = 24;
	else
		offset *= 24;

	/* Scale in the hours and minutes; ignore seconds. */
	offset += gtm.tm_hour - ptm.tm_hour;
	offset *= 60;
	offset += gtm.tm_min - ptm.tm_min;
#else
	offset=-ptm.tm_gmtoff/60L;
#endif

	if (offset <= 0)
	{
		sign='+';
		offset=-offset;
	}
	else sign='-';
	hr=offset/60L;
	min=offset%60L;

	sprintf(buf,"%c%02d%02d",sign,hr,min);
	return(buf);
}