How to use datecalc for adding hours


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use datecalc for adding hours
# 1  
Old 05-30-2008
How to use datecalc for adding hours

Hello:
I need to generate two datetimestamps in one hour apart.
the start date is 05262008000000
then generate the following dates
05262008010000
05262008020000
05262008030000
... ... ...
... ... ...

05262008230000
05272008000000
05272008010000
05272008020000
05272008030000
... ... ...
... ... ...

05282008230000

I would like to use datecalc as it seems to be pretty much well written. But not sure how I could use it for adding it for hours and generationg the format I would need. Please help
# 2  
Old 05-30-2008
Code:
#! /usr/bin/ksh
date[0]=2008
date[1]=5
date[2]=28
hour=0
while : ; do
        printf "%02d%02d%4d%02d00\n" ${date[1]} ${date[2]} ${date[0]} $hour
        if (( hour == 23)) ; then
                ((hour=0))
                set -A date $(datecalc -a ${date[*]} + 1)
        else
                ((hour=hour+1))
        fi
done

# 3  
Old 05-31-2008
Here is another way using ksh93 which BTW has great builtin date arithmetic capabilities
Code:
#!/bin/ksh93

startdate="2008-05-26 01:00:00"
count=71

for ((i=0; i < count; i++))
do
   printf "%(%m%d%Y%H0000)T\n" "${startdate} + $i hour"
done

exit 0

# 4  
Old 06-02-2008
Thanks
Quote:
Originally Posted by Perderabo
Code:
#! /usr/bin/ksh
date[0]=2008
date[1]=5
date[2]=28
hour=0
while : ; do
        printf "%02d%02d%4d%02d00\n" ${date[1]} ${date[2]} ${date[0]} $hour
        if (( hour == 23)) ; then
                ((hour=0))
                set -A date $(datecalc -a ${date[*]} + 1)
        else
                ((hour=hour+1))
        fi
done

correct me if I am wrong.
I have datecalc.ksh and the above script say "test.ksh" in the same directory and when I run test.ksh I am getting "datecalc not found" error.

Should n't this line be:
Code:
set -A date $(datecalc -a ${date[*]} + 1)

modified line
Code:
set -A date $(. datecalc.ksh -a ${date[*]} + 1)

Please see the folating point and .ksh extensiton. Please let me know if I am missing anything.
# 5  
Old 06-02-2008
I tested my script before I posted it. It works fine as written, But I called datecalc datecalc. If you call datecalc something else, like datecalc.ksh, then that is what you must use. I also have datecalc in a directory on my PATH. If you can't do that, use a full path like:
set -A date $(/path/to/datecalc -a ${date[*]} + 1)

What is folating point? floating point? datecalc uses integer arithmetic.
# 6  
Old 06-03-2008
Thanks.
I named my datecalc as datecalc.ksh

Floating point is the point you see here
Code:
(. datecalc.ksh

the dot you see before datecalc. It is not related to Maths.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Adding 48 hours to DateTime

Hey Guys, I have looked for a solution throughout the forum for my particular question, but I cant find one. So I'm sorry if I overlooked it. I need to be able to 48 add hours to a particular DateTime string. I have a variable named $startTime I would like to be able to take that... (1 Reply)
Discussion started by: chagan02
1 Replies

3. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

4. Shell Programming and Scripting

Adding hours to current date

Hi, any idea how to add hours to current date in unix. thanks in advance (9 Replies)
Discussion started by: Abhijeet_Atti
9 Replies

5. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

6. Shell Programming and Scripting

how to list files between last 6 hours to 3 hours

Hi Frens, I want to list some files from a directory, which contains "DONE" in their name, i am receiving files every minute. In this i want to list all the files which are newer than 6 hours but older than 3 hours, of current time i dont want my list to contain the latest files which are ... (4 Replies)
Discussion started by: Prat007
4 Replies

7. UNIX for Dummies Questions & Answers

Doubt in datecalc.....

I have a doubt in date conversion. Can anyone explain the logic behind the date2jd and jd2date in datecalc function specified in this forum. I could not understand what the number are actually representing. Please explain...bcoz i am doing a program with this one.. Note: No need to... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

8. Shell Programming and Scripting

datecalc question.

I have a script that works perfectly on a linux box, but I am going to have to move it over to solaris which I have never really worked with. I was a bit miffed that date -d was not available on solaris so I started looking for alternative soltuoins and found this forum and datecalc. But I... (4 Replies)
Discussion started by: trey85stang
4 Replies

9. Shell Programming and Scripting

datecalc

Is there no simple way using 'date' command just to get number of days from given date. ie. if I pass 2007-01-15 as an argument. I want to obtain result as 015 Simarly for 2007-02-10. I want to obtain result as 41. Can't this be achieved by simple 1 or 2 lines of command (date). As this is... (1 Reply)
Discussion started by: tostay2003
1 Replies

10. Shell Programming and Scripting

Need a help Please- runing the perderabos script datecalc

i have a script in bourne called cdrsnokia.sh and inside of it calls a script called resta_dias where it calls the datecalc script (perderabos date calculator). The purpose is to rest (-) one day arithmetical operation the content of each line and the result is passed to another file lista2;after... (1 Reply)
Discussion started by: alexcol
1 Replies
Login or Register to Ask a Question