Skip to contentSkip to conversation

Special date/cal needs eg. "thrid friday of july"

DISCUSSION Martin Klier 5 participants 5 messages Jan 11, 2007 – Jan 12, 2007

lore
Share & tools
lore
#0 lore raw

Hi Linux Admins,

is there a command to get something like "thrid friday of july" or "second wednesday each month"? I crossread the manuals for date and gcal, but it seems to be impossible. Next thing I found was gcal, with "--period-of-fixed-dates", but I have not been able to get useful results, and
date -d "35 tuesday" (35th tuesday of a year), but I have not been able to limit it to months nor selecting the year (by the way, I do not need it).

Has somebody experience with this one, and can you give me a hint where to look, or even an example?

Thanks a lot in advance,

Signature · 7 lines
Mit freundlichen Grüßen

i.A. Martin Klier
Systemadministration / Datenbanken
-----------------------------------------------------------------
A.T.U Auto-Teile-Unger
Handels GmbH & Co. KG
#1 lore raw

This method may be out of date, but if you add the CPAN for Date::Calc to a perl script you can write a script that will manipulate a given date to add / subtract dates and give days of the week, months etc as output or viseversa

David

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Martin Klier
Sent: Thursday, January 11, 2007 10:24 AM
To: [email protected]
Subject: Special date/cal needs eg. "thrid friday of july"

Hi Linux Admins,

is there a command to get something like "thrid friday of july" or "second wednesday each month"? I crossread the manuals for date and gcal, but it seems to be impossible. Next thing I found was gcal, with "--period-of-fixed-dates", but I have not been able to get useful results, and
date -d "35 tuesday" (35th tuesday of a year), but I have not been able to limit it to months nor selecting the year (by the way, I do not need it).

Has somebody experience with this one, and can you give me a hint where to look, or even an example?

Thanks a lot in advance,

Signature · 11 lines
Mit freundlichen Grüßen

i.A. Martin Klier
Systemadministration / Datenbanken
-----------------------------------------------------------------
A.T.U Auto-Teile-Unger
Handels GmbH & Co. KG
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to Martin Klier

Hi

An example relative to this week:
ben@chimay:~ $ date -d "tuesday + 34 weeks" [will give the next tuesday 34 weeks in the future]

An example relative to now
ben@chimay:~ $ date -d "now + 35 seconds - 3 days + 1 year" [will give a dummy date in year 2008, based on "now" and some + and - ]

Other example
ben@chimay:~ $ date -d "3 days ago"
[no comment]

An absolute date:
ben@chimay:~ $ date -d "2007 1 jan"

could not find out how to mix absolute dates and '+' and '-' HTH

Quoted 10 lines · Martin Klier

Hi Linux Admins,

is there a command to get something like "thrid friday of july" or "second wednesday each month"? I crossread the manuals for date and gcal, but it seems to be impossible. Next thing I found was gcal, with "--period-of-fixed-dates", but I have not been able to get useful results, and
date -d "35 tuesday" (35th tuesday of a year), but I have not been able to limit it to months nor selecting the year (by the way, I do not need it).

Quoted 4 lines

Has somebody experience with this one, and can you give me a hint where to look, or even an example?

Thanks a lot in advance,

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Martin Klier

Martin,

This script should give you enough information to figure out how to do what you want. It is not intended to be used as written; it doesn't do any syntax checking.

Throw the script in a file, mark it executable, and pass the date you're looking, in the same format as third friday of july, on the command line. (e.g. ./some_file third friday of july)

#!/bin/sh

# Parse input
week=$1
weekday=$2
month=$4
case "$week" in
[Ff][Ii][Rr][Ss][Tt])

    skip_weeks=0;
    ;;
  [Ss][Ee][Cc][Oo][Nn][Dd])
    skip_weeks=1;
    ;;
  [Tt][Hh][Ii][Rr][Dd])
    skip_weeks=2;
    ;;
  [Ff][Oo][Uu][Rr][Tt][Hh])
    skip_weeks=3;
    ;;
  [Ff][Ii][Ff][Tt][Hh])
    skip_weeks=4;
    ;;

esac

# Convert the weekday to a number ( 1 = Monday, 2 = Tuesday, ... ) weekday_no=$( date -d "$weekday" +%u )

# Qualify the month with a year and a day (the first) for

# illustrative purposes
first_of_month=$( date -I -d "${month} 1" )

# Now that we're done formating the input, these three lines do the

# real work.

# Find the weekday number of the first day of the month
# (again 1 = Monday, ... )
first_weekday_no=$( date -d "${first_of_month} ${skip_weeks} weeks" +%u )

# Do some math; difference between desired weekdays + 7 then mod by 7. let skip_days=(7+weekday_no-first_weekday_no)%7

# Use date to print the date. You can format if desired. date -d "${first_of_month} ${skip_weeks} weeks ${skip_days} days"

Doug Knight

Quoted 14 lines · Martin Klier

Hi Linux Admins,

is there a command to get something like "thrid friday of july" or "second wednesday each month"? I crossread the manuals for date and gcal, but it seems to be impossible. Next thing I found was gcal, with "--period-of-fixed-dates", but I have not been able to get useful results, and
date -d "35 tuesday" (35th tuesday of a year), but I have not been able to limit it to months nor selecting the year (by the way, I do not need it).

Has somebody experience with this one, and can you give me a hint where to look, or even an example?

Thanks a lot in advance,

#4 lore raw
In reply to Martin Klier

Hi,

Quoted 10 lines · Martin Klier

Hi Linux Admins,

is there a command to get something like "thrid friday of july" or "second wednesday each month"? I crossread the manuals for date and gcal, but it seems to be impossible. Next thing I found was gcal, with "--period-of-fixed-dates", but I have not been able to get useful results, and
date -d "35 tuesday" (35th tuesday of a year), but I have not been able to limit it to months nor selecting the year (by the way, I do not need it).

Quoted 4 lines

Has somebody experience with this one, and can you give me a hint where to look, or even an example?

Thanks a lot in advance,

I love the --date GNU extension, but I tried a number of different syntaxes to get it to do what you wanted without any luck. So I thought a one line pipe would be fun:

cal 7 2007 | awk -F. 'NR > 2{print substr($1,5*3+1,2)}' | grep '[0-9]' | awk 'NR == 3 {print $1}'

You set the day you're interested in through the first integer of the second parameter of the substr call of the awk command. 0=Sunday (0*3+1), 1=Monday (1*3+1) etc. You set the week number with the 'NR == n' part of the final awk. You set the month and year with the parameters to cal. It invokes awk twice which if used in a very tight loop could be a problem. It would be easy to merge the awk | grep | awk into one awk command, but it starts to look more like program code on a single line rather than a series of simple(-ish) commands.

Cheers

Adam