Fixed to compare after conversion to localtime in alert_localtime logic
[platform/core/pim/calendar-service.git] / server / cal_server_alarm.c
index 40bbced..3ee36be 100644 (file)
@@ -138,6 +138,18 @@ static int _cal_server_alarm_update_alarm_id(int alarm_id, int event_id, int tic
        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;
@@ -169,12 +181,18 @@ static int _get_event_alert_localtime(const char *field, int event_id, time_t cu
        int ret = 0;
        char query[CAL_DB_SQL_MAX_LEN] = {0};
        struct tm st = {0};
-       tzset();
-       localtime_r(&current, &st);
-       time_t mod_current = timegm(&st);
+       struct tm now_s = {0};
+       char buf[256] = {0};
+
+       localtime_r(&current, &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 ORDER BY %s LIMIT 1",
-                       field, CAL_TABLE_LOCALTIME_INSTANCE, event_id, field, mod_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);
@@ -190,6 +208,7 @@ static int _get_event_alert_localtime(const char *field, int event_id, time_t cu
        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);
@@ -209,8 +228,9 @@ static int _get_event_alert_localtime(const char *field, int event_id, time_t cu
        st.tm_hour = h;
        st.tm_min = n;
        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)
@@ -236,10 +256,10 @@ static int64_t _get_todo_alert_utime(const char *field, int id, time_t now_t)
        case CAL_SQLITE_ROW:
                utime = (int64_t)sqlite3_column_int64(stmt, 0);
                break;
-               /* LCOV_EXCL_START */
+               /* LCOV_EXCL_START
        case SQLITE_DONE:
                ERR("No data");
-               break;
+               break;*/ //Svace:371986
        default:
                ERR("Invalid return(%d)", ret);
                break;
@@ -255,12 +275,18 @@ static int _get_todo_alert_localtime(const char *field, int event_id, time_t cur
        int ret = 0;
        char query[CAL_DB_SQL_MAX_LEN] = {0};
        struct tm st = {0};
-       tzset();
-       localtime_r(&current, &st);
-       time_t mod_current = timegm(&st);
+       struct tm now_s = {0};
+       char buf[256] = {0};
+
+       localtime_r(&current, &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 ORDER BY %s LIMIT 1",
-                       field, CAL_TABLE_SCHEDULE, event_id, field, mod_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);
@@ -295,8 +321,9 @@ static int _get_todo_alert_localtime(const char *field, int event_id, time_t cur
        st.tm_hour = h;
        st.tm_min = n;
        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.
@@ -362,7 +389,8 @@ static int cal_server_alarm_get_alert_time(int alarm_id, time_t *tt_alert)
                                st.tm_hour = h;
                                st.tm_min = n;
                                st.tm_sec = s;
-                               *tt_alert = mktime(&st);
+                               st.tm_isdst = cal_time_is_dst_savings();
+                               *tt_alert = _make_time(&st);
                                DBG("datetime[%s] to %02d:%02d:%02d (%ld)", datetime, h, n, s, *tt_alert);
                        }
                }
@@ -372,6 +400,7 @@ static int cal_server_alarm_get_alert_time(int alarm_id, time_t *tt_alert)
        sqlite3_finalize(stmt);
 
        time_t current = time(NULL);
+
        current += (tick * unit);
        current -= 2; /* in case time passed */
 
@@ -400,9 +429,10 @@ static int cal_server_alarm_get_alert_time(int alarm_id, time_t *tt_alert)
                }
                break;
        }
+       *tt_alert = utime - (tick * unit);
+
        DBG("alert_time(%ld) = utime(%lld) - (tick(%d) * unit(%d))", *tt_alert, utime, tick, unit);
 
-       *tt_alert = utime - (tick * unit);
        return CALENDAR_ERROR_NONE;
 }
 
@@ -515,7 +545,8 @@ static void _cal_server_alarm_get_upcoming_specific_localtime(const char *dateti
                st.tm_hour = h;
                st.tm_min = n;
                st.tm_sec = s;
-               ad->alert_utime = (long long int)mktime(&st);
+               st.tm_isdst = cal_time_is_dst_savings();
+               ad->alert_utime = (long long int)_make_time(&st);
                if (false == get_all) break;
        }
        sqlite3_finalize(stmt);
@@ -633,7 +664,8 @@ static void _cal_server_alarm_get_upcoming_nonspecific_event_localtime(const cha
                st.tm_hour = h;
                st.tm_min = n;
                st.tm_sec = s;
-               ad->alert_utime = (long long int)mktime(&st) - (ad->tick * ad->unit);
+               st.tm_isdst = cal_time_is_dst_savings();
+               ad->alert_utime = (long long int)_make_time(&st) - (ad->tick * ad->unit);
                if (false == get_all) break;
        }
        sqlite3_finalize(stmt);
@@ -750,7 +782,8 @@ static void _cal_server_alarm_get_upcoming_nonspecific_todo_localtime(const char
                st.tm_hour = h;
                st.tm_min = n;
                st.tm_sec = s;
-               ad->alert_utime = (long long int)mktime(&st) - (ad->tick * ad->unit);
+               st.tm_isdst = cal_time_is_dst_savings();
+               ad->alert_utime = (long long int)_make_time(&st) - (ad->tick * ad->unit);
                if (false == get_all) break;
        }
        sqlite3_finalize(stmt);
@@ -758,6 +791,7 @@ static void _cal_server_alarm_get_upcoming_nonspecific_todo_localtime(const char
 
 static void _cal_server_alarm_get_latest(time_t utime, bool get_all, GList **out_l)
 {
+       int ret = 0;
        CAL_FN_CALL();
        RET_IF(NULL == out_l);
 
@@ -766,9 +800,14 @@ static void _cal_server_alarm_get_latest(time_t utime, bool get_all, GList **out
        localtime_r(&utime, &st_local);
 
        char datetime[CAL_STR_SHORT_LEN32] = {0};
-       snprintf(datetime, sizeof(datetime), CAL_FORMAT_LOCAL_DATETIME,
+       ret = snprintf(datetime, sizeof(datetime), CAL_FORMAT_LOCAL_DATETIME,
                        st_local.tm_year +1900, st_local.tm_mon + 1, st_local.tm_mday,
                        st_local.tm_hour, st_local.tm_min, st_local.tm_sec);
+
+       if(ret < 0){
+               WARN("datetime is truncated (%s)",datetime);
+       }
+
        DBG("get alert to register with given time (%ld) datetime[%s]", utime, datetime);
 
        GList *l = NULL;
@@ -796,8 +835,8 @@ static gint _cal_server_alarm_sort_cb(gconstpointer a, gconstpointer b)
 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;
 }