adding time


 
Thread Tools Search this Thread
# 1  
adding time

I want to write a little script that will add 24:00 to the current time if the current time is in between 00:00 and 05:00. I want to make this new time a brand new file so I can work with it. I am not sure how to do this.




variable = current time on our system ( I will pull this)

example:

if (variable) is in between 00:00 and 05:00
then
(variable) + 24:00 = newfile


an example might look like this :

if the time I pull is 00:25 then add 00:25 to 24:00 and get 24:25 and put the 24:25 in a brand new file.

Thanks,

ngg
# 2  
You should clarify your requirements. What time is 24:15 supposed to represent? A day does not extend past 24 hours. But if we ignore that, you just get the hours and minutes using the "date" command. Then you add 24 to the hours if you want to. Where are you stuck? Also what language are you using?
# 3  
If you are going to respond, please try to answer my question instead of asking me why I am doing what I am doing. Obviously, I have a good reason for doing it or I would not be posting this.
# 4  
Genuine question

Perderabo is right. His questions are genuine.
# 5  
#!/usr/local/bin/bash

HOURS=`date +%H`
MINUTS=`date +%M`

if [[ $HOURS -lt 5 ]]
then
let "HOURS +=24"
fi

echo "${HOURS}:${MINUTS}"

but Perderabo said that hourt must be {0..23}. You must update this script for right calculate of hours.
# 6  
Thank you azbest. This needs to be an automated script and your example is working just like I need it to.
# 7  
Quote:
Originally Posted by azbest
#!/usr/local/bin/bash

That will not work on many systems. Do not use a shebang unless you know the location of the executable on the target system.

Quote:
HOURS=`date +%H`
MINUTS=`date +%M`

That will fail if the date changes between one date call and the next. Better (and faster) is:

Code:
eval "$( date "+HOURS=%H MINUTS=%M" )"

Quote:
if [[ $HOURS -lt 5 ]]

It is best to avoid the non-portable syntax; use:

Code:
if [ $HOURS -lt 5 ]

Quote:
then
let "HOURS +=24"

Another case of non-portable syntax. This will work in all POSIX shells:

Code:
HOURS=$(( $HOURS + 24 ))

Quote:
fi

echo "${HOURS}:${MINUTS}"
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #672
Difficulty: Medium
The 802.11ad/ay, also known as WiGig operates in 60 GHz ISM band.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Seconds to UNIX/Epoch-Time

Hello All, I have a Perl script I'm writing where I ask the user to enter a "start time" for something. The "$start_time" will be in the format of: # The Time CLI Option Can be in the format of: --start-time="1day" --start-time="2hours" --start-time="45min" ... (1 Reply)
Discussion started by: mrm5102
1 Replies

2. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

3. Shell Programming and Scripting

Adding columns of time

Hello all, I'm in the process of writing a script, and I need to be able to add columns of time in the following format (time elapsed Net Backup logs): 000:01:03 000:00:58 000:00:49 Does anyone have a way of converting and/or adding timestamps such as these accurately? Thanks in... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

4. Shell Programming and Scripting

Adding date and time to file name

Hi All, i wanted to add date and time to the file names in the same directory so lets say a file in the directory is test.txt then after running the shell script it should be test-15-11-2010.txt. So I used the following script which works, #!/bin/bash thetime=`date +%Y-%m-%d--%H:%M:%S`... (7 Replies)
Discussion started by: cc_at_work
7 Replies

5. UNIX for Dummies Questions & Answers

Adding date and time to a log file

Morning all Im hoping you can help me. We have a nice new oracle server :( and are needing to move some files around for EDI and BACS. The server runs windows but has an app called MKS toolkit installed which give unix commands. (Needed for the oracle stuff) I have had a go using dos commands... (2 Replies)
Discussion started by: ltodd2
2 Replies

6. Shell Programming and Scripting

problem with displaying date and adding time

Hi, I have a log file with contents like 81.49.74.131 - - 81.49.74.131 - - 116.112.52.31 - - 116.112.52.31 - - I need an output like this 81.49.74.131 14/Sep/2008 Time duration: 00:06:00 116.112.52.31 15/Sep/2008 Time duration: 00:00:01 Please anyone suggest a script for this.... (1 Reply)
Discussion started by: FuncMx
1 Replies

7. Shell Programming and Scripting

change the filename by adding up 1 each time, tricky one

:confused: Hi, I posted here before for adding up of datafile name each time, here is an example: #!/bin/bash cutdfname="data11.dbf" newname=$(echo "${cutdfname}" |tr "" "" |tr "#_@-" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |tr -s "x") num=$(echo $newname |cut -d"." -f1|awk... (5 Replies)
Discussion started by: netbanker
5 Replies

8. UNIX for Dummies Questions & Answers

adding time server

Hello Everybody, I have sun machine running Sol9 with ntpq running on it, and I have a network time server, I want to synchronize my sun machine with that time server, is there any way I can add the time server to my sun machine, root@yuda> ntpq ntpq> peers localhost: timed out, nothing... (4 Replies)
Discussion started by: aladdin
4 Replies

9. UNIX for Advanced & Expert Users

Adding # minutes to current time...

Hi all, Looking for a way to add lets say 10 minutes to the current time output should look like 7:15 AM or 7:15 PM. I know that gdate could do this for me but unfortunately its not available on the system I'm working on. So if any one know any way I can accomplish this using the date command it... (7 Replies)
Discussion started by: gptavares
7 Replies

10. Shell Programming and Scripting

Adding Elapsed time

I'm using the Bourne shell and trying to write a script that will add all the time that any particular user has been on the network for. I've used last-h | grep "username" | cut -c 58-62 to get the times. Then I wrote a script that takes the time and converts it into just minutes. Now I... (1 Reply)
Discussion started by: jrdnoland1
1 Replies

Featured Tech Videos