text
stringlengths
0
357
struct tm mytm;
yourtm = *tmp;
saved_seconds = yourtm.tm_sec;
yourtm.tm_sec = 0;
/*
** Calculate the number of magnitude bits in a time_t
** (this works regardless of whether time_t is
** signed or unsigned, though lint complains if unsigned).
*/
for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
;
/*
** If time_t is signed, then 0 is the median value,
** if time_t is unsigned, then 1 << bits is median.
*/
if(bits > 40) bits = 40;
t = (t < 0) ? 0 : ((time_t) 1 << bits);
for ( ; ;) {
gmtime_r(&t, &mytm);
dir = tmcomp(&mytm, &yourtm);
if (dir!=0) {
if (bits-- < 0) {
return DC_INVALID_TIMESTAMP;
}
if (bits < 0)
--t;
else if (dir > 0)
t -= (time_t) 1 << bits;
else t += (time_t) 1 << bits;
continue;
}
break;
}
t += saved_seconds;
return t;
}
time_t dc_timestamp_from_date(struct mailimf_date_time * date_time) /* from mailcore2 */
{
struct tm tmval;
time_t timeval = 0;
int zone_min = 0;
int zone_hour = 0;
memset(&tmval, 0, sizeof(struct tm));
tmval.tm_sec = date_time->dt_sec;
tmval.tm_min = date_time->dt_min;
tmval.tm_hour = date_time->dt_hour;
tmval.tm_mday = date_time->dt_day;
tmval.tm_mon = date_time->dt_month - 1;
if (date_time->dt_year < 1000) {
/* workaround when century is not given in year */
tmval.tm_year = date_time->dt_year + 2000 - 1900;
}
else {
tmval.tm_year = date_time->dt_year - 1900;
}
timeval = mkgmtime(&tmval);
if (date_time->dt_zone >= 0) {
zone_hour = date_time->dt_zone / 100;
zone_min = date_time->dt_zone % 100;
}
else {
zone_hour = -((- date_time->dt_zone) / 100);
zone_min = -((- date_time->dt_zone) % 100);
}
timeval -= zone_hour * 3600 + zone_min * 60;
return timeval;
}
long dc_gm2local_offset(void)
{
/* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime.
the function may return nagative values. */
time_t gmtime = time(NULL);
struct tm timeinfo = {0};
localtime_r(&gmtime, &timeinfo);
return timeinfo.tm_gmtoff;
}
char* dc_timestamp_to_str(time_t wanted)
{
struct tm wanted_struct;
memcpy(&wanted_struct, localtime(&wanted), sizeof(struct tm));
/* if you need the current time for relative dates, use the following lines:
time_t curr;
struct tm curr_struct;
time(&curr);
memcpy(&curr_struct, localtime(&curr), sizeof(struct tm));
*/
return dc_mprintf("%02i.%02i.%04i %02i:%02i:%02i",