How to convert date and timestamp?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert date and timestamp?
# 1  
Old 08-03-2011
Question How to convert date and timestamp?

Hi,
I have a file file1 having data as below

Code:
20110501,070742, ,012345678909,09999999999,68.5,
20110501,070236, ,089375855455,09376383333,374.3, 
20110501,070525, ,090345895555,08444233444,206.2,
20110501,230051, ,000934744433,07624262223,480.1,

First field is date(YYYYMMDD) and second field is time stamp(HHMMSS).The timestamp is GMT. Now I have to add 5 hr. and 30 min. to the second field .But now the time will be changed and date also(if time is 23 hrs 00 min and 51 seconds).

the ouput should be:

Code:
20110501,123742, ,012345678909,09999999999,68.5,
20110501,123236, ,089375855455,09376383333,374.3, 
20110501,123525, ,090345895555,08444233444,206.2,
20110502,043051, ,000934744433,07624262223,480.1,

How to do this. Plese help.

Last edited by radoulov; 08-03-2011 at 10:27 AM.. Reason: Code tags.
# 2  
Old 08-03-2011
Asked many times here (similar ones). search "getcalc" in this forum.

If you have DB in your system, then it would be a simple "sql" to do that.
# 3  
Old 08-03-2011
There is no function getcalc available. please help if anybody having any idea.
# 4  
Old 08-03-2011
Aplogies for the typo, it is "datecalc".

A link with more details:

https://www.unix.com/answers-frequent...rithmetic.html
# 5  
Old 08-03-2011
Thanks panyam. But my question is totally different. I have certain records in a file. First field is YYYYMMDD(Yearmonth and date) and the second field is HHMMSS(Hour,min and sconds).this is 24 Hrs format. Then I have to convert the time in the fields to IST. i.e. I have to add 5 hrs 30 min to the second field. and then obviously i have to check the date.suppose if the time in second field is 233000. then the new time will be 050000. and the date will be incremented by 1. I have to implement this scenario.

sample input file:
----------------
Code:
20110501,070742, ,012345678909,09999999999,68.5,
20110501,070236, ,089375855455,09376383333,374.3, 
20110501,070525, ,090345895555,08444233444,206.2,
20110501,230051, ,000934744433,07624262223,480.1,
.
.
.


Output File:
-----------
Code:
20110501,123742, ,012345678909,09999999999,68.5,
20110501,123236, ,089375855455,09376383333,374.3, 
20110501,123525, ,090345895555,08444233444,206.2,
20110502,043051, ,000934744433,07624262223,480.1,
.
.
.


The date has been changed in the 4th record.

Please help if anybody having any idea.

Last edited by Franklin52; 08-03-2011 at 01:13 PM.. Reason: Please use code tags for data and code samples, thank you
# 6  
Old 08-03-2011
Dont know how to do date arithmetic in shell or awk...as it is tedious and cumbersome but here's a perl script you can try. It reads each line from the input file and splits it into fields based on the comma delimiter...then it extracts the year month monthday etc. information from each field and converts it into seconds since the epoch and adds 5.5 hrs (converted into seconds) to it...followed by recalculating the new date and time and putting it into a tm object...
Code:
#!/usr/bin/perl

while (<>) {
    use Time::Local;
    use Time::localtime;

    @f = split /,/, $_;
    $gmt = 5*60*60 + 30*60;

    $yr = substr($f[0], 0, 4) - 1900;
    $mo = substr($f[0], 4, 2) - 1;
    $dy = substr($f[0], 6, 2);
    $hr = substr($f[1], 0, 2);
    $mi = substr($f[1], 2, 2);
    $se = substr($f[1], 4, 2);

    $epoch = timelocal($se,$mi,$hr,$dy,$mo,$yr) + $gmt;
    $tm = localtime($epoch);

    printf("%04d%02d%02d,",$tm->year+1900,$tm->mon+1,$tm->mday);
    printf("%02d%02d%02d,",$tm->hour,$tm->min,$tm->sec);
    printf("%s,%s,%s,%s,\n",$f[2],$f[3],$f[4],$f[5]);
}

Save the above perl script into a file and run it on the command line like...
Code:
your_script_name inputfile

This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Timestamp in text to Serial Date-Time

Hi, I have a data file where the timestamp is in the style of: 2016-10-11 07:01:23.375-500 which is yyyy-mm-dd hh-mm-ss-000 then time conversion from UTC What i need to do is convert these timestamps from the above format to a the Serial Date format (i.e 42,654.2920446 ) now.. if... (14 Replies)
Discussion started by: AshBax
14 Replies

2. Shell Programming and Scripting

Convert date in dd mm yyyy format to UNIX timestamp

Hello All, I have a date in DD/MM/YYYY format. I am trying to convert this into unix timestamp. I have tried following: date -d $mydate +%s where mydate = 23/12/2016 00:00:00 I am getting following error: date: extra operand `+%s' Try `date --help' for more information. ... (1 Reply)
Discussion started by: angshuman
1 Replies

3. UNIX for Dummies Questions & Answers

Convert Epoch to timestamp in a file

Hi Team, Could you please let me know ,how to convert Epoch column to timestamp in a flat file. "57894"|"1454247163111"|"""HH""" "57897"|"1454247163111"|"""HH""" "7906"|"1454247163111"|"""ss""" I want second field as timestamp. This thread has been moved from a non-technical forum to... (6 Replies)
Discussion started by: shabeena
6 Replies

4. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies

6. AIX

convert a specific date to a unix timestamp

hello, i have an AIX5.3 machine and i am writing a script to display some processes. inside the script i want to get the time that the process starts and convert it to a unix timestamp. is there a command that i can use to do that? i search the web but all i found is long scripts and it does... (4 Replies)
Discussion started by: omonoiatis9
4 Replies

7. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

8. 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

9. Shell Programming and Scripting

Convert Unix Timestame to real timestamp

Hello, Did anyone know how to use script (e.g. perl) to conver Unix Timestame to real timestame in GMT+8 ? 1245900787 file:/tmp/a/Test/.txt.swp has created 1245900988 file:/tmp/a/Test/.txt.swp has changed Thu, 25 Jun 2009 11:33:07 GMT+8 file:/tmp/a/Test/.txt.swp has created Thu, 25 Jun... (4 Replies)
Discussion started by: happyday
4 Replies

10. Shell Programming and Scripting

how to convert from timestamp to date format in tcsh

hello all im looking for fast way to convert timestamp format to date format and vaiseversa in tcsh , can it be done? thanks allot (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question