summaryrefslogtreecommitdiffstats
path: root/bin/date/vary.c
diff options
context:
space:
mode:
authoryar <yar@FreeBSD.org>2004-08-09 13:43:39 +0000
committeryar <yar@FreeBSD.org>2004-08-09 13:43:39 +0000
commitdd0f8a67929deaead706c990da5b6ecb9af261ac (patch)
tree9a797209d384a487f3c1b54df047042e3daaf5a0 /bin/date/vary.c
parentef551a94aceafda3f38cb51670de442924036f0b (diff)
downloadFreeBSD-src-dd0f8a67929deaead706c990da5b6ecb9af261ac.zip
FreeBSD-src-dd0f8a67929deaead706c990da5b6ecb9af261ac.tar.gz
Change the behaviour of `-v' so that, e.g., stepping a month back
on March 31 won't take you to March 2 or 3 (now the result will be the last day of February.) In general, now stepping by months from the last days of the current month A will take you to the very last day of the target month B if B is shorter than A. The previous version would just step to March 31 and rely on mktime(3) to correct the date. Despite its simplicity, such way was counter-intuitive to users and caused pain to shell script writers. Noticed by: Igor Timkin <ivt at gamma dot ru> Approved by: brian MFC after: 2 weeks
Diffstat (limited to 'bin/date/vary.c')
-rw-r--r--bin/date/vary.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/bin/date/vary.c b/bin/date/vary.c
index a314e48..5f01231 100644
--- a/bin/date/vary.c
+++ b/bin/date/vary.c
@@ -148,6 +148,8 @@ adjyear(struct tm *t, char type, int val, int mk)
static int
adjmon(struct tm *t, char type, int val, int istext, int mk)
{
+ int lmdays;
+
if (val < 0)
return 0;
@@ -195,6 +197,11 @@ adjmon(struct tm *t, char type, int val, int istext, int mk)
t->tm_mon = --val;
}
+ /* e.g., -v-1m on March, 31 is the last day of February in common sense */
+ lmdays = daysinmonth(t);
+ if (t->tm_mday > lmdays)
+ t->tm_mday = lmdays;
+
return !mk || domktime(t, type) != -1;
}
OpenPOWER on IntegriCloud