Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to read a string from a file and then convert it into a date but for some reason it's failing. I'm actually modifying a GeekTool Geeklet that's not working at the moment. It reads a file locally and pulls out a target date that is then used in a countdown clock. The line of code that is failing is:

TARGET=$(date -j -f %D_%T $1 +%s)

I know that I can hardcode the value in the script using -v but I want to work out why this line isn't working. I can't seem to find any refs to converting a string from a file into a date object. I also can't find refs to -j or -f flags...

[edit] Here's the full script:

#!/bin/bash
# Homework countdown
# BETA - I will update this when I get more time
function countdown
{
CURRENT=$(date +%s)
TARGET=$(date -j -f %D_%T $1 +%s)
LEFT=$((TARGET-CURRENT))
WEEKS=$((LEFT/604800))
DAYS=$(( (LEFT%604800)/86400))
HOURS=$(( (LEFT%86400)/3600))
MINS=$(( (LEFT%3600)/60))
SECS=$((LEFT%60))

lblWEEKS="Weeks"
lblDAYS="Days"

if [ $DAYS == 1 ]
then
lblDAYS="Day"
fi

if [ $WEEKS == 1 ]
then
lblWEEKS="Week"
fi

if [ $HOURS -lt 10 ]
then
    HOURS=0$HOURS
fi  

if [ $MINS -lt 10 ]
then
    MINS=0$MINS
fi


if [ $SECS -lt 10 ]
then
    SECS=0$SECS
fi

echo $1
echo $TARGET
echo $2 $WEEKS Weeks, $DAYS $lblDAYS $HOURS:$MINS:$SECS
# Optional extra line between timers
echo
}
DATES=( $( cat /Users/Shared/Deadlines.txt ) )
# Even numbered indices are names, odd numbered indices are dates


if [ ${#DATES[@]} == 0 ]
then
echo "No Deadlines!"
return
fi

for (( i = 0 ; i < ${#DATES[@]} ; i+=2 ))
do
countdown ${DATES[i+1]} ${DATES[i]}
done

And the file simply contains: test: 17/05/2011_12:00

share|improve this question
    
Why don't you show the pertinent content of the file and the desired output? -j is not present in GNU date, what date are you using? –  enzotib Aug 9 '11 at 11:53

1 Answer 1

Looks like the script was written for FreeBSD.

-f input_fmt: Use input_fmt as the format string to parse the new_date provided rather than using the default [[[[[cc]yy]mm]dd]HH]MM[.ss] format. Parsing is done using strptime(3).
-j: Do not try to set the date. This allows you to use the -f flag in addition to the + option to convert one date format to another.

These are nonstandard extensions to the standard date command, which can only format the current date. Under Linux (or Cygwin) or any other system with GNU date, you can use the -d option if you're willing to pass a different date format:

TARGET=$(date -d "$1" +%s)

and call the script as e.g. test 2011/05/17 12:00. For fancier parsing, preprocess the string in the shell first or call Perl or Python:

TARGET=$(IFS=/_:; set $1; date +%S $(($2*10000000000+$1*100000000+
                                      $4*1000000+$5*10000+$1)))
TARGET=$(perl -l -e '($day,$month,$year,$h,$m) = split(/[^0-9]+/, $ARGV[0]); print mktime(0, $m, $h, $day, $month, $year-1900)' "$1")
TARGET=$(python -c '
    import sys, time;
    print time.strftime("%s", time.strptime(sys.argv[1], "%d/%m/%Y_%H:%M"))
   '            "$1")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.