1834149 Members
2319 Online
110064 Solutions
New Discussion

Re: Unix Date Check

 
intp
Frequent Advisor

Unix Date Check

Hi,

I receive an input flat file and part of firt line of the file will be the date of creation of that file in the format of YYYYMMDDHH:MM:SS
for example .. first line will always look like (except for date change),

CREATED ON 2006032417:23:27
---
---

Now, i open this file after some days ..say 10,20 or even months later...i want to
check how old the file is and if its only less than or equal to 12 days old then i need to process the file else exit with a msg "File is "XYZ" days old , cannot process".

how to check this string value against unix date ? should i cut each field (MM / DD etc) and check ?

Any help appreciated. Thanks.

8 REPLIES 8
Tomek Gryszkiewicz
Trusted Contributor

Re: Unix Date Check

Hi,
compare this string:
date "+CREATED ON %Y%m%d%H:%M:%S"
with yours
James R. Ferguson
Acclaimed Contributor

Re: Unix Date Check

Hi:

Perl can accomodate this easily by matching the expected line, extracting the date, and converting it to the epoch seconds for comparison:

# cat .thedate

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $file = shift or die "Filename expected\n";
open(FH, "<", $file) or die "Can't open $file: $!\n";
$_ =;
if (/CREATED ON (\d{10}):(\d\d):(\d\d)/) {
my $time = timelocal
($3, $2, substr($1,8,2), substr($1,6,2),
(substr($1,4,2)-1), (substr($1,0,4)-1900));
print time()-$time >= (60*60*24*12) ? "1\n" : "0\n";
}
1;

...Run as:

# ./thedate filename

The return of a "1" means that the file is equal or greater than 12-days old. A return value of "0" means its not.

By the way, there is really no such thing as a "creation" date in Unix. The 'mtime' of a file represents its last modification date which when a file is first "created" coincidentally equals a creation date.

Regards!

...JRF...
intp
Frequent Advisor

Re: Unix Date Check

I cant use perl scripting for some official reasons.pure shell script will help. but thanks for your time.

Matti_Kurkela
Honored Contributor

Re: Unix Date Check

Comparing the human-readable times is rather complex: you must account for changes of month, changes of year, different month lengths and leap years.

The simplest way would be to use the operating system's facilities to convert both the file's creation time and the current time to Unix time format (seconds elapsed since 00:00:00 UTC 1.1.1970). Then the comparision is simple "if difference is less than X seconds, do this".

However, there seems to be no easy access to date conversion functions from HP-UX standard shells. With Perl, this would be easy.

A. Clay Stephenson has posted to these forums a "date hammer" script named caljd.sh which should help if you have to solve your problem with shell scripts. Search the ITRC forums with "any word" option for "date hammer caljd.sh", you should get the script and many examples of its use.
MK
James R. Ferguson
Acclaimed Contributor

Re: Unix Date Check

Hi (again):

If you can't use the features of a STANDARD Perl distibution, then good luck to you and your management!

You can use the Perl script I suggested in a shell script simply by doing:

#!/usr/bin/sh
STATE=`/usr/local/bin/thedate /file_to_test`
echo ${STATE}

Regards!

...JRF...

Peter Nikitka
Honored Contributor

Re: Unix Date Check

Hi,

I wouldn't care about centuries, minutes and seconds.
Additional leep year changes are left to the reader as an exercise ...

awk -v now=$(date +%Y.%m.%d.%H) -v lim=12 'BEGIN {m[1]=0;m[2]=31;m[3]=59;m[4]=90;m[5]=120;m[6]=151
m[7]=181;m[8]=212;m[9]=243;m[10]=273;m[11]=304;m[12]=334
lim*=24
}
function date2num(dmy) { split(dmy,d,"."); d_of_y=d[3]+m[d[2]+0];
if(!d[1]%4 && (d[3]+0>2)) d_of_y++
d_of_y+=(d[1]-2000)*365
return d_of_y }
{numnow=date2num(now)*24+d[4]; ymdh_file=substr($3,1,4)"."substr($3,5,2)"."substr($3,7,2)"."substr($3,9,2)
numfile=date2num(ymdh_file)*24+d[4]
age=numnow-numfile;
if(age>lim) print FILENAME,"too old:",age
}' yourfile

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arturo Galbiati
Esteemed Contributor

Re: Unix Date Check

Hi,
you can sue this script in conjunction with the caljd.sh script attached.

#!/user/bin/ksh
WrkFil=
MM=$(head -1 $WrkFil|cut -c16-17)
DD=$(head -1 $WrkFil|cut -c18-19)
YY=$(head -1 $WrkFil|cut -c12-15)
echo $YY $MM $DD
integer DAYS_OLD=$(caljd.sh)-$(caljd.sh $MM $DD $YY)
echo File $WrkFil is $DAYS_OLD days old
#eof

HTH,
Art
Arturo Galbiati
Esteemed Contributor

Re: Unix Date Check

Hi,
please, could you take same time to assign points?
It's a little effort for you but much appreciated by the answers.
Rgds,
Art