return CALENDAR_ERROR_NONE;
}
+static time_t _make_time(struct tm *time)
+{
+ time_t utc = mktime(time);
+
+ /* If an error occurs when isdst is 1, retry it after chaning isdst is 0 */
+ if (utc < 0 && time->tm_isdst != 0 ) {
+ time->tm_isdst = 0;
+ utc = mktime(time);
+ }
+ return utc;
+}
+
static long long int _get_event_alert_utime(const char *field, int event_id, time_t current)
{
int ret = 0;
int ret = 0;
char query[CAL_DB_SQL_MAX_LEN] = {0};
struct tm st = {0};
+ struct tm now_s = {0};
+ char buf[256] = {0};
+
+ localtime_r(¤t, &now_s);
+ snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d", (int)(now_s.tm_year + 1900),
+ (int)(now_s.tm_mon + 1), (int)now_s.tm_mday, (int)now_s.tm_hour,
+ (int)now_s.tm_min, (int)now_s.tm_sec);
+ DBG("Current time : (%s)", buf);
+
snprintf(query, sizeof(query), "SELECT %s FROM %s "
- "WHERE event_id=%d AND (strftime('%%s', %s) - %ld > 0) ORDER BY %s LIMIT 1",
- field, CAL_TABLE_LOCALTIME_INSTANCE, event_id, field, current, field);
+ "WHERE event_id=%d AND (strftime('%%s', %s) - strftime('%%s', '%s') > 0) ORDER BY %s LIMIT 1",
+ field, CAL_TABLE_LOCALTIME_INSTANCE, event_id, field, buf, field);
sqlite3_stmt *stmt = NULL;
ret = cal_db_util_query_prepare(query, &stmt);
if (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt))
datetime = (const char *)sqlite3_column_text(stmt, 0);
+
if (NULL == datetime || '\0' == *datetime) {
/* LCOV_EXCL_START */
ERR("Invalid datetime [%s]", datetime);
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- return (long long int)mktime(&st);
+ return (long long int)_make_time(&st);
}
static int64_t _get_todo_alert_utime(const char *field, int id, time_t now_t)
int ret = 0;
char query[CAL_DB_SQL_MAX_LEN] = {0};
struct tm st = {0};
+ struct tm now_s = {0};
+ char buf[256] = {0};
+
+ localtime_r(¤t, &now_s);
+ snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d", (int)(now_s.tm_year + 1900),
+ (int)(now_s.tm_mon + 1), (int)now_s.tm_mday, (int)now_s.tm_hour,
+ (int)now_s.tm_min, (int)now_s.tm_sec);
+ DBG("Current time : (%s)", buf);
+
snprintf(query, sizeof(query), "SELECT %s FROM %s "
- "WHERE id=%d AND (strftime('%%s', %s) - %ld > 0) ORDER BY %s LIMIT 1",
- field, CAL_TABLE_SCHEDULE, event_id, field, current, field);
+ "WHERE id=%d AND (strftime('%%s', %s) - strftime('%%s', '%s') > 0) ORDER BY %s LIMIT 1",
+ field, CAL_TABLE_SCHEDULE, event_id, field, buf, field);
sqlite3_stmt *stmt = NULL;
ret = cal_db_util_query_prepare(query, &stmt);
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- return (long long int)mktime(&st);
+ return (long long int)_make_time(&st);
}
/*
* time(NULL) is not appropriate as parameter.
st.tm_min = n;
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- *tt_alert = mktime(&st);
+ *tt_alert = _make_time(&st);
DBG("datetime[%s] to %02d:%02d:%02d (%ld)", datetime, h, n, s, *tt_alert);
}
}
sqlite3_finalize(stmt);
time_t current = time(NULL);
+
current += (tick * unit);
current -= 2; /* in case time passed */
st.tm_min = n;
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- ad->alert_utime = (long long int)mktime(&st);
+ ad->alert_utime = (long long int)_make_time(&st);
if (false == get_all) break;
}
sqlite3_finalize(stmt);
st.tm_min = n;
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- ad->alert_utime = (long long int)mktime(&st) - (ad->tick * ad->unit);
+ ad->alert_utime = (long long int)_make_time(&st) - (ad->tick * ad->unit);
if (false == get_all) break;
}
sqlite3_finalize(stmt);
st.tm_min = n;
st.tm_sec = s;
st.tm_isdst = cal_time_is_dst_savings();
- ad->alert_utime = (long long int)mktime(&st) - (ad->tick * ad->unit);
+ ad->alert_utime = (long long int)_make_time(&st) - (ad->tick * ad->unit);
if (false == get_all) break;
}
sqlite3_finalize(stmt);
static GFunc _cal_server_alarm_print_cb(gpointer data, gpointer user_data)
{
struct _alarm_data_s *ad = (struct _alarm_data_s *)data;
- DBG("id(%d) unit(%d) tick(%d) type(%d) time(%lld) datetime[%s]",
- ad->event_id, ad->unit, ad->tick, ad->type, ad->time, ad->datetime);
+ DBG("id(%d) unit(%d) tick(%d) type(%d) time(%lld) datetime[%s] alert_utime[%lld]",
+ ad->event_id, ad->unit, ad->tick, ad->type, ad->time, ad->datetime, ad->alert_utime);
return 0;
}