Computing.Net > Forums > Unix > Calculation the date

Calculation the date

Reply to Message Icon

Original Message
Name: fdipaolo
Date: May 4, 2004 at 01:32:10 Pacific
Subject: Calculation the date
OS: unix
CPU/Ram: compaq
Comment:

I have to calculate the date - 1. I use this shell:
#!/bin/sh
DataFlusso='date +"%Y%m%d"'
DataFlusso='expr $DataFlusso - 1'
but if the date is 20040501 return 20040500.
Someone can help me?
Thanks!!


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: May 4, 2004 at 09:21:42 Pacific
Subject: Calculation the date
Reply: (edit)

You can get yesterday's date with:

TZ=aaa24 date +%Y%m%d

Or if you are using GNU date, you can do:

date --date=yesterday


Report Offensive Follow Up For Removal

Response Number 2
Name: camin
Date: May 4, 2004 at 10:43:39 Pacific
Subject: Calculation the date
Reply: (edit)

Hi Fdipaolo,

I am having the same date problem, I need to find all files with a date greater than or equal to Jan 1, 2004.

I do not know how to go back to jan 1, 2004, can anyone help....! please!

camin


Report Offensive Follow Up For Removal

Response Number 3
Name: fdipaolo
Date: May 5, 2004 at 05:15:57 Pacific
Subject: Calculation the date
Reply: (edit)

Thanks!!! I try to use TZ=aaa24 date +%Y%m%d
and now works!!!


Report Offensive Follow Up For Removal

Response Number 4
Name: fdipaolo
Date: May 5, 2004 at 08:07:39 Pacific
Subject: Calculation the date
Reply: (edit)

I have an other question: TZ is the date a local zone. But I have to put the return value of TZ=aaa24 date +%Y%m%d
into a variable DataFlusso. But if I make the assignment
DataFlusso = $TZ
DataFlusso is blank. Why?


Report Offensive Follow Up For Removal

Response Number 5
Name: thepubba
Date: May 5, 2004 at 08:10:46 Pacific
Subject: Calculation the date
Reply: (edit)

Two functions I wrote long ago that play around with dates for systems the do not have the GNU date command. Feel free to modify them and incorporate them into your scripts.

######################
function leapCheck { #
######################
# Comments: This function was written to determine if Feb ends on the 28th or
# on the 29th. It was part of the 24 hour review of errors from the 'errpt'
# command. It is not being implemented at this time but the function was left
# in for future implementation.
#######################

for LastDay in $(cal 02 $(date "+%Y") )
do
:
done
export LastDay

} # end of the function leapCheck.

This function calculates the dates:

#########################
function getStartTime { #
#########################
# Comments: Function orginally written to allow a search back 24 hours in the
# error log. Function is not implemented but is being left in for possible
# future implementation.
#########################

Month=$(date "+%m")


if [[ $(date "+%d") = 1 ]]
then
if [[ $(date "+%m") = 1 ]]
then
(( Year=$(date "+%y") - 1 ))
Month=12
Day=31
else
(( Month=$Month - 1 ))
case $Month in

03 | 06 | 09 | 11 ) LastDay=30
;;
02 ) :
;;
* ) LastDay=31
;;
esac
Year=$(date "+%y")
(( Month=$(date "+%m") - 1 ))
Day=$LastDay
fi
else
Year=$(date "+%y")
(( Day=$(date "+%d") -1 ))
fi

StartTime=$Month$Day$(date "+%H%M")$Year


} # end of the getStartTime function


Report Offensive Follow Up For Removal


Response Number 6
Name: Jim Boothe
Date: May 5, 2004 at 08:18:17 Pacific
Subject: Calculation the date
Reply: (edit)

Actually camin, your problem is nothing like fdipaolo's, thus it should really start a new thread.

fdipaolo wanted simply to get the date from the system and subtract one day from it.  You want to find files based on a given date.

Here is a simple solution, but it comes with a slight flaw:

touch -t200401010000 /tmp/Jan01
find /mypath -type f -newer /tmp/Jan01

The above will find files newer than the control file that I created named Jan01, and which has a modify date of Jan 01 00:00.

The flaw is that the touch command allows me to specify my desired date stamp only to the nearest minute (00:00) and it defaults to 00:00:00. Since the find command is looking for files newer than this, it will not select files that have a date stamp of exactly Jan 01 00:00:00.  It will
select files having a date stamp of Jan 01 00:00:01 and higher.

The find command will find files that are "newer than" or "not newer than", when what we really need here is "not older than", which would be equivalent to "equal to or newer than".

To avoid missing that one-second window, we could instead do this:

touch -t200312312359 /tmp/Dec31
find /mydirtree -type f -newer /tmp/Dec31

That solution will select files that are newer than Dec 31 23:59:00 which means that it would also select files in the 23:59:01 to 23:59:59 range.  So instead of missing files in a one-second window, this solution will give us files in a 59-second window that are too old.

I am not good at perl, but I do know that this would be an easy task for perl. There may be a straight-forward way to code it, but if not, I know that perl could pull each file's mtime (number of seconds since Jan 1, 1970):

  @file_info = stat("myfile");
  print $file_info[9],"\n";

The perl script would want to select files having mtime seconds of 1072936800 or greater, which is exactly Jan 01, 2004 00:00:00.

Maybe someone will post the perl solution.


Report Offensive Follow Up For Removal

Response Number 7
Name: camin
Date: May 5, 2004 at 09:20:53 Pacific
Subject: Calculation the date
Reply: (edit)

A million thanks Jim B.

I will try your suggestion....will let you know.

camin


Report Offensive Follow Up For Removal

Response Number 8
Name: camin
Date: May 5, 2004 at 11:06:17 Pacific
Subject: Calculation the date
Reply: (edit)

Hi Jim,

I tried using [touch -t200312312359 /tmp/Dec31 ]

but I get error: bad time specification

thanks for all your help..I do not know perl. Any other suggestions would be great...
camin


Report Offensive Follow Up For Removal

Response Number 9
Name: Jim Boothe
Date: May 5, 2004 at 11:25:44 Pacific
Subject: Calculation the date
Reply: (edit)

camin, what operating system are you using, and what shell?

uname -a
echo $SHELL


Report Offensive Follow Up For Removal

Response Number 10
Name: camin
Date: May 6, 2004 at 05:41:24 Pacific
Subject: Calculation the date
Reply: (edit)

Hi Jim,

I am using SunOS 5.6 Generic_105181-38 sun4u sparc SUNW,Ultra-60

SHELL=/bin/csh

thanks.
camin


Report Offensive Follow Up For Removal

Response Number 11
Name: Jim Boothe
Date: May 6, 2004 at 09:53:03 Pacific
Subject: Calculation the date
Reply: (edit)

Well, I don't know why it does not like the time specification. You might try looking at the man pages (man touch). Maybe your version requires accuracy to the second instead of to the minute (which would be good). If that were the case, you would want:

touch -t20031231235959 /tmp/Dec31


Report Offensive Follow Up For Removal

Response Number 12
Name: camin
Date: May 6, 2004 at 13:25:46 Pacific
Subject: Calculation the date
Reply: (edit)

Hi Jim,

I have looked up touch in the man pages,

I came up with this:

touch -t 2003123123.59 /tmp/Dec31
find /mydirtree -type f -newer /tmp/Dec31

am I right to say that the first statement will create a file called Dec31 with this 2003123123.59 in it? yes/no?

and the second statement will find all files newer than that date specified in /tmp/Dec31?
yes/no?

however, when I run it, the first statement does not return an error like before, but there is nothing in Dec31, thus the second statement does not return an error, but does not find any files, because Dec31 is empty...
do you have any suggestion?
thanks a million...



Report Offensive Follow Up For Removal

Response Number 13
Name: Jim Boothe
Date: May 6, 2004 at 14:46:48 Pacific
Subject: Calculation the date
Reply: (edit)

Oh gosh - I am embarassed! Your discovery of .SS in the man pages is a new discovery for me also. So I guess I should go read the man page myself! :-D

The touch command you just posted is invalid on my system, and should be invalid on yours also. You need an extra 59 in it:

touch -t 2003123123.59 /tmp/Dec31 (bad)
touch -t 200312312359.59 /tmp/Dec31 (good)

It means YYYYMMDDHHMI.SS

The touch command will change the datestamp on an existing file (if exists) or will create a new file. The new file will be empty as you have observed. That does not matter because we are interested in the date stamp of the file and not its contents.

Your touch command might be more lenient than mine since it did take your abbreviated time specification. Look at the date stamp to see what it created. If it simply assumed 00 minutes, then your date stamp would be Dec 31 23:00:59, which is not what we want but the find command should have still found some files.

At any rate, run the proper touch command, then the find command should work. And since we can in fact create a date stamp down to the nearest second, creating the date stamp of Dec 31, 2003 23:59:59 is exactly what you want.

And correct, the find command will locate all files having a modify date stamp more recent than the date stamp of the Dec31 file. And this would be date stamps of Jan 01, 2004 00:00:00 or later.

And let me know what date stamp your abbreviated time specification created.

I learned something today - thanks!


Report Offensive Follow Up For Removal

Response Number 14
Name: camin
Date: May 6, 2004 at 15:45:23 Pacific
Subject: Calculation the date
Reply: (edit)

Hi Jim,

You are a great it works!
I had to try it with year 2002 because I needed to work on a test machine first.

touch -t 200212312359.59 /tmp/Dec31
find $mydir/cups -type f -newer $workdir/Dec31 >file2.txt

The timestamp on Dec31 is:
14 Dec 31 2002 Dec31

I got 14 files.

Thanks Jim.
camin


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Calculation the date

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge