Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Apr 2005 01:15:51 -0800 (PST)
From:      "ALeine" <aleine@austrosearch.net>
To:        ganbold@micom.mng.net
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: subtracting days from localtime problem
Message-ID:  <200504010915.j319Fpwf037974@marlena.vvi.at>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format...

------------=_1112346951-37973-0
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: binary


I attached an example which shows the DST related changes
this year. I just couldn't resist writing something where
I get to use rare values such as 1112345678 and 1131131131
in a meaningful way. :-)

ALeine

___________________________________________________________________
WebMail FREE http://mail.austrosearch.net 
------------=_1112346951-37973-0
Content-Type: text/plain; name="dst_2005.c"
Content-Disposition: inline; filename="dst_2005.c"
Content-Transfer-Encoding: 8bit

/*-
 * Copyright (c) 2005 ALeine <aleine@austrosearch.net>
 * All rights reserved.
 *
 * Imported into CVS repository at exactly 1112345678 seconds
 * since the Epoch.
 *
 * $Id: dst_2005.c,v 1.1.1.1 2005/04/01 08:54:38 aleine Exp $
 *
 */

#include <stdio.h>
#include <time.h>

void get_date_string_x_days_from_time(char *, size_t, int, time_t);
void get_date_string_x_days_before_time(char *, size_t, int, time_t);

int
main(void)
{
	time_t		now;
	int		last_day;
	int		day_offset;
	char		date_string[32];

	now = time(NULL);

	/* Uncomment the following line to see what happens on 01-Apr-2005 */
	/* now = 1112345678; */

	/* Uncomment the following line to see what happens on 04-Nov-2005 */
	/* now = 1131131131; */

	fprintf(stderr, "Reference date and time: %s\n", ctime(&now));

	last_day = 32;
	for (day_offset = 1; day_offset < last_day; day_offset++) {
		get_date_string_x_days_before_time(date_string,
						   sizeof(date_string),
						   day_offset,
						   now);
		printf("%3d day%c before reference date: %s\n",
		       day_offset,
		       (day_offset > 1 ? 's' : ' '),
		       date_string);
	}

	return 0;
}

void 
get_date_string_x_days_before_time(char *date_string,
				   size_t size,
				   int day_offset,
				   time_t time)
{
	get_date_string_x_days_from_time(date_string, size, -day_offset, time);
}

void
get_date_string_x_days_from_time(char *date_string,
				 size_t size,
				 int day_offset,
				 time_t time)
{
	struct tm      *tm_p;

	tm_p = localtime(&time);
	tm_p->tm_mday += day_offset;
	tm_p->tm_hour = tm_p->tm_min = tm_p->tm_sec = 0;

	/* make mktime(3) figure out whether DST is in effect */
	tm_p->tm_isdst = -1;

	time = mktime(tm_p);
	if (time == ((time_t) - 1))
		printf("mktime(3) failed\n");

	snprintf(date_string, size, "%d-%.2d-%.2d [DST: %d]",
		 tm_p->tm_year + 1900,
		 tm_p->tm_mon + 1,
		 tm_p->tm_mday,
		 tm_p->tm_isdst);
}

------------=_1112346951-37973-0--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200504010915.j319Fpwf037974>