Date validation


 
Thread Tools Search this Thread
The Lounge What is on Your Mind? Date validation
# 8  
Old 08-31-2016
Why is this not useful for you?
Code:
STRING=20150712_123
touch ZZ -t $STRING"0000"
touch: invalid date format ‘20150712_1230000'
STRING=201507
touch ZZ -t $STRING"0000"
touch: invalid date format ‘2015070000'
STRING=20150715TTT
touch ZZ -t $STRING"0000"
touch: invalid date format ‘20150715TTT0000'
STRING=12340512
touch ZZ -t $STRING"0000"
STRING=20160526
touch ZZ -t $STRING"0000"
STRING=20150712
touch ZZ -t $STRING"0000"
STRING=20150712
touch ZZ -t $STRING"0000"


Last edited by RudiC; 08-31-2016 at 05:59 AM..
# 9  
Old 08-31-2016
Quote:
Originally Posted by RudiC
Why is this not useful for you?
Code:
STRING=20150712_123
touch ZZ -t $STRING"0000"
touch: invalid date format ‘20150712_1230000'
STRING=201507
touch ZZ -t $STRING"0000"
touch: invalid date format ‘2015070000'
STRING=20150715TTT
touch ZZ -t $STRING"0000"
touch: invalid date format ‘20150715TTT0000'
STRING=12340512
touch ZZ -t $STRING"0000"
STRING=20160526
touch ZZ -t $STRING"0000"
STRING=20150712
touch ZZ -t $STRING"0000"
STRING=20150712
touch ZZ -t $STRING"0000"

Hi RudiC,
That is an interesting link (back to a time about 6 weeks before I joined this forum)...

Be VERY careful with this. The above usage is not portable. According to the standards, options come before operands. On systems conforming to the standards, the above will create (or update the timestamps) for three files (ZZ, -t, and the specified timestamp) to the current date and time; not try to set the timestamp of ZZ to the specified timestamp.

Furthermore, touch does some rounding to valid dates. For example:
Code:
touch -t 201606310000 ZZ # Trying to set date to June 31...
ls -l ZZ # Note July 1 timestamp...

produces the output:
Code:
-rw-r--r-- 1 doc  staff  0 Jul  1 00:00 ZZ

and:
Code:
touch -t 201606000000 ZZ # Trying to set date to June 00...
ls -l ZZ # Note May 31 timestamp...

produces the output:
Code:
-rw-r--r-- 1 doc  staff  0 May 31 00:00 ZZ

So, an exit code 0 from touch doesn't necessarily mean that a date is valid.
# 10  
Old 08-31-2016
Here is the why behind Don's correct comment about touch - the answer is the mktime STDC library call. It tries to "fix" bad dates.

Code:
//  mktime_test.c
#include <stdio.h>
#include <time.h>
// mktime attempts correction for bad dates given to other 
//     utilities like touch.

int main(void)
{
   struct tm time_str;
    char buf[30]={0x0};
    time_str.tm_year = 116;// which is 2016 - 1900
    time_str.tm_mon =  5;  // June, months are 0 - 11
    time_str.tm_mday = 34; // June 34 which is wrong
    time_str.tm_hour = 0;
    time_str.tm_min = 0;
    time_str.tm_sec = 1;   // first second of the day
    time_str.tm_isdst = -1;  // figure out whether DST is active or not
    if (mktime(&time_str) == -1) // error, note that mktime attempts to correct "bad" values.
        fprintf(stderr,"Bad input\n");
    else 
    {
        (void)strftime(buf, sizeof(buf), "%c", &time_str);
         printf("%s: time_str.tm_mon=%d  time_str.tm_mday=%d\n",
                buf, time_str.tm_mon, time_str.tm_mday);
    }
    return 0;
}

Example run:

Code:
$ ./mktime
Mon Jul  4 00:00:01 2016: time_str.tm_mon=6  time_str.tm_mday=4

The bad date was "fixed" to July 4.
This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Date validation in mysql

Hi All, We need to create the custom function to pass the parameter is date.if it is valid return 1 else 0 return should be 1 select is_date('2012-09-17'); return should be 0 select is_date('2012-79-17'); Thanks (2 Replies)
Discussion started by: bmk
2 Replies

2. Shell Programming and Scripting

Date validation

File contains below data,how to validate the date using awk command or any command. date formate is fixed as "YYYYMMDD" test1|20120405 test2|20121405 output should be: test1|20120405 Thanks (2 Replies)
Discussion started by: bmk
2 Replies

3. UNIX for Dummies Questions & Answers

Unix date validation

Dears, I am working on a batch that processes file with name containing date prefix eg., 20101222_file.dat. The logic is to process files in order. Eg., 20101225 must be processed only after 20101222. Ok first glance it looked simple, it use a variable to check this date value as number and... (2 Replies)
Discussion started by: naraink
2 Replies

4. Shell Programming and Scripting

Date validation

Hi, I have a script which runs on specific sunday. If that script runs on the sunday i want to execute another script on following wednesday. I have a log for that server. My wednesday scripts needs to check the sunday run log timestamp and if it matches it should run. Please help. Thanks,... (1 Reply)
Discussion started by: Krrishv
1 Replies

5. Shell Programming and Scripting

Date Validation in unix

I have a script which is take date as parameter sh abc.sh <2010-02-01> #!/sh/bin my_date=$1 #Here i want to two diffrent dates ## 3 Days before ##date14query=$mydate - 4 (it will be 2010-01-28) ##date24query=$mydate +4 (it will be 2010-01-05) #Please Help (3 Replies)
Discussion started by: pritish.sas
3 Replies

6. Shell Programming and Scripting

Date Validation

the user have to input the date format in mmddmmhhyyyy (month,date,minutes,hour,year) i want a shell script to check whether the user has properly input in the above said manner. kindly advice (2 Replies)
Discussion started by: vkca
2 Replies

7. Shell Programming and Scripting

Basic date validation help

Hi, I've made a very basic date validation script, but the syntax of the until condtion is wrong, could someone have a quick look and correct it please?:) Thanks for any help. echo -n "Please enter your date of birth (dd-mm-yyyy): " read dob day=${dob:0:2} month=${dob:3:2} year=${dob:6:4}... (1 Reply)
Discussion started by: mustaine85
1 Replies

8. Shell Programming and Scripting

Date Validation:

Hi I have below file with 3 rd column as date ....i want to make 3 column to mm/dd/yyyy . in the below file 2 row date is like 1/23/1994 so i want to append '0' to month i,e. 01/23/1994 and in the 3 row date is like 6/4/1994 ---so i want this to 06/04/1994 Source:... (1 Reply)
Discussion started by: satyam_sat
1 Replies

9. Shell Programming and Scripting

Date Validation again

I want a sample Date validation script using if loops. The script should first compare "year".If the year is lesser than the current year,It should go for "month" checking. I have the script that splits the date into year,month and date.I want only the checking part. My if loop checking is not... (4 Replies)
Discussion started by: dave_nithis
4 Replies

10. Shell Programming and Scripting

Problem with Date validation

Hi All, I've to validate a date input for the format YYYYMMDD. The input should be of 8 digits with only numeric values. I tried the following echo $1 | grep '^\{8}$/p' if then echo "Valid" else echo "invalid" I get the error, grep: RE error 16: Bad number. Any help... (3 Replies)
Discussion started by: sumesh.abraham
3 Replies
Login or Register to Ask a Question