Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-01-2013, 09:36 AM   #1
LQ Newbie
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Date Command Wrong Result.

I am running a monthly cron job. Whenever cron runs , I will got notified. But , this month I got notified earlier 1 hour (Day / Light Saving).

My TimeZone : America/Los Angeles

Command Used: `date +%m --date="1 month ago"`


Correct Result:

What went wrong?.

Is there any problem in date command due to day / light savings?
Old 04-01-2013, 09:51 AM   #2
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 96

Rep: Reputation: 24
At what time is your cronjob running?
At what time does daylightsavingtime start?

Output of this:
date +%d-%m-%Y --date="1 month ago 1 day ago"
correctly says:
28-02-2013 (Because it's 01-04-2013 now)
Old 04-01-2013, 12:34 PM   #3
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As far as I am concerned, cron will run according to the result of "date" command.

Let's know what's your distro and what's crontab entry.
Old 04-01-2013, 12:52 PM   #4
Registered: Aug 2002
Posts: 16,122

Rep: Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866
Welcome to LinuxQuestions.

What distribution / version are you running? Your tzdata might be incorrect or your hardware / system clock settings might be misconfigured.

Post the output of the following command:

zdump -v /etc/localtime | grep 2013
Old 04-02-2013, 10:57 PM   #5
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
'1 month ago' gives unexpected results when the previous month has fewer days than the current month.
For March 1-28 '1 month ago' is February but
for March 29-31 '1 month ago' is March.

Using GNU coreutils date:-
for day in 2013-03-28 2013-03-29 2013-03-30 2013-03-31 2013-04-01
  month_ago=$(date '+%e %b' -d "$day 1 month ago")
  echo "1 month ago on $day is $month_ago"

1 month ago on 2013-03-28 is 28 Feb # As expected
1 month ago on 2013-03-29 is  1 Mar # There isn't a 29 Feb
1 month ago on 2013-03-30 is  2 Mar # There isn't a 30 Feb
1 month ago on 2013-03-31 is  3 Mar # There isn't a 31 Feb
1 month ago on 2013-04-01 is  1 Mar # As expected
A work-around is to calculate the previous month from first day of the current month instead of from today.
first_day=$(date '+%Y-%m-01')                 # e.g. "2013-04-01"
last_month=$(date '+%m' -d "$first_day 1 month ago")  # e.g. "03"
However the 'Output'/'Correct Result' stated in post #1 are the wrong way round for this to be the cause of the original poster's problem.

Correct Result:


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sorting find result by date without printf nadigo Programming 8 04-26-2012 02:44 PM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM
DNS test result. What's wrong? frankpretec Linux - Networking 3 04-05-2005 07:21 PM
df -h wrong result ? csrh Linux - General 2 03-08-2005 08:35 AM
df reports wrong result for Win partition sharathkv1 Fedora 2 09-03-2004 06:24 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration