LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-30-2008, 11:30 AM   #1
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Shell and handling dates


I have some scripts that take a date as an optional parameter. It's now becoming necessary to actually do some sanity checking on that date as it is required to be in a strict format. I've come up with the following pretty quick, but wonder if you guys have anything like this or a more clever way. The following does check for future dates and invalid dates such as Feb 29 on non-leap year.

Code:
#!/bin/env bash

shopt -s -o nounset

DDMMYY=${1-'today'}
HHMMSS=${2-'09:00'}

# Returns:
# 0 - valid date
# 1 - Future date
# 2 - Invalid date
check_date()
{
   { DATE="`date -d "$1 $2" +%s`" || return 2
     NOW="`date +%s`"
   } >>/dev/null 2>&1
     [[ "$DATE" -lt "$NOW" ]] && return 0 || return 1
}

# Returns: 
# Exit code of `date`
# Sets FMT_DATE to formatted date "DD-Mon-YY HH:MM:SS"
get_fmt_date()
{
     FMT_DATE="`date -d "$1 $2" +"%d-%b-%y %T"`"
     return $?
}

check_date $DDMMYY $HHMMSS || exit $?
get_fmt_date $DDMMYY $HHMMSS
echo $FMT_DATE
Example usage:

Code:
[jc@centosvm ~]$ ./test_date.sh 
30-Jul-08 09:00:00
[jc@centosvm ~]$ ./test_date.sh 2/29/08
29-Feb-08 09:00:00
[jc@centosvm ~]$ ./test_date.sh 2/29/08 1031
[jc@centosvm ~]$ ./test_date.sh 2/29/08 10:31
29-Feb-08 10:31:00
[jc@centosvm ~]$ ./test_date.sh 2/29/09
[jc@centosvm ~]$

Last edited by jcookeman; 07-30-2008 at 11:37 AM.
 
Old 07-30-2008, 01:09 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 57
Nice.

This line (and others like it) is a little convoluted:

DATE="`date -d "$1 $2" +%s`"

Use either of the simpler:

DATE=$(date -d "$1 $2" +%s)
DATE=`date -d "$1 $2" +%s`

Consider placings braces around your compound logic groupings for clarity (who remembers precedence?)

{ [[ "$DATE" -lt "$NOW" ]] && return 0 ; } || return 1

You can't really append to the bit bucket:

>>/dev/null 2>&1 # appending is superfluous

> /dev/null 2>&1 # simple redirection is sufficient
 
  


Reply

Tags
bash, date


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling I/O redirections in my shell. Sebouh Programming 2 04-10-2007 01:31 PM
file handling shell script ankit_mcgill Programming 6 04-29-2006 12:32 AM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 08:34 AM
Shell question: moving files into subdirectories by dates timothee Linux - Newbie 5 12-20-2005 10:14 AM
Need help w/ shell handling 3 commands/2 pipes sptchamp Programming 3 07-15-2004 09:37 PM


All times are GMT -5. The time now is 03:55 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration