2nd of 2 forwarded messages from Bradley White: strtotm

Arthur David Olson ado
Tue Jun 9 18:44:46 UTC 1992

The attached comes from Bradley White, bww+ at cs.cmu.edu.
Your comments will be appreciated!


I often miss an at-least-pseudo-standard means of converting a
reasonable string date/time specification into a "struct tm", and,
from the number of requests on different newsgroups, it appears
others do too.  Perhaps adding one to the "tz" package might be
a reasonable thing to do?

If you concur, and are open to suggestions or want to open it up
for discussion, here are some random thoughts on the interface
off the top of my head.  It's undoubtedly deficient.

Then again, perhaps you have considered all of this before, and
decided against it?

static int
_strtotm(str, flags, np, tp)
	char *str;
	unsigned *flags;
	struct tm *np;
	struct tm *tp;
	 * decode str into tp using (if necessary) flags and np;
	 * undoubtedly requires yacc and/or lex to do a good job
	 * return 0 on success, non-zero (error codes?) on error
	 * The only flags I can currently think of are:
	 *	IN:
	 *		XX_DEFPAST	default to most recent past
	 *				matching time, rather than
	 *				most near future matching time,
	 *				for incomplete specifications
	 *		XX_DEFDDMM	DD/MM/YY instead of MM/DD/YY
	 *				if ambiguous
	 *		XX_DEFYYMM	YY/MM/DD instead of above
	 *				if ambiguous
	 *	OUT:
	 *		XX_DEFTIME	no time specified, used "now" time
	 *		XX_DEFZONE	no zone specified, used "now" zone
	return XX_EUNIMPL;

struct tm *
strtotm(str, flags, np, tp)
	char *str;
	unsigned *flags;
	struct tm *np;		/* "now" for relative times */
	struct tm *tp;		/* space for result, malloc if NULL */
	struct tm nowtm, tmptm;

	if (np == 0) {
		time_t now = time((time_t *) 0);
		np = localtime(&now);
	nowtm = *np;
	if (_strtotm(str, flags, &nowtm, &tmptm) != 0)
		return 0;
#if 0
	 * I'd like to do this to check/canonicalize the struct tm,
	 * but it is really only appropriate if `tmptm' is in the
	 * local timezone, right???  Perhaps timeoff()???
	if (mktime(&tmptm) == -1)
		return 0;
	if (tp == 0 && (tp = malloc(sizeof *tp)) == 0)
		return 0;
	*tp = tmptm;
	return tp;

More information about the tz mailing list