#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <sys/time.h>
+#include <time.h>
#include <string.h>
#include <glib.h>
#include <fcntl.h>
void _initialize_alarm_info(alarm_info_t *alarm_info, int alarm_type,
time_t trigger_at_time, time_t interval, bool precision)
{
- struct timeval current_time;
+ struct timespec current_time;
struct tm duetime_tm;
- gettimeofday(¤t_time, NULL);
+ clock_gettime(CLOCK_REALTIME, ¤t_time);
memset(alarm_info, 0, sizeof(alarm_info_t));
alarm_info->mode.repeat = ALARM_REPEAT_MODE_ONCE;
alarm_info->alarm_type = alarm_type;
- if (current_time.tv_usec > 500 * 1000) {
- /* When the millisecond part of the current_time is bigger than 500ms,
+ if (current_time.tv_nsec > (BILLION / 2)) {
+ /* When the nanosecond part of the current_time is bigger than 500ms,
* the duetime increases by extra 1sec. */
current_time.tv_sec += (trigger_at_time + 1);
} else {
alarm_info->mode.u_interval.interval = interval;
}
- alarm_info->msec = precision ? (int)current_time.tv_usec / 1000 : 0;
+ alarm_info->msec = precision ? (int)current_time.tv_nsec / MILLION : 0;
LOGD("trigger_at_time(%ld), start(%d-%d-%d, %02d:%02d:%02d),\
repeat(%d), interval(%ld), type(%d)",
const gchar *name, const gchar *path, const gchar *interface,
const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
{
- struct timeval current_time;
+ struct timespec current_time;
if (method && strcmp(method, "alarm_expired") == 0) {
gchar *package_name = NULL;
LOGD("[alarm-lib] : Alarm expired for [%s] : Alarm id [%d]", package_name, alarm_id);
if (msec > 0) {
- gettimeofday(¤t_time, NULL);
- msec = msec - (int)current_time.tv_usec / 1000;
+ clock_gettime(CLOCK_REALTIME, ¤t_time);
+ msec = msec - (int)current_time.tv_nsec / MILLION;
}
if (alarm_context.alarm_handler != NULL) {
static void __adjust_current_milliseconds(alarm_info_t *alarm_info)
{
- struct timeval current_time;
+ struct timespec current_time;
struct tm start_tm;
time_t start;
- gettimeofday(¤t_time, NULL);
- if (current_time.tv_usec > 500 * 1000) {
- /* When the millisecond part of the current_time is bigger than 500ms,
+ clock_gettime(CLOCK_REALTIME, ¤t_time);
+ if (current_time.tv_nsec > (BILLION / 2)) {
+ /* When the nanosecond part of the current_time is bigger than 500ms,
* the duetime increases by extra 1sec. */
start_tm.tm_year = alarm_info->start.year - 1900;
#include <glib/gmacros.h>
#endif
-#define BILLION 1000000000 /* for calculating nano seconds */
-
/* link path for timezone info */
#define TIMEZONE_INFO_LINK_PATH tzplatform_mkpath(TZ_SYS_ETC, "localtime")
}
}
-static int accrue_msec = 0; /* To check a millisecond part of current time at changing the system time(sec) */
+static int accrue_nsec = 0; /* To check a millisecond part of current time at changing the system time(sec) */
int alarm_manager_alarm_set_time(GVariant* parameters, pid_t pid)
{
double diff_time = 0.0;
- struct timeval cur_time = {0,};
+ struct timespec cur_time;
gint64 time_sec;
time_t new_time;
char sender_id[MAX_APP_ID_LEN];
_alarm_disable_timer(); /* Disable the timer to reschedule the alarm before the time is changed. */
tzset();
- gettimeofday(&cur_time, NULL);
+ clock_gettime(CLOCK_REALTIME, &cur_time);
- accrue_msec += (cur_time.tv_usec / 1000); /* Accrue the millisecond to compensate the time */
- if (accrue_msec > 500) {
+ accrue_nsec += (cur_time.tv_nsec / MILLION); /* Accrue the nanosecond to compensate the time */
+ if (accrue_nsec > (BILLION / 2)) { /* Over 500ms */
diff_time = difftime(new_time, cur_time.tv_sec) - 1;
- accrue_msec -= 1000;
+ accrue_nsec -= BILLION;
} else {
diff_time = difftime(new_time, cur_time.tv_sec);
}
struct timespec delay = {0,};
struct timespec sleep_time = {0,};
time_t real_newtime = 0;
- accrue_msec = 0; /* reset accrued msec */
+ accrue_nsec = 0; /* reset accrued msec */
time_t new_sec, req_sec;
long new_nsec, req_nsec;
gint64 tmp_new_sec, tmp_req_sec, tmp_new_nsec, tmp_req_nsec;