LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-01-2013, 09:36 AM   #1
kannanrbk
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"`

Output:
02

Correct Result:
03

What went wrong?.

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

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
shivaa
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
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
'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:-
Code:
for day in 2013-03-28 2013-03-29 2013-03-30 2013-03-31 2013-04-01
do
  month_ago=$(date '+%e %b' -d "$day 1 month ago")
  echo "1 month ago on $day is $month_ago"
done

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.
Code:
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.
Quote:
Output:
02

Correct Result:
03
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:14 AM.

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