Convert string to date and add 1 hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert string to date and add 1 hours
# 1  
Old 09-05-2016
Convert string to date and add 1 hours

i have some set of date data inside csv files and need to convert the timezone,
Code:
08302016113611861
08302016113623442
08302016113541570
08302016113557732
08302016113548439
08302016112853115
08302016113620684
08302016113432827
08302016113630321

date format is : %m%d%Y%H%M%Smilisec
Code:
awk -F, '{OFS="";dat=substr($1,5,4)"-"substr($1,1,2)"-"substr($1,3,2)" "substr($1,9,2)":"substr($1,11,2)":"substr($1,13,2)"."substr($1,15,4);print dat }'

input.txt:
Code:
2016-08-30 11:36:11.861
2016-08-30 11:36:23.442
2016-08-30 11:35:41.570
2016-08-30 11:35:57.732
2016-08-30 11:35:48.439
2016-08-30 11:28:53.115
2016-08-30 11:36:20.684
2016-08-30 11:34:32.827
2016-08-30 11:36:30.321

i can do manual convert with date command buts its not effective
Code:
date -d '2016-08-30 23:36:30.321 1hours' +%m%d%Y%H%M%S%3N

how to execute date inside awk?

sample input 08302016113630321 add 8 hours to adjust timezone and the output is 08312016003630321
output from sample data above
Code:
08302016123611861
08302016123623442
08302016123541570
08302016123557732
08302016123548439
08302016122853115
08302016123620684
08302016123432827
08302016123630321


Last edited by vbe; 09-05-2016 at 08:53 AM.. Reason: more code tags
# 2  
Old 09-05-2016
Hello before4,

Could you please try following and let me know if this helps you, though following is not giving miliseconds at last, though I am working on it to get it done. Let me know how it goes then.
Code:
awk -vs1="\"" -vs2="'" '{VAL=s1 substr($0,5,4)"-"substr($0,1,2)"-"substr($0,3,2)" " substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2) OFS s1;print "date --date="VAL " +%s  | awk -vs1=" s1 "\\" s1 s1 OFS s2 "{VAL=$1+3600*8;system(" s1 "date -d@" s1 " s1 VAL s1 " s1 " +%m%d%Y%H%M%S" s1 ")}" s2}'  Input_file  | sh

OR (Non-one liner form of solution)

awk -vs1="\"" -vs2="'" '{
				VAL=s1 substr($0,5,4)"-"substr($0,1,2)"-"substr($0,3,2)" " substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2) OFS s1;
				print "date --date="VAL " +%s  | awk -vs1=" s1 "\\" s1 s1 OFS s2 "{VAL=$1+3600*8;system(" s1 "date -d@" s1 " s1 VAL s1 \
				" s1 " +%m%d%Y%H%M%S" s1 ")}" s2
                        }
                       '  Input_file | sh

Output will be as follows.
Code:
08302016193611
08302016193623
08302016193541
08302016193557
08302016193548
08302016192853
08302016193620
08302016193432
08302016193630

Where your Input_file is as follows.
Code:
cat  Input_file
08302016113611861
08302016113623442
08302016113541570
08302016113557732
08302016113548439
08302016112853115
08302016113620684
08302016113432827
08302016113630321

Explanation of above code is if you remove | shafter Input_file then you could see code will execute following commands one by one to shell which it will only print to console once you remove | sh from above code.
Code:
date --date="2016-08-30 11:36:11 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:36:23 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:35:41 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:35:57 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:35:48 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:28:53 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:36:20 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:34:32 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'
date --date="2016-08-30 11:36:30 " +%s  | awk -vs1="\"" '{VAL=$1+3600*8;system("date -d@" s1 VAL s1 " +%m%d%Y%H%M%S")}'

Will post update once I am able to get miliseconds too for this solution.
EDIT: Adding complete solution with miliseconds part too as follows.
Code:
awk -vs1="\"" -vs2="'" '{
				VAL=s1 substr($0,5,4)"-"substr($0,1,2)"-"substr($0,3,2)" " substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2)"."substr($0,15,3) s1;
				print "date --date="VAL " +%s.%3N  | awk -vs1=" s1 "\\" s1 s1 OFS s2 "{VAL=sprintf(" s1 "%.3f" s1 ",$1+3600*8);system(" \
				s1 "date -d@" s1 " s1 VAL s1 " s1 " +%m%d%Y%H%M%S%3N" s1 ")}" s2
		        }
                       '   Input_file | sh

Output will be as follows.
Code:
08302016193611861
08302016193623442
08302016193541570
08302016193557732
08302016193548439
08302016192853115
08302016193620684
08302016193432827
08302016193630321

Here wanted to mention previous solution command date --date="2016-08-30 11:36:11.861" +%s%3N was not having %3Npart so it was not able to print miliseconds, now it is. Please do let me know if this helps you or you have any queries on same.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-05-2016 at 02:16 PM.. Reason: Adding non-one liner forms of solutions now. Added complete solution successfully with GOD's grace now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-05-2016
You could also utilize something like
Code:
echo 08302016113611861 | sed 's/\(..\)\(..\)\(....\)\(..\)\(..\)\(..\)\(.*\)/\3-\2-\1T\4:\5:\6 \7/' | while read date msec
do
...
done

but I don't know if it's any more readable than the substrs Smilie

Juha

sed -E would allow one to omit the backslashes, does not help much

Last edited by rbatte1; 09-06-2016 at 04:53 AM.. Reason: Add ICODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: Thanks, SK (11 Replies)
Discussion started by: SK123
11 Replies

2. Shell Programming and Scripting

Convert string to date and add 20 days

Hi, I have a requirement where I am getting date in string format (20161130). I need to add 20 days(not no. 20) to the above string. The o/p should 20161220. In case of 20170228, it should show 20170320. Could you please help me with the command to achieve this. Note: I am using AIX 7.1... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

3. Shell Programming and Scripting

Convert string into date format

Hi, I am getting the below string as a input for date. 12/03/2013 11:02 AM I want to change this date as 03-DEC-2013 11:02 AM. Could you please help on this. Thanks Chelladurai (4 Replies)
Discussion started by: ckchelladurai
4 Replies

4. Emergency UNIX and Linux Support

Convert string to date and add 1

Hi All, I want to convert string in format YYYYMMDD(20120607) to date in unix and add 1 day to it and convert back to string in format YYYYMMDD. Please help. (4 Replies)
Discussion started by: cns1710
4 Replies

5. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

6. AIX

Convert string to date in script

Hi, How can I convert a string "Jul 10 09" to date in aix? the output can be like 20090710. Thanks. (4 Replies)
Discussion started by: Gbyte
4 Replies

7. Shell Programming and Scripting

Convert String to Date Unix

Hi people, I need to convert a string eg 09/13/2008 to a valid unix date. (4 Replies)
Discussion started by: sameerspice
4 Replies

8. Shell Programming and Scripting

Convert String to Date

Hi, I have a String input parameter like this: 20080430 (YYYYMMDD). Inside my korn shell script I need to add one day to this date. L_TRADE_DAY=$1 let L_TODAY=$L_TRADE_DAY+1 Offcourse this raises a problem at the end of a month. 20080430 + 1 gives 20080431 instead of 20080501. ... (2 Replies)
Discussion started by: ORatjeuh
2 Replies

9. Shell Programming and Scripting

Convert DATE string to a formatted text

Hi guys, i need your help. I need to convert a date like this one 20071003071023 , to a formated date like 20071003 07:10:23 . Could this be possible ? Regards, Osramos (6 Replies)
Discussion started by: osramos
6 Replies
Login or Register to Ask a Question