示例#1
0
文件: logcut.c 项目: yasuoka/logcut
static int
read_time(int fd, const char *fmt, time_t * t)
{
	int		 sz, web_log;
	struct tm	 tm;
	char		*bufp, buf[40];

	web_log = (fmt == APACHE_FMT)? 1 : 0;
	memset(&tm, 0, sizeof(tm));
	tm.tm_mon = -1;
	if ((sz = read(fd, buf, sizeof(buf) - 1)) < 0)
		err(1, "read");
	if (sz == 0)
		return (-1);
	buf[sz] = '\0';
	if (web_log && (bufp = strchr(buf, '[')) != NULL)
		bufp++;
	else
		bufp = buf;
	if (strptime(bufp, fmt, &tm) == NULL)
		return 0;
	adjust_tm(&tm);

	*t = mktime(&tm);

	return (1);
}
示例#2
0
文件: misc.c 项目: rdebath/sgt
/* ----------------------------------------------------------------------
 * With the aid of the above two functions, _this_ completely ANSI-
 * compliant function is the equivalent of mktime() using GMT. In
 * other words, it's the inverse of gmtime().
 */
static time_t mktimegm(struct tm *tm) {
    tm->tm_isdst = 0;		       /* GMT is never daylight-saving */
    /*
     * If local time is an hour ahead of GMT, then calling mktime
     * on tm will give us a result one hour _earlier_ than what we
     * want, so we would have to add an hour to tm before calling
     * mktime.
     */
    adjust_tm(tm, tzshift());
    return mktime(tm);
}
示例#3
0
文件: xttotm.c 项目: vocho/openqnx
struct tm *_Ttotm(struct tm *t, time_t secsarg, int isdst)
#endif
	{	/* convert scalar time to time structure */
#ifdef __QNX__
	long tzoffdst;
	long tzoff;
	static struct tm ts;
	uint64_t lsecsarg;
	uint64_t secs;

	lsecsarg = _TBIAS + (uint64_t)secsarg;
	tzoffdst = _Tzoff_dst();
	if (t == 0)
		t = &ts;
	t->tm_isdst = isdst;
	switch (ct) {
		case __GMTIME_CALL:
			t = adjust_tm(lsecsarg, t);
			t->tm_zone = tzname[(0 < t->tm_isdst ? 1 : 0)];
			t->tm_gmtoff = 0;
			break;
		case __LOCALTIME_CALL:
			tzoff = _Tzoff();
			secs = lsecsarg + tzoff;
			t = adjust_tm(secs, t);
			t->tm_isdst = _Isdst(t, tzoff, tzoffdst); 
			if (t->tm_isdst > 0) { // 
				secs = lsecsarg + tzoffdst;
				t = adjust_tm(secs, t);
				t->tm_gmtoff = tzoffdst;
			}
			else 
				t->tm_gmtoff = tzoff;
			t->tm_zone = tzname[(0 < t->tm_isdst ? 1 : 0)];
			break;
		case __MKTIME_CALL:
			tzoff = _Tzoff();
			t = adjust_tm(lsecsarg, t);
			if (isdst == 0) { // isdst == 0
				t->tm_isdst = isdst;
			}
			else {
				int  dst_state;
				if (_Getrules(&dst_state) == NULL)
					t->tm_isdst = 0;
				else	if (isdst <= 0)
					t->tm_isdst = _Isdst(t, 0, 0);
			}

			/* set up proper zone */
			t->tm_zone = tzname[(0 < t->tm_isdst ? 1 : 0)];
			if (t->tm_isdst > 0) // dst is on
				t->tm_gmtoff = tzoffdst;
			else 
				t->tm_gmtoff = tzoff;
			break;
		default:
			break;
	}	
 	return(t);
#else
	int year;
	long days;
	unsigned long secs;

	secsarg += _TBIAS;	/* changed to (wraparound) time since 1 Jan 1900 */
	if (t == 0)
		t = _TLS_DATA_PTR(ts);
	t->tm_isdst = isdst;
	for (secs = (unsigned long)secsarg; ;
		secs = (unsigned long)secsarg + 3600)
		{	/* loop to correct for DST */
		days = secs / 86400;
		t->tm_wday = (days + WDAY) % 7;
		 {	/* determine year */
		long i;

		for (year = days / 365;
			days < (i = _Daysto(year, 0) + 365L * year); )
			--year;	/* correct guess and recheck */
		days -= i;
		t->tm_year = year;
		t->tm_yday = days;
		 }
		 {	/* determine month */
		int mon;
		const short *pm = MONTAB(year);

		for (mon = 12; days < pm[--mon]; )
			;
		t->tm_mon = mon;
		t->tm_mday = days - pm[mon] + 1;
		 }
		secs = secs % 86400;
		t->tm_hour = secs / 3600;
		secs %= 3600;
		t->tm_min = secs / 60;
		t->tm_sec = secs % 60;
		if (0 <= t->tm_isdst || (t->tm_isdst = _Isdst(t)) <= 0)
			return (t);	/* loop only if <0 => 1 */
		}
#endif
		}