1205821Sedwin/*-
213840Swosch * Copyright (c) 1989, 1993, 1994
313840Swosch *	The Regents of the University of California.  All rights reserved.
413840Swosch *
513840Swosch * Redistribution and use in source and binary forms, with or without
613840Swosch * modification, are permitted provided that the following conditions
713840Swosch * are met:
813840Swosch * 1. Redistributions of source code must retain the above copyright
913840Swosch *    notice, this list of conditions and the following disclaimer.
1013840Swosch * 2. Redistributions in binary form must reproduce the above copyright
1113840Swosch *    notice, this list of conditions and the following disclaimer in the
1213840Swosch *    documentation and/or other materials provided with the distribution.
1313840Swosch * 4. Neither the name of the University nor the names of its contributors
1413840Swosch *    may be used to endorse or promote products derived from this software
1513840Swosch *    without specific prior written permission.
1613840Swosch *
1713840Swosch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1813840Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1913840Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2013840Swosch * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2113840Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213840Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313840Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413840Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2513840Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2613840Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2713840Swosch * SUCH DAMAGE.
2813840Swosch */
2913840Swosch
3087235Smarkm#include <sys/cdefs.h>
3187235Smarkm__FBSDID("$FreeBSD$");
3287235Smarkm
3387235Smarkm#include <err.h>
3415714Sache#include <locale.h>
3513840Swosch#include <stdio.h>
3687235Smarkm#include <stdlib.h>
3715714Sache#include <string.h>
3813840Swosch#include <time.h>
3913840Swosch
4013840Swosch#include "calendar.h"
4113840Swosch
42241737Sedstatic time_t		time1, time2;
43205821Sedwinconst struct tm		tm0;
44205821Sedwinchar			dayname[100];
45205821Sedwinint			year1, year2;
4613840Swosch
4713840Swosch
48169343Sdwmalonevoid
49205821Sedwinsettimes(time_t now, int before, int after, int friday, struct tm *tp1, struct tm *tp2)
5015714Sache{
51205821Sedwin	char *oldl, *lbufp;
52205821Sedwin	struct tm tp;
5313840Swosch
54205821Sedwin	localtime_r(&now, &tp);
5515714Sache
56205821Sedwin	/* Friday displays Monday's events */
57205821Sedwin	if (after == 0 && before == 0 && friday != -1)
58205821Sedwin		after = tp.tm_wday == friday ? 3 : 1;
5915714Sache
60205821Sedwin	time1 = now - SECSPERDAY * before;
61205821Sedwin	localtime_r(&time1, tp1);
62205821Sedwin	year1 = 1900 + tp1->tm_year;
63205821Sedwin	time2 = now + SECSPERDAY * after;
64205821Sedwin	localtime_r(&time2, tp2);
65205821Sedwin	year2 = 1900 + tp2->tm_year;
6615714Sache
67205821Sedwin	strftime(dayname, sizeof(dayname) - 1, "%A, %d %B %Y", tp1);
6815714Sache
6992006Sache	oldl = NULL;
7092006Sache	lbufp = setlocale(LC_TIME, NULL);
7192006Sache	if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL)
7292006Sache		errx(1, "cannot allocate memory");
73181322Sedwin	(void)setlocale(LC_TIME, "C");
74181322Sedwin	(void)setlocale(LC_TIME, (oldl != NULL ? oldl : ""));
7592006Sache	if (oldl != NULL)
7692006Sache		free(oldl);
7715714Sache
7815714Sache	setnnames();
7913840Swosch}
8013840Swosch
8113840Swosch/* convert Day[/Month][/Year] into unix time (since 1970)
8215066Smpp * Day: two digits, Month: two digits, Year: digits
8313840Swosch */
84169343Sdwmalonetime_t
85181322SedwinMktime(char *dp)
8621615Smpp{
87181322Sedwin	time_t t;
88181322Sedwin	int d, m, y;
89205821Sedwin	struct tm tm, tp;
9013840Swosch
91181322Sedwin	(void)time(&t);
92205821Sedwin	localtime_r(&t, &tp);
9349053Sn_hibma
94181322Sedwin	tm = tm0;
95205821Sedwin	tm.tm_mday = tp.tm_mday;
96205821Sedwin	tm.tm_mon = tp.tm_mon;
97205821Sedwin	tm.tm_year = tp.tm_year;
9813840Swosch
99181322Sedwin	switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) {
100181322Sedwin	case 3:
101181322Sedwin		if (y > 1900)
102181322Sedwin			y -= 1900;
103181322Sedwin		tm.tm_year = y;
104181322Sedwin		/* FALLTHROUGH */
105181322Sedwin	case 2:
106181322Sedwin		tm.tm_mon = m - 1;
107181322Sedwin		/* FALLTHROUGH */
108181322Sedwin	case 1:
109181322Sedwin		tm.tm_mday = d;
110181322Sedwin	}
11113840Swosch
11249053Sn_hibma#ifdef DEBUG
113181322Sedwin	fprintf(stderr, "Mktime: %d %d %s\n",
114181322Sedwin	    (int)mktime(&tm), (int)t, asctime(&tm));
11513840Swosch#endif
116181322Sedwin	return (mktime(&tm));
11713840Swosch}
118