Add time hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add time hours
# 1  
Old 12-02-2015
Add time hours

Gents,

Is there the way to increase hours (+3) to the data with the format following.

example
Code:
 JDhhmmss
335193508

input
Code:
335193508
335203508
335213508
335223508
335233508

output
Code:
335223508
335233508
336003508
336013508
336023508

Or the following

input
Code:
193508
203508
213508
223508
233508

output
Code:
223508
233508
003508
013508
023508

Thanks fro your help
# 2  
Old 12-02-2015
Hi.

Using a repository (Debian) package dateutils:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate date arithmetic, dadd, dateutils.
# dateutils: repository, or:
# http://www.fresse.org/dateutils/
# https://github.com/hroptatyr/dateutils ; both verified 2015.12.02.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dadd

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

# Time format like:
# JDhhmmss
# 335193508

pl " Expected output:"
cat expected-output.txt

pl " Results:"
dadd -i"%D%H%M%S" -f"%D%H%M%S%n" +3H < $FILE 

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
dadd 0.2.6

-----
 Input data file data1:
335193508
335203508
335213508
335223508
335233508

-----
 Expected output:
335223508
335233508
336003508
336013508
336023508

-----
 Results:
335223508
335233508
336003508
336013508
336023508

So: specify input and output formats, read from STDIN.

See man pages and/or files at sites noted in script.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
Old 12-02-2015
Could this be accomplished by changing the TZ to 3 hours further west, and not actually changing the data?
This User Gave Thanks to jgt For This Post:
# 4  
Old 12-02-2015
JGT
Please can you explain .. by changing the TZ to 3 hours further west
# 5  
Old 12-02-2015
It occurred to me that 1pm Pacific Time is the same as 4pm Eastern.
In fact, if you look at the posts in this forum, the time of a particular post is shown in whatever local time you choose.
If you want to do it with arithmetic:
Code:
day=substr(input, pos 1,2 and 3)
hour=substr(input,pos 4 and 5)
minsec=substr(input, pos 6-9)
hour=hour+3
if hour gt 23
   hour=hour - 24
   day = day +1
endif

The exact code will depend on whether you use bash or php or ksh, or embed it in your application.
This User Gave Thanks to jgt For This Post:
# 6  
Old 12-02-2015
bash:
Code:
while { read -N3 day; read -N2 hour; read REST; }; do hour=$(((hour+=3) %24)); day=$((day+(hour<3))); printf "%s%02d%d\n" $day $hour $REST; done < file
335223508
335233508
336003508
336013508
336023508

---------- Post updated at 23:07 ---------- Previous update was at 22:35 ----------

or even:
Code:
while { read -N3 day; read -N2 hour; read REST; }; do printf "%s%02d%d\n" $((day+(hour>20)))  $(((hour+=3) %24)) $REST; done < file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-03-2015
Thanks RudiC

---------- Post updated at 12:49 PM ---------- Previous update was at 02:06 AM ----------

RudiC,

Please can you advise on this:

Code:
42933.00 47729.00 310043656
42931.00 47729.00 310043745
42929.00 47729.00 310043853
42927.00 47729.00 310044157
42925.00 47729.00 310044245
42925.00 47717.00 310045829
42927.00 47717.00 310045918
42929.00 47717.00 310050013
42931.00 47717.00 310050103
42933.00 47717.00 310050149
42935.00 47717.00 310050236
42935.00 47705.00 310050510
42933.00 47705.00 310050556
42931.00 47705.00 310050642
42929.00 47705.00 310050729
42927.00 47705.00 310050822
42925.00 47705.00 310050934

I got the msg
Code:
printf: 0149: invalid octal number
printf: 0729: invalid octal number
printf: 0822: invalid octal number

How to select to change the code to make the changes in column 3

Thanks for your elp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can anyone help me to print UNIX epoch time to days,hours,min,sec ?

I have unix epoch time 1441678454803, Can you please help me to print this time in below format ? DAY,HOUR,MIN,SEC Appreciate your help!!! Thanks, Prince (7 Replies)
Discussion started by: prince1987
7 Replies

2. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

3. Shell Programming and Scripting

awk : Search for text between two time frame (12 hours)

I have created the script to grep the errors from weblogic logs files and redirecting output to file.txt ...From file.txt I'm using awk command to collect the past 20 mins output...The script running from cron every 15 mins... The script working well... Now the challenges, I'm trying to use... (27 Replies)
Discussion started by: zenkarthi
27 Replies

4. Shell Programming and Scripting

Diff between two time in hours in last column

Dear All I want to diff between two time(FIRST 4 COLUMN) in hours in last column. Kindly help me for same. 2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 2013-11-23 15:51:23 2013-11-22... (9 Replies)
Discussion started by: jaydeep_sadaria
9 Replies

5. UNIX for Dummies Questions & Answers

Execute crontab for every 4 hours and begin from current time

I want to add a crontab entry which should execute for every 4 hours and that 4 hours calculation should begin from the current time. Normally if I set the crontab entry like this, 00 */4 30 05 * root date >>/tmp/cronout The above will execute the date command for every 4 hours like... (7 Replies)
Discussion started by: Ganeshwari
7 Replies

6. Shell Programming and Scripting

Minues four hours from time

Hi, I am trying to design a script, which will monitor a log file, and it should dump all the rows that were logged between now and past four hours. The date format within the logs is "2/Jan/2011:03:13:27". So I am using below command to substract four hours from the current time : $... (8 Replies)
Discussion started by: john_prince
8 Replies

7. UNIX for Dummies Questions & Answers

Calculating the Hours between two time values

Dear Folks, I want to calculate the elapsed hours between two time columns. I am using timestampdiff method for the same. I am able to get the value. But facing an issue of decimal values. For example the elapsed hours between 09:00:00 and 20:30:00 is coming as 11 instead of 11.5. I am using below... (1 Reply)
Discussion started by: dinesh1985
1 Replies

8. Shell Programming and Scripting

Process Time in hours or minutes only

Hi i want to print the time of a process in hours only..(or) in minutes only.Is there anyway to print the process such like that when i give the commnand like following #ps -eo pid,time PID TIME 412 01:49:32 481 00:03 it shows in HH:MM:SS format: Could anyone... (1 Reply)
Discussion started by: srikanthg
1 Replies

9. Shell Programming and Scripting

script for add and subtract two hours

i find a script to calculate hours of job i(nclude extraordinary) i make a script to calculate add and subtract two hours (format hh:mm:ss) (7 Replies)
Discussion started by: ZINGARO
7 Replies

10. UNIX for Dummies Questions & Answers

capturing the time stamp to get hours

Hi All, I am working on a korn shell script. I have a problem.i am calculating the next 27 hours from the time stamp. say TSTAMP=20060724000000 if i add 27 hours to the above time stamp, i will get 20060724143000 this is the code: YEAR=`echo $TSTAMP | awk... (0 Replies)
Discussion started by: pavan_test
0 Replies
Login or Register to Ask a Question