From unknown Sat Aug 16 18:44:41 2025 X-Loop: help-debbugs@gnu.org Subject: bug#13071: Date and month ago Resent-From: Tuc at T-B-O-H Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-coreutils@gnu.org Resent-Date: Mon, 03 Dec 2012 20:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 13071 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: 13071@debbugs.gnu.org X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.13545672342177 (code B ref -1); Mon, 03 Dec 2012 20:41:02 +0000 Received: (at submit) by debbugs.gnu.org; 3 Dec 2012 20:40:34 +0000 Received: from localhost ([127.0.0.1]:51961 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1Tfcob-0000Yy-33 for submit@debbugs.gnu.org; Mon, 03 Dec 2012 15:40:34 -0500 Received: from eggs.gnu.org ([208.118.235.92]:52298) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1TfcgP-0000N1-10 for submit@debbugs.gnu.org; Mon, 03 Dec 2012 15:32:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tfcdz-0002J8-6D for submit@debbugs.gnu.org; Mon, 03 Dec 2012 15:29:40 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.2 Received: from lists.gnu.org ([208.118.235.17]:49156) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tfcdz-0002J3-3v for submit@debbugs.gnu.org; Mon, 03 Dec 2012 15:29:35 -0500 Received: from eggs.gnu.org ([208.118.235.92]:33389) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tfcdt-0004iH-Dk for bug-coreutils@gnu.org; Mon, 03 Dec 2012 15:29:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tfcdn-00029O-O8 for bug-coreutils@gnu.org; Mon, 03 Dec 2012 15:29:29 -0500 Received: from vjofn.tucs-beachin-obx-house.com ([204.107.90.128]:1707) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tfcdn-00021z-Ji for bug-coreutils@gnu.org; Mon, 03 Dec 2012 15:29:23 -0500 Received: (from tbohml@localhost) by vjofn.tucs-beachin-obx-house.com (8.14.2/8.14.2) id qB3Jr5U5072894; Mon, 3 Dec 2012 14:53:05 -0500 (EST) From: Tuc at T-B-O-H Message-Id: <201212031953.qB3Jr5U5072894@vjofn.tucs-beachin-obx-house.com> Date: Mon, 3 Dec 2012 14:53:05 -0500 (EST) X-Mailer: ELM [version 2.5 PL6] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 208.118.235.17 X-Spam-Score: -4.2 (----) X-Mailman-Approved-At: Mon, 03 Dec 2012 15:40:31 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -4.2 (----) Hi, Running into a problem with date and month... ------------------------------------------------------------------- #!/bin/bash function example { rptdate=$(date +%Y-%m-%d -d "$1 -6 months") echo "6 months ago from $1 is ${rptdate}" } example 2012-09-30 example 2012-10-31 example 2012-10-30 example 2012-11-30 example 2012-12-01 example 2012-03-31 ------------------------------------------------------------------- 6 months ago from 2012-09-30 is 2012-03-30 ****6 months ago from 2012-10-31 is 2012-05-01*** 6 months ago from 2012-10-30 is 2012-04-30 6 months ago from 2012-11-30 is 2012-05-30 6 months ago from 2012-12-01 is 2012-06-01 *****6 months ago from 2012-03-31 is 2011-10-01* It seems that its running into the issue that if the 6 months ago doesn't have the same amount of days, its falling into the next month. Is there a way to tell it to instead just use the last day of the month? Thanks, Tuc From unknown Sat Aug 16 18:44:41 2025 X-Loop: help-debbugs@gnu.org Subject: bug#13071: Date and month ago Resent-From: Bob Proulx Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-coreutils@gnu.org Resent-Date: Mon, 03 Dec 2012 23:39:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 13071 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: Tuc at T-B-O-H Cc: 13071@debbugs.gnu.org Received: via spool by 13071-submit@debbugs.gnu.org id=B13071.135457789117850 (code B ref 13071); Mon, 03 Dec 2012 23:39:01 +0000 Received: (at 13071) by debbugs.gnu.org; 3 Dec 2012 23:38:11 +0000 Received: from localhost ([127.0.0.1]:52097 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1TffaV-0004dq-03 for submit@debbugs.gnu.org; Mon, 03 Dec 2012 18:38:11 -0500 Received: from joseki.proulx.com ([216.17.153.58]:60233) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1TffaR-0004de-RU; Mon, 03 Dec 2012 18:38:09 -0500 Received: from hysteria.proulx.com (hysteria.proulx.com [192.168.230.119]) by joseki.proulx.com (Postfix) with ESMTP id D216A211D6; Mon, 3 Dec 2012 16:35:41 -0700 (MST) Received: by hysteria.proulx.com (Postfix, from userid 1000) id 7F6C42DCD7; Mon, 3 Dec 2012 16:35:41 -0700 (MST) Date: Mon, 3 Dec 2012 16:35:41 -0700 From: Bob Proulx Message-ID: <20121203233541.GA31070@hysteria.proulx.com> References: <201212031953.qB3Jr5U5072894@vjofn.tucs-beachin-obx-house.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201212031953.qB3Jr5U5072894@vjofn.tucs-beachin-obx-house.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Score: 0.8 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: 0.8 (/) tag 13071 + notabug moreinfo close 13071 thanks Tuc at T-B-O-H wrote: > Hi, Hi! :-) > Running into a problem with date and month... Since you aren't really submitting a bug report but just asking questions I am going to go ahead here and mark the accounting of the bug as closed. If a bug report arrises out of this feel free to open it again. Also, in the future, if you have general discussion please bring that up on the coreutils@gnu.org mailing list instead of the bug tracker. We don't need to track discussion like a bug. Thanks. > function example { As a random side note I always suggest the "example() {" syntax. > rptdate=$(date +%Y-%m-%d -d "$1 -6 months") Just as a shortcut the GNU extension %F is the same as %Y-%m-%d. Same thing. No difference. But easier to remember and saves some typing. > echo "6 months ago from $1 is ${rptdate}" > > example 2012-09-30 > example 2012-10-31 > example 2012-10-30 > example 2012-11-30 > example 2012-12-01 > example 2012-03-31 Ew! You didn't specify the time and/or didn't use UTC. So the default will be midnight in the local time. Depending upon the timezone that is going to hit DST changes and trip over "illegal" times. (One of the few times where it isn't a "legal" time, as opposed to valid because timezones is by act of congress!) > 6 months ago from 2012-09-30 is 2012-03-30 > ****6 months ago from 2012-10-31 is 2012-05-01*** > 6 months ago from 2012-10-30 is 2012-04-30 > 6 months ago from 2012-11-30 is 2012-05-30 > 6 months ago from 2012-12-01 is 2012-06-01 > *****6 months ago from 2012-03-31 is 2011-10-01* Right. > It seems that its running into the issue that if the 6 months ago doesn't > have the same amount of days, its falling into the next month. Is there a > way to tell it to instead just use the last day of the month? Yes. See the documentation about relative dates. It says: 28.7 Relative items in date strings The unit of time displacement may be selected by the string `year' or `month' for moving by whole years or months. These are fuzzy units, as years and months are not all of equal duration. More precise units are `fortnight' which is worth 14 days, `week' worth 7 days, `day' worth 24 hours, `hour' worth 60 minutes, `minute' or `min' worth 60 seconds, and `second' or `sec' worth one second. An `s' suffix on these units is accepted and ignored. The unit of time may be preceded by a multiplier, given as an optionally signed number. Unsigned numbers are taken as positively signed. No number at all implies 1 for a multiplier. Following a relative item by the string `ago' is equivalent to preceding the unit by a multiplier with value -1. The string `tomorrow' is worth one day in the future (equivalent to `day'), the string `yesterday' is worth one day in the past (equivalent to `day ago'). The strings `now' or `today' are relative items corresponding to zero-valued time displacement, these strings come from the fact a zero-valued time displacement represents the current time when not otherwise changed by previous items. They may be used to stress other items, like in `12:00 today'. The string `this' also has the meaning of a zero-valued time displacement, but is preferred in date strings like `this thursday'. When a relative item causes the resulting date to cross a boundary where the clocks were adjusted, typically for daylight saving time, the resulting date and time are adjusted accordingly. The fuzz in units can cause problems with relative items. For example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example: $ date -R Thu, 31 Jul 2003 13:02:39 -0700 $ date --date='-1 month' +'Last month was %B?' Last month was July? $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' Last month was June! Also, take care when manipulating dates around clock changes such as daylight saving leaps. In a few cases these have added or subtracted as much as 24 hours from the clock, so it is often wise to adopt universal time by setting the `TZ' environment variable to `UTC0' before embarking on calendrical calculations. And also see the FAQ entry which also discusses this topic in some detail: http://www.gnu.org/software/coreutils/faq/#The-date-command-is-not-working-right_002e I am sure those docs answer your questions. But just the same we would appreciate a note from you saying that it did. Or suggesting improvements! :-) Bob