Check if a string is a valid timestamp in UNIX.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a string is a valid timestamp in UNIX.
# 1  
Old 05-21-2014
Check if a string is a valid timestamp in UNIX.

Hi all,
I have date and time value in a string,
I want to check if it is a valid date and time.

Need help on this.

Thanks
# 2  
Old 05-21-2014
A few questions:-
  • What have you tried so far?
  • What output/errors are you getting?
  • What OS and version are you using?
  • What are you writing your code in?
Most importantly, What have you tried so far?


Give a man some food and he will eat for a day. Give him seeds and teach him to farm and he will feed his family.



Robin
# 3  
Old 05-21-2014
I tried date -d $substring2
but this doesnt take the mins and secs
# 4  
Old 05-21-2014
What is the value of $substring2 when you call the date command?

If I set it to "12:34:56" I get the current date displayed with that as a time and a zero return code. Using "12:34:67" I get an error and a non-zero return code.


What input/output/errors do you see?




Robin

---------- Post updated at 12:55 PM ---------- Previous update was at 12:47 PM ----------

Have a look here too.

https://www.gnu.org/software/coreuti...-input-formats



Robin
# 5  
Old 05-21-2014
Its like
Code:
20140521122301

where its YYYYMMDDHHMISS format..
# 6  
Old 05-21-2014
you can do this check in oracle
Code:
select to_date('20140521122301','YYYYMMDDHHMISS') from dual;

if date is invalid oracle will throw error else the result.
# 7  
Old 05-21-2014
Without reaching out to Oracle, I don't think that format is acceptable. Did you read the GNU web pages I suggested in my update to previous post? They are not wonderfully clean/explicit and I have struggled to get things worked out, but they are the best I have found.

Try setting it to format "YYYYMMDD HH:Mi:SS"

That works for me:-
Code:
# date -d "20140505 13:33:45"
Mon May  5 13:33:45 BST 2014
# echo $?
0
# date -d 20140505133345     
date: invalid date `20140505133345'
# echo $?               
1
#



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

2. Shell Programming and Scripting

Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid. echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }' ex: ./script.ksh 12:34:21 = okay ./script.ksh... (10 Replies)
Discussion started by: erin00
10 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. UNIX for Dummies Questions & Answers

How to check if file contains valid strings?

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (5 Replies)
Discussion started by: rtagarra
5 Replies

5. Shell Programming and Scripting

how to check for valid password

I need to check if an account has a valid password. Would something like this work? read ACCNAME if grep -q "^$ACCNAME:\$6:" /etc/shadow; thenI noticed every entry in my shadow file that has a password starts with $6 ... it works for my current setup, but would it always work? I can't test... (4 Replies)
Discussion started by: ADay2Long
4 Replies

6. Homework & Coursework Questions

Bash shell - Check if value is valid directory.

1. The problem statement, all variables and given/known data: The script usage will be as follows: library.third source_directory - Your script will display an appropriate error message and exit with status 3 if no parameters are given - Your script will display an appropriate error... (2 Replies)
Discussion started by: netmaster
2 Replies

7. Shell Programming and Scripting

Whether a string is a valid unix command

How to find a string which is entered in command promt is a valid unix command or not?. Thanks in advance ~Saravana (2 Replies)
Discussion started by: tsaravanan
2 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

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

10. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies
Login or Register to Ask a Question