validate timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validate timestamp
# 1  
Old 03-22-2012
validate timestamp

How to validate the user supplied timestamp?

Ther requirement is as follows.
While invoking the script, the parameters passed to the script are minimum and maixmum timestamps.
let's say min_tstmp and max_tstmp

ksh abc.ksh 2011-09-01-00:00:00 2011-12-01-00:00:00

so min_tstmp=2011-09-01-00:00:00
and max_tstmp=2011-12-01-00:00:00

now i need to validate the min_tstmp and max_tstmp whether it is a valid timestamp or not.
If min_tstmp is valid display "min_tstmp is valid" else "min_tstmp is invalid timestamp".
If max_tstmp is valid display "max_tstmp is valid" else "max_tstmp is invalid timestamp".

I know that it is bit difficult to achieve to validate this using shell script.

But it is possible using perl script.

can anyone help me out with the perl script to validate these two timestamps are valid or not.

Thanks
Krishnakanth
# 2  
Old 03-22-2012
What's your system?
# 3  
Old 03-22-2012
Quote:
Originally Posted by kmanivan82
now i need to validate the min_tstmp and max_tstmp whether it is a valid timestamp or not
Validate based on what criterion?
# 4  
Old 03-22-2012
If you have GNU date (supports -d)

This will do it:

Code:
if date -d "$(echo $1 | sed 's/-//;s/-//;s/-/ /')" > /dev/null 2>&1
then
   echo min_tstmp is valid
else
   echo min_tstmp is invalid timestamp
fi
 
if date -d "$(echo $2 | sed 's/-//;s/-//;s/-/ /')" > /dev/null 2>&1
then
   echo max_tstmp is valid
else
   echo max_tstmp is invalid timestamp
fi

or
Code:
D1=$(date -d "$(echo $1 | sed 's/-//;s/-//;s/-/ /')" +%s 2> /dev/null)
D2=$(date -d "$(echo $2 | sed 's/-//;s/-//;s/-/ /')" +%s 2> /dev/null)
if [ -n "$D1" -a -n "$D2" -a ${D1:-0} -gt ${D2:-0} ]
then
   echo "min_tstmp is after max_tstmp"
else
   [ -n "$D1" ] && echo min_tstmp is valid || echo min_tstmp is invalid timestamp 
   [ -n "$D2" ] && echo max_tstmp is valid || echo max_tstmp is invalid timestamp
fi


Last edited by Chubler_XL; 03-22-2012 at 10:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

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

3. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

4. Shell Programming and Scripting

Identifying files with a timestamp greater than a given timestamp

I need to be able to identify files with file timestamps greater than a given timestamp. I am using the following solution, although it appears to compare files at the "seconds" granularity and I need it at the milliseconds. When I tested my solution, it missed files that had timestamps... (3 Replies)
Discussion started by: nkm0brm
3 Replies

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

6. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

7. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

8. Shell Programming and Scripting

conversion of different timestamp to standard timestamp

hi i need a scrit to convert one date format to another. for example i have three columns in a file which gets a different format, but lastly i want output with stadard timestamp as "yyyy-mm-dd hh:mm:ss" column1 column2 ... (2 Replies)
Discussion started by: dprakash
2 Replies

9. Shell Programming and Scripting

How to Validate

I have one script by which I am taking Extraction Schedule in the following format ,,,...... Here I want validate the Input from user is okey or not. Let say i have one variable SCH SCH=12:34,23:12,11:20 Could you please tell me how I will validate it. HH & MM Both should be Numeric HH... (10 Replies)
Discussion started by: SanjayLinux
10 Replies

10. Shell Programming and Scripting

validate

hi need help here, a bit of code im doing requires a number to be enetered i have got validation and loop if its outside a ceratain range, but was wondering what code i need to validate if a character has been entered. cheers (3 Replies)
Discussion started by: ruffenator
3 Replies
Login or Register to Ask a Question