LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help in date command (https://www.linuxquestions.org/questions/linux-newbie-8/help-in-date-command-850909/)

mmhs 12-17-2010 01:36 PM

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 !

colucix 12-17-2010 02:19 PM

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.

markush 12-17-2010 03:37 PM

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


All times are GMT -5. The time now is 07:27 PM.