- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Unix Date Check
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 05:08 AM
05-18-2006 05:08 AM
Unix Date Check
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 05:35 AM
05-18-2006 05:35 AM
Re: Unix Date Check
compare this string:
date "+CREATED ON %Y%m%d%H:%M:%S"
with yours
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 05:50 AM
05-18-2006 05:50 AM
Re: Unix Date Check
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 05:54 AM
05-18-2006 05:54 AM
Re: Unix Date Check
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 05:56 AM
05-18-2006 05:56 AM
Re: Unix Date Check
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 06:01 AM
05-18-2006 06:01 AM
Re: Unix Date Check
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 06:05 AM
05-18-2006 06:05 AM
Re: Unix Date Check
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2006 09:05 PM
05-18-2006 09:05 PM
Re: Unix Date Check
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2006 09:14 PM
05-21-2006 09:14 PM
Re: Unix Date Check
please, could you take same time to assign points?
It's a little effort for you but much appreciated by the answers.
Rgds,
Art