Example #1
0
/* strdate_r and strtime_r must be at least BUF_DATE AND BUF_TIME in size */
void move_time(const char *fromtz, const char *totz, const char *strdate, const char *strtime, char *strdate_r, char *strtime_r)
{
    if (strtime && strlen(strtime)) {
        time_t t;
        struct tm tm;

        if (strdate && strlen(strdate)) {
            tm_read_strdate(&tm, strdate);
        } else {
            time(&t);
            localtime_r(&t, &tm);
        }
        tm_read_strtime(&tm, strtime);

        if ((t = tmtz_mktime(&tm, fromtz)) == -1) {
            fprintf(stderr, "tmtz_mktime failed in move_time\n");
            return;
        }
        localtime_tz(&t, totz, &tm);
        strftime(strtime_r, BUF_TIME, "%H:%M", &tm);
        strftime(strdate_r, BUF_DATE, "%Y-%m-%d", &tm);
    } else {
        *strtime_r = '\0';
        strncpy(strdate_r, strdate, BUF_DATE);
        strdate_r[BUF_DATE - 1] = '\0';
    }
    //fprintf(stderr, "move_time -- fromtz: %s, totz: %s\n", fromtz, totz);
}
Example #2
0
void 
Time::setToNow()
{
    time_t seconds;
    time(&seconds);
    localtime_tz(&seconds, &(this->t), this->timezone);
}
Example #3
0
void reconcile_time(ReconcileTime rc, struct tm *t1, const char *tz1, struct tm *t2, const char *tz2, long *elapsed)
{
    time_t t1_t, t2_t;

    switch (rc) {
    case RECONCILE_TIME_BEGIN:
        t2_t = tmtz_mktime(t2, tz2);
        t1_t = t2_t - *elapsed;
        localtime_tz(&t1_t, tz1, t1);
        break;
    case RECONCILE_TIME_END:
        t1_t = tmtz_mktime(t1, tz1);
        t2_t = t1_t + *elapsed;
        localtime_tz(&t2_t, tz2, t2);
        break;
    case RECONCILE_TIME_ELAPSED:
        t1_t = tmtz_mktime(t1, tz1);
        t2_t = tmtz_mktime(t2, tz2);
        *elapsed = t2_t - t1_t;
        break;
    }
}
Example #4
0
File: pdc.c Project: OpenDMM/zvbi
/**
 * @param begin The begin of the validity of the PTY will be stored
 *   here.
 * @param end The end of the validity of the PTY will be stored here.
 * @param last_transm The last time when a program ID with the PTY
 *   in question was broadcast by the network.
 * @param tz A time zone name in the same format as the TZ environment
 *   variable. If @c NULL the current value of TZ will be used.
 *
 * This function calculates the validity time window of a Program Type
 * (PTY) code according to EN 300 231. That is the time window where a
 * network can be expected to broadcast another program with the same
 * PTY, approximately up to four weeks after its last
 * transmission. When the PTY is a series code (>= 0x80) and not
 * transmitted again before @a end, the network may assign the code to
 * another series.
 *
 * @a tz is the time zone of the intended audience of the program.
 * Ideally the time zone would be specified as a geographic area like
 * "Europe/London", such that the function can determine if
 * daylight-saving time is in effect at @a time or at the end of the
 * validity time window. See the documentation of the localtime()
 * function and the TZ environment variable for details. If no time
 * zone name is available "UTC" should be specified, the returned
 * @a end time may be off by one hour in this case.
 *
 * @returns
 * On error the function returns @c FALSE and @a *begin and @a *end
 * remain unchanged:
 * - @a tz is empty or contains an equal sign '='.
 * - The @a end time cannot be represented as a time_t value
 *   (December 2037 is closer than you think!).
 * - Insufficient memory was available.
 *
 * @since 0.2.34
 *
 * @bug
 * This function is not thread safe unless @a tz is @c NULL.
 * That is a limitation of the C library which permits the conversion
 * of a broken-down time in an arbitrary time zone only by setting
 * the TZ environment variable. The function may also fail to restore
 * the value of TZ if insufficient memory is available.
 */
vbi_bool
vbi_pty_validity_window		(time_t *		begin,
				 time_t *		end,
				 time_t			last_transm,
				 const char *		tz)
{
	char *old_tz;
	struct tm tm;
	time_t stop;
	int saved_errno;

	assert (NULL != begin);
	assert (NULL != end);

	if (NULL != tz && 0 == strcmp (tz, "UTC")) {
		vbi_bool success;

		success = pty_utc_validity_window (begin, end,
						   last_transm);
#if 2 == VBI_VERSION_MINOR
		errno = 0;
#endif
		return success;
	}

	if (unlikely (!localtime_tz (&tm, &old_tz, last_transm, tz)))
		goto failed;

	tm.tm_mday += 4 * 7 + 1;
	tm.tm_hour = 4;
	tm.tm_min = 0;
	tm.tm_sec = 0;
	tm.tm_isdst = -1; /* unknown */

	stop = mktime (&tm);
	if (unlikely ((time_t) -1 == stop)) {
		saved_errno = errno;

		if (unlikely (!restore_tz (&old_tz, tz)))
			return FALSE;

#if 3 == VBI_VERSION_MINOR
		errno = saved_errno;
#else
		errno = 0;
#endif
		return FALSE;
	}

	if (unlikely (!restore_tz (&old_tz, tz)))
		goto failed;

	*begin = last_transm;
	*end = stop;

	return TRUE;

 failed:
#if 2 == VBI_VERSION_MINOR
	errno = 0;
#endif
	return FALSE;
}
Example #5
0
File: pdc.c Project: OpenDMM/zvbi
/**
 * @param pil Program Identification Label (PIL) to convert.
 * @param start The most recently announced start time of the
 *   program. If zero the current system time will be used.
 * @param tz A time zone name in the same format as the TZ environment
 *   variable. If @c NULL the current value of TZ will be used.
 *
 * This function converts a PIL to a time_t in the same manner
 * localtime() converts a broken-down time to time_t.
 *
 * Since PILs do not contain a year field, the year is determined from
 * the @a start parameter, that is the most recently announced start
 * time of the program or "AT-1" in EN 300 231 parlance. If @a pil
 * contains a month more than five months after @a start, @a pil is
 * assumed to refer to an earlier date than @a start.
 *
 * @a pil is assumed to be a time in the time zone @a tz. @a start
 * will be converted to a local time in the same time zone to
 * determine the correct year.
 *
 * Teletext packet 8/30 format 2, VPS and DVB PDC descriptors give a
 * PIL relative to the time zone of the intended audience of the
 * program. Ideally the time zone would be specified as a geographic
 * area like "Europe/London", such that the function can determine
 * the correct offset from UTC and if daylight-saving time is in
 * effect at the specified date. See the documentation of the
 * localtime() function and the TZ environment variable for details.
 *
 * XDS Current/Future Program ID packets give a PIL relative to UTC.
 * Just specify time zone "UTC" in this case.
 *
 * @returns
 * The PIL as a time_t, that is the number of seconds since
 * 1970-01-01 00:00 UTC. On error the function
 * returns (time_t) -1:
 * - @a pil does not contain a valid date or time. February 29th is
 *   a valid date only if the estimated year is a leap year.
 * - @a tz is empty or contains an equal sign '='.
 * - @a start is zero and the current system time could not be
 *   determined.
 * - The time specified by @a pil, @a start and @a tz cannot be
 *   represented as a time_t value.
 * - Insufficient memory was available.
 *
 * @since 0.2.34
 *
 * @bug
 * This function is not thread safe unless @a tz is @c NULL. That is
 * a limitation of the C library which permits the conversion of a
 * broken-down time in an arbitrary time zone only by setting the TZ
 * environment variable. The function may also fail to restore the
 * value of TZ if insufficient memory is available.
 */
time_t
vbi_pil_to_time			(vbi_pil		pil,
				 time_t			start,
				 const char *		tz)
{
	struct tm tm;
	char *old_tz;
	time_t result;
	int saved_errno;

	if (unlikely (!vbi_pil_is_valid_date (pil))) {
#if 3 == VBI_VERSION_MINOR
		errno = VBI_ERR_INVALID_PIL;
#else
		errno = 0;
#endif
		return (time_t) -1;
	}

	if (NULL != tz && 0 == strcmp (tz, "UTC")) {
		time_t t;

		t = valid_pil_lto_to_time (pil, start,
					   /* seconds_east */ 0);
#if 2 == VBI_VERSION_MINOR
		errno = 0;
#endif
		return t;
	}

	if (unlikely (!localtime_tz (&tm, &old_tz, start, tz))) {
#if 2 == VBI_VERSION_MINOR
		errno = 0;
#endif
		return (time_t) -1;
	}

	if (unlikely (!tm_mon_mday_from_pil (&tm, pil))) {
		saved_errno = EOVERFLOW;
		goto failed;
	}

	if (unlikely (!tm_leap_day_check (&tm))) {
		saved_errno = VBI_ERR_INVALID_PIL;
		goto failed;
	}

	tm.tm_hour = VBI_PIL_HOUR (pil);
	tm.tm_min = VBI_PIL_MINUTE (pil);
	tm.tm_sec = 0;

	tm.tm_isdst = -1; /* unknown */

	result = mktime (&tm);
	if (unlikely ((time_t) -1 == result))
		goto failed;

	if (unlikely (!restore_tz (&old_tz, tz))) {
#if 2 == VBI_VERSION_MINOR
		errno = 0;
#endif
		return (time_t) -1;
	}

	return result;

 failed:
	if (unlikely (!restore_tz (&old_tz, tz))) {
#if 2 == VBI_VERSION_MINOR
		errno = 0;
#endif
		return (time_t) -1;
	}

#if 3 == VBI_VERSION_MINOR
	errno = saved_errno;
#else
	errno = 0;
#endif
	return (time_t) -1;
}
Example #6
0
File: pdc.c Project: OpenDMM/zvbi
static vbi_bool
valid_pil_validity_window	(time_t *		begin,
				 time_t *		end,
				 vbi_pil		pil,
				 time_t			start,
				 const char *		tz)
{
	char *old_tz;
	struct tm tm;
	struct tm tm2;
	time_t stop;
	int saved_errno;

	/* EN 300 231 Section 9.3 and Annex F. */

	old_tz = NULL;

	if (NULL != tz && 0 == strcmp (tz, "UTC")) {
		return valid_pil_lto_validity_window
			(begin, end, pil, start, /* seconds_east */ 0);
	}

	if (unlikely (!localtime_tz (&tm, &old_tz, start, tz)))
		return FALSE;

	if (unlikely (!tm_mon_mday_from_pil (&tm, pil))) {
		saved_errno = EOVERFLOW;
		goto failed;
	}

	if (unlikely (!tm_leap_day_check (&tm))) {
		/* Annex F: "Invalid days - indefinite time window". */
		if (!restore_tz (&old_tz, tz))
			return FALSE;
		*begin = TIME_MIN;
		*end = TIME_MAX;
		return TRUE;
	}

	tm.tm_hour = 0;
	tm.tm_min = 0;
	tm.tm_sec = 0;
	tm.tm_isdst = -1; /* unknown */

	tm2 = tm;

	if (VBI_PIL_HOUR (pil) < 4) {
		--tm.tm_mday;
		tm.tm_hour = 20;
	}

	start = mktime (&tm);
	if (unlikely ((time_t) -1 == start))
		goto failed;

	tm2.tm_mday += 1;
	tm2.tm_hour = 4;

	stop = mktime (&tm2);
	if (unlikely ((time_t) -1 == stop))
		goto failed;

	if (unlikely (!restore_tz (&old_tz, tz)))
		return FALSE;

	*begin = start;
	*end = stop;

	return TRUE;

 failed:
	if (unlikely (!restore_tz (&old_tz, tz)))
		return FALSE;

	errno = saved_errno;
	return FALSE;
}
Example #7
0
void 
Time::switchTimezone(const char* timezone)
{
    time_t seconds = mktime_tz(&(this->t), this->timezone);
    localtime_tz(&seconds, &(this->t), timezone);
}
Example #8
0
void 
Time::set(int64_t millis)
{
    time_t seconds = millis / 1000;
    localtime_tz(&seconds, &(this->t), this->timezone);
}