Comparing current date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing current date
# 8  
Old 11-03-2010
Code:
$
$
$ echo "Tue 6:00 AM-Wed 10:00 PM" | perl -lne 'BEGIN {%dow=qw(Sun 0 Mon 1 Tue 2 Wed 3 Thu 4 Fri 5 Sat 6)}
           /^(\w+)\s*(\w+):(\w+)\s*(\w+)-(\w+)\s*(\w+):(\w+)\s*(\w+)$/;
           $smin = $2*60+$3+($4 eq "PM" ? 12*60 : 0);
           $emin = $6*60+$7+($8 eq "PM" ? 12*60 : 0);
           @x = localtime;
           if ( ($x[6] < $dow{$1}) or
                ($x[6] == $dow{$1} and ($x[2]*60+$x[1]) < $smin) or
                ($x[6] > $dow{$5}) or
                ($x[6] == $dow{$5} and ($x[2]*60+$x[1]) > $emin)
              ) {
             print "Current date/timestamp is outside the range => $_"
           } else {
             print "Current date/timestamp is within the range => $_"
           }'
Current date/timestamp is within the range => Tue 6:00 AM-Wed 10:00 PM
$
$
$

tyler_durden
# 9  
Old 11-03-2010
OK here is a bash/ksh script that does it (again you need GUN date for the --date function).

I defined a function thisweek() that returns the epoch time of a string like "Tue 09:00AM", once that's done the rest is a simple numeric comparison.

inrange:
Code:
DOW_TODAY=$(date +%u)
thisweek()
{
  DOW=$(date --date "$1" +%u)
  [ $DOW -lt $DOW_TODAY ] && date --date "${1}-7days" +%s && return
  date --date "${1}" +%s
}
 
END=${1#*-}
START=${1%-*}
NOW=$(date +%s)
if [ $NOW -ge $(thisweek $START) -a $NOW -le $(thisweek $END) ]
then
   echo "In range"
else
   echo "Not in range"
fi

Here are some tests:
Code:
$ inrange "Mon 01:00AM-Fri 09:00PM"
In range
 
$ inrange "Mon 01:00AM-Tue 01:00AM"
Not in range
 
$ inrange "Thu 11:00PM-Fri 11:00PM"
Not in range
 
$ inrange "Yesterday 11PM-3PM"
In range


Last edited by Chubler_XL; 11-03-2010 at 10:33 PM..
# 10  
Old 11-03-2010
Hi,

Thanks a lot for your code. Let me try and see.

Thanks,
bharathappriyan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

4. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

5. UNIX for Dummies Questions & Answers

Comparing Output Date to Current System Date

Hi Guys, Anyone who knows how to compare the current date with the a file containing a date, say for example I have a file that looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Fri Dec 16... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

6. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

7. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. UNIX for Dummies Questions & Answers

comparing field to current year

Hi, I have a comma delimited file that contains name, account number, and account date/time(example record below). I want to pull off all the records that have an account date greater than 8/1 of the current year, and create a new file with those records. So for this year, it would take... (6 Replies)
Discussion started by: keeferb
6 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question