Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #12082

Re: Only one Friday 13th coming in 2016

Path csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.glorb.com!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Only one Friday 13th coming in 2016
Date Tue, 22 Dec 2015 08:16:28 -0500
Lines 37
Approved bug-bash@gnu.org
Message-ID <mailman.375.1450790195.843.bug-bash@gnu.org> (permalink)
References <slrnn7g4tl.2r3t.knock_yourself_out@vps.jonz.net> <20151221151338.GW27325@eeg.ccf.org> <20151222010544.GA21298@linda> <1450753456.7845.5.camel@16bits.net>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Content-Transfer-Encoding 8bit
X-Trace usenet.stanford.edu 1450790195 8661 208.118.235.17 (22 Dec 2015 13:16:35 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org
To Ángel González <angel@16bits.net>
Envelope-to bug-bash@gnu.org
Content-Disposition inline
In-Reply-To <1450753456.7845.5.camel@16bits.net>
User-Agent Mutt/1.4.2.3i
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From 139.137.100.1
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.14
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:12082

Show key headers only | View raw


On Tue, Dec 22, 2015 at 04:04:16AM +0100, Ángel González wrote:
> Aren't you making things more complex than needed, with so much pipes
> and awk?
> 
> date(1) is your friend:
> 
> For instance:
>  $ for y in {1900..2199} ; do echo -n "$y "; for m in {1..12}; do date +%A -d $y-$m-13; done | grep -c Friday ; done
> 
> shows there are between 1 and 3 Fridays per year.

This is the most obvious approach, but it does a fair amount of forking.
Also, you're relying on GNU date.  There's no portable way to do simple
date lookups in a shell script, unfortunately.

A pure-bash approach that avoids forking might look something like:

t=946702800	# Start at Sat Jan  1 12:00:00 EST 2000
endyear=2036

while true; do
  printf -v year '%(%Y)T' "$t"
  ((year > endyear)) && break
  printf -v day  '%(%d)T' "$t"
  printf -v dow  '%(%w)T' "$t"
  if [[ $day = 13 && $dow = 5 ]]; then
    printf -v month '%(%m)T' "$t"
    echo "$year-$month-$day"
  fi
  ((t += 86400))
done

But just because it doesn't fork, doesn't mean it's *fast*.  Bash is so
slow at everything. :(  Your one-fork-per-month loop (plus one fork per
year) might end up being much faster than my zero-forks-per-day loop.
Mine is portable, though.

Back to gnu.bash.bug | Previous | NextNext in thread | Find similar


Thread

Re: Only one Friday 13th coming in 2016 Greg Wooledge <wooledg@eeg.ccf.org> - 2015-12-22 08:16 -0500
  Re: Only one Friday 13th coming in 2016 Ralf Goertz <me@myprovider.invalid> - 2015-12-22 14:43 +0100

csiph-web