From owner-freebsd-hackers@FreeBSD.ORG Fri Apr 1 09:15:47 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0829D16A4CE for ; Fri, 1 Apr 2005 09:15:47 +0000 (GMT) Received: from marlena.vvi.at (marlena.vvi.at [208.252.225.59]) by mx1.FreeBSD.org (Postfix) with ESMTP id AA8AC43D46 for ; Fri, 1 Apr 2005 09:15:46 +0000 (GMT) (envelope-from www@marlena.vvi.at) Received: from marlena.vvi.at (localhost.marlena.vvi.at [127.0.0.1]) by marlena.vvi.at (8.12.10/8.12.9) with ESMTP id j319Fwh3037975; Fri, 1 Apr 2005 01:16:00 -0800 (PST) (envelope-from www@marlena.vvi.at) Received: (from www@localhost) by marlena.vvi.at (8.12.10/8.12.10/Submit) id j319Fpwf037974; Fri, 1 Apr 2005 01:15:51 -0800 (PST) (envelope-from www) Date: Fri, 1 Apr 2005 01:15:51 -0800 (PST) Message-Id: <200504010915.j319Fpwf037974@marlena.vvi.at> Content-Type: multipart/mixed; boundary="----------=_1112346951-37973-0" Content-Transfer-Encoding: binary MIME-Version: 1.0 X-Mailer: MIME-tools 5.411 (Entity 5.404) From: "ALeine" To: ganbold@micom.mng.net cc: freebsd-hackers@freebsd.org Subject: Re: subtracting days from localtime problem X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Apr 2005 09:15:47 -0000 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 * 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 #include 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--