LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2010, 02:36 PM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
help in date command


hi guys i have a simple problem i want to give date in my shell script and then convert it to epoch but i wanna to filter illegal day or bad day same as 12123213 or asd or ...
i put the date in n variable and then
i use `date -d "$n" +%s` but i have problem when user press n or d f g 1 or 2 or 3 or 4 or f or other character date work fine without any error :-o

e.g
date -d "n" +%s you can use 1a a1 12 b c o f or other character Instead of "n" and i want to filter it !!!

i filter some of them with wildcard but it's not enough or professional !!


plz help me !
 
Old 12-17-2010, 03:19 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
First, valid dates have limits, that are different for 32 or 64-bit systems. The number of seconds elapsed from (or before) epoch cannot exceed the maximum 4-byte integer representable by the system. This is a limit especially for 32-bit systems, that will stop working on 19 January 2038 03:14:07 UTC. Similarly the minimum representable date is 13 December 1901 20:45:52 UTC.

Second if the format is yyyymmdd you can simply test if the input date is between the lower and the upper limit of valid dates by means of a numerical comparison: indeed, using this format dates are sorted both by time and numerically.

Anyway your best bet is to write a regular expression that ensures the following conditions:
1) date is made only of digits
2) digits are 8
3) year is between the limits of valid dates on 32-bit systems
4) month and day are between their own limits.
 
1 members found this post helpful.
Old 12-17-2010, 04:37 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hi mmhs,

as an example
Code:
#!/bin/bash
if [[ $1 =~ (0[1-9]|1[0-2])([0-2][1-9]|30|31)20[0-1][0-9] ]]; then
      echo "correct";
else
      echo "wrong";
fi
this regex checks for a mmddyyyy format (without any guarantee ), for example today we have 12172010.

Markus
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using date command sumeet inani Linux - Newbie 9 05-06-2010 06:40 AM
date command miran_shahverdyan Linux - General 4 10-14-2009 07:59 AM
command for date DarReNz Solaris / OpenSolaris 7 09-05-2006 10:53 AM
getting hour from date command Piotrekzc Programming 4 01-31-2006 10:53 PM
date command aetucker1 Linux - General 3 01-21-2005 01:57 PM


All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration