Fixed to compare after conversion to localtime in alert_localtime logic 60/229060/1 submit/tizen/20200327.115503
authorjk.koo <jk.koo@samsung.com>
Fri, 27 Mar 2020 10:30:38 +0000 (19:30 +0900)
committerjk.koo <jk.koo@samsung.com>
Fri, 27 Mar 2020 10:30:38 +0000 (19:30 +0900)
Change-Id: I66130223f20f71e80e6c3557eba927e666aed106
Signed-off-by: jk.koo <jk.koo@samsung.com>
packaging/calendar-service.spec
server/cal_server_alarm.c

index 4b8e5ed..f55d307 100644 (file)
@@ -1,6 +1,6 @@
 Name:       calendar-service
 Summary:    DB library for calendar
-Version:    0.1.178
+Version:    0.1.179
 Release:    1
 Group:      System/Libraries
 License:    Apache-2.0
index 6fa80a1..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,9 +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};
+       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 > 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);
@@ -187,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);
@@ -208,7 +230,7 @@ static int _get_event_alert_localtime(const char *field, int event_id, time_t cu
        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)
@@ -253,9 +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};
+       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 > 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);
@@ -292,7 +323,7 @@ static int _get_todo_alert_localtime(const char *field, int event_id, time_t cur
        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.
@@ -359,7 +390,7 @@ static int cal_server_alarm_get_alert_time(int alarm_id, time_t *tt_alert)
                                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);
                        }
                }
@@ -369,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 */
 
@@ -514,7 +546,7 @@ static void _cal_server_alarm_get_upcoming_specific_localtime(const char *dateti
                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);
@@ -633,7 +665,7 @@ static void _cal_server_alarm_get_upcoming_nonspecific_event_localtime(const cha
                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);
@@ -751,7 +783,7 @@ static void _cal_server_alarm_get_upcoming_nonspecific_todo_localtime(const char
                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);
@@ -803,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;
 }