Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the date command in a script

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
US
I need to add two entires to a data file. These entires are the present date and (the date that would be two weeks back from the present date)

In other words

todays date - 14 days
If todays date was 1/3/01
The second date would need to be 12/20/00

These entries get appened to a file.
I figured out how to create the first date.
date +%D > date.new

Any ideas on how to create the second date ?

-Danny






 
I could not find any date functions in the operating system, nor in the C functions that would allow you to obtain the date by subtracting a number from the current date.

However there is a way around this but it is somewhat terse. It requires that you set up some initial parameters to do the calucations. But once completed this utility script can perform the function for many types of applications.

Start with setting up Max Days in each month, include a conditional for leap year.

1. 31
2. 28 2. 29 (leap year)
3. 31
4. 30
5. 31
6. 30
7. 31
8. 31
9. 30
10.31
11.30
12.31

nonlpyr = "312831303130313130313031"
lpyr = "312931303130313130313031"

You can use awk to return the value based on the index position of the selected month.

if today's date (day of Month) - n_days > 0 then
this is easy.
else
this is where it gets a little more involved
remainder = n_days - today's date (day of Month)
if current month = 1 then
prvyear = current year (4 digits) - 1
prvmonth = 12
else
prvmonth = current month - 1

if modulus or remainder of current year / 4 = 0
leap year, use standard Y2K leap year calucation rules
( has rules for leap year calcs)
use leap year rules to determine leap year
and if prvmonth is 2
if prvmonth = 2
prvmonthdays = 29
else
prvmonthdays = get prvmonth max days of month from list

prvmonthday = prvmonthdays - remainder (from above)

This should give you the year, month, day.

If you use this with $1 argument in a script and place it in the /usr/bin directory it will provide a tool for you to get a date based on the value entered.

Hope this helps.

Scott.
 
I'll give it a try. I'll post back how it goes.



-Danny






 
Well I am not a strong AWK user. It may take me some time to figure this all out?



-Danny






 
Danny,

How's your C then?

On the mktime man page there's a chunk of code that could easily be modified to make a little program that outputs the date plus or minus whatever value you specify.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top