need "yy-ddd-hh:mm:ss ==> (time_t) clock" converter

Erik Naggum enag at ifi.uio.no
Tue Feb 26 12:01:46 AEST 1991


In article <790 at saxony.pa.reuter.COM>, Dave Gillett writes:
| In article <1991Feb18.133937.28469 at fivegl.co.nz>, Hugh Grierson writes:
| >Maintainability????  How often *do* you have to change the number
| >of days in a month?????  (Sept 1752 notwithstanding)
| >Readability? A one-line comment...

| Sorry.  If I'm trying to find a defect in the program's date-
| handling, I can verify by eye the {31, 28, ... } form.  Even with a
| comment, it may take several moments to understand what the {31, 59,
| ... } form is trying to do -- especially if I didn't write this
| piece of code (which happens, you know).

Hey, that's what comments are for.

| And it will take several minutes to verify that these magic numbers
| are in fact calculated correctly, a waste of both the author's and
| the maintainer's time and effort.  If your algorithm, for
| efficiency's sake, really must use the numbers in this form, then
| the table should be built by the machine at run time, and the
| numbers in the source should be the familiar ones that anyone can
| easily verify.

I can't believe this.  At run-time, you do useful things, not table
generation.  You can easily do it pre-compile-time, once.  You write a
few shell commands to do that, or some simple-minded elisp code, and
include the code as a comment:

/*
 * cumulative days into year at beginning of month
 */
int cumulative_yday[] = {
	/*
	 * This list was generated by piping these three lines into
	 * /bin/sh.  Repeat with "3!!sh<CR>" in vi.
	 	days=0
	 	for i in 31 28 31 30 31 30 31 31 30 31 30 31
	 	do echo -n $days,; days=`expr $days + $i`; done
	 */
	0,31,59,90,120,151,181,212,243,273,304,334,
};

Ok, so this is UNIX-centric par excellence.

/*
 * cumulative days into year at beginning of month
 */
int cumulative_yday[] = {
	/*
	 * This list was generated by eval'ing this code (C-X C-E):
	   (let
	       ((mday '(31 28 31 30 31 30 31 31 30 31 30 31))
		(ydays 0))
	     (while mday
	       (insert (int-to-string ydays) ",")
	       (setq ydays (+ ydays (car mday))
		     mday (cdr mday))))
	 */
	0,31,59,90,120,151,181,212,243,273,304,334,
};

Ok, this is GNU Emacs-centric.  So sue me.

If you don't have quality editors, you should get one before you do
anything else.

This code was the direct result of a desire to amend a perceived lack
of tool-building initiative.  I also have flint stones to make fire,
if you're interested.  The little tool built above is not necessarily
the best possible tool, but it should give an indication of the desire
to minimize the work you would do.  (I spent 45 seconds writing the
shell lines, and a few minutes fumbling with the simple elisp code.)

Morale: Let the machines do the chores.

--
[Erik Naggum]					     <enag at ifi.uio.no>
Naggum Software, Oslo, Norway			   <erik at naggum.uu.no>



More information about the Comp.lang.c mailing list