vlm: store dates as time_t / seconds
authorRémi Denis-Courmont <remi@remlab.net>
Mon, 23 May 2016 18:28:39 +0000 (21:28 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Tue, 24 May 2016 19:09:47 +0000 (22:09 +0300)
There was no point multiplying and dividing by CLOCK_FREQ all the time.
VLM never had sub-second precision.

Also use div() as appropriate.

src/input/vlm.c
src/input/vlm_internal.h
src/input/vlmshell.c

index 0854dd6..dd4d893 100644 (file)
@@ -256,16 +256,6 @@ int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
     return i_result;
 }
 
-
-int64_t vlm_Date(void)
-{
-    struct timespec ts;
-
-    (void)timespec_get( &ts, TIME_UTC );
-    return ts.tv_sec * INT64_C(1000000) + (ts.tv_nsec / 1000);
-}
-
-
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -390,11 +380,9 @@ static void* Manage( void* p_object )
 {
     vlm_t *vlm = (vlm_t*)p_object;
     int i, j;
-    mtime_t i_lastcheck;
-    mtime_t i_time;
-    mtime_t i_nextschedule = 0;
+    time_t lastcheck, now, nextschedule = 0;
 
-    i_lastcheck = vlm_Date();
+    time(&lastcheck);
 
     for( ;; )
     {
@@ -406,8 +394,8 @@ static void* Manage( void* p_object )
         mutex_cleanup_push( &vlm->lock_manage );
         while( !vlm->input_state_changed && !scheduled_command )
         {
-            if( i_nextschedule )
-                scheduled_command = vlc_cond_timedwait( &vlm->wait_manage, &vlm->lock_manage, i_nextschedule ) != 0;
+            if( nextschedule != 0 )
+                scheduled_command = vlc_cond_timedwait( &vlm->wait_manage, &vlm->lock_manage, nextschedule * CLOCK_FREQ ) != 0;
             else
                 vlc_cond_wait( &vlm->wait_manage, &vlm->lock_manage );
         }
@@ -454,38 +442,38 @@ static void* Manage( void* p_object )
         }
 
         /* scheduling */
-        i_time = vlm_Date();
-        i_nextschedule = 0;
+        time(&now);
+        nextschedule = 0;
 
         for( i = 0; i < vlm->i_schedule; i++ )
         {
-            mtime_t i_real_date = vlm->schedule[i]->i_date;
+            time_t real_date = vlm->schedule[i]->date;
 
             if( vlm->schedule[i]->b_enabled )
             {
-                if( vlm->schedule[i]->i_date == 0 ) // now !
+                if( vlm->schedule[i]->date == 0 ) // now !
                 {
-                    vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
-                    i_real_date = i_time;
+                    vlm->schedule[i]->date = now;
+                    real_date = now;
                 }
-                else if( vlm->schedule[i]->i_period != 0 )
+                else if( vlm->schedule[i]->period != 0 )
                 {
                     int j = 0;
-                    while( vlm->schedule[i]->i_date + j *
-                           vlm->schedule[i]->i_period <= i_lastcheck &&
+                    while( ((vlm->schedule[i]->date + j *
+                             vlm->schedule[i]->period) <= lastcheck) &&
                            ( vlm->schedule[i]->i_repeat > j ||
-                             vlm->schedule[i]->i_repeat == -1 ) )
+                             vlm->schedule[i]->i_repeat < 0 ) )
                     {
                         j++;
                     }
 
-                    i_real_date = vlm->schedule[i]->i_date + j *
-                        vlm->schedule[i]->i_period;
+                    real_date = vlm->schedule[i]->date + j *
+                        vlm->schedule[i]->period;
                 }
 
-                if( i_real_date <= i_time )
+                if( real_date <= now )
                 {
-                    if( i_real_date > i_lastcheck )
+                    if( real_date > lastcheck )
                     {
                         for( j = 0; j < vlm->schedule[i]->i_command; j++ )
                         {
@@ -495,9 +483,9 @@ static void* Manage( void* p_object )
                         }
                     }
                 }
-                else if( i_nextschedule == 0 || i_real_date < i_nextschedule )
+                else if( nextschedule == 0 || real_date < nextschedule )
                 {
-                    i_nextschedule = i_real_date;
+                    nextschedule = real_date;
                 }
             }
         }
@@ -516,7 +504,7 @@ static void* Manage( void* p_object )
             free( psz_command );
         }
 
-        i_lastcheck = i_time;
+        lastcheck = now;
         vlc_mutex_unlock( &vlm->lock );
         vlc_restorecancel (canc);
     }
index 286912d..d5a6905 100644 (file)
@@ -71,10 +71,10 @@ typedef struct
     char **command;
 
     /* the date of 1st execution */
-    mtime_t i_date;
+    time_t date;
 
-    /* if != 0 repeat schedule every (period) */
-    mtime_t i_period;
+    /* if != 0, repeat period in seconds */
+    unsigned period;
     /* number of times you have to repeat
        i_repeat < 0 : endless repeat     */
     int i_repeat;
@@ -108,7 +108,6 @@ struct vlm_t
     vlm_schedule_sys_t **schedule;
 };
 
-int64_t vlm_Date(void);
 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... );
 int ExecuteCommand( vlm_t *, const char *, vlm_message_t ** );
 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_sys_t *sched );
index afc7499..d3aefe1 100644 (file)
@@ -969,8 +969,8 @@ static vlm_schedule_sys_t *vlm_ScheduleNew( vlm_t *vlm, const char *psz_name )
     p_sched->b_enabled = false;
     p_sched->i_command = 0;
     p_sched->command = NULL;
-    p_sched->i_date = 0;
-    p_sched->i_period = 0;
+    p_sched->date = 0;
+    p_sched->period = 0;
     p_sched->i_repeat = -1;
 
     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
@@ -1026,7 +1026,6 @@ static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
     {
         struct tm time;
         const char *p;
-        time_t date;
 
         time.tm_sec = 0;         /* seconds */
         time.tm_min = 0;         /* minutes */
@@ -1043,7 +1042,7 @@ static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
 
         if( !strcmp( psz_value, "now" ) )
         {
-            schedule->i_date = 0;
+            schedule->date = 0;
         }
         else if(p == NULL)
         {
@@ -1089,8 +1088,7 @@ static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
                     return 1;
             }
 
-            date = mktime( &time );
-            schedule->i_date = ((mtime_t) date) * 1000000;
+            schedule->date = mktime(&time);
         }
     }
     else if( !strcmp( psz_cmd, "period" ) )
@@ -1098,7 +1096,6 @@ static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
         struct tm time;
         const char *p;
         const char *psz_time = NULL, *psz_date = NULL;
-        time_t date;
         unsigned i,j,k;
 
         /* First, if date or period are modified, repeat should be equal to -1 */
@@ -1165,8 +1162,9 @@ static int vlm_ScheduleSetup( vlm_schedule_sys_t *schedule, const char *psz_cmd,
         }
 
         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
-        date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
-        schedule->i_period = ((mtime_t) date) * 1000000;
+        schedule->period = ((((time.tm_year * 12 + time.tm_mon) * 30
+            + time.tm_mday) * 24 + time.tm_hour) * 60 + time.tm_min) * 60
+            + time.tm_sec;
     }
     else if( !strcmp( psz_cmd, "repeat" ) )
     {
@@ -1380,12 +1378,11 @@ static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
                         vlm_MessageNew( "enabled", schedule->b_enabled ?
                                         "yes" : "no" ) );
 
-        if( schedule->i_date != 0 )
+        if( schedule->date != 0 )
         {
             struct tm date;
-            time_t i_time = (time_t)( schedule->i_date / 1000000 );
 
-            localtime_r( &i_time, &date);
+            localtime_r( &schedule->date, &date);
             vlm_MessageAdd( msg_schedule,
                             vlm_MessageNew( "date", "%d/%d/%d-%d:%d:%d",
                                             date.tm_year + 1900, date.tm_mon + 1,
@@ -1395,23 +1392,23 @@ static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
         else
             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
 
-        if( schedule->i_period != 0 )
+        if( schedule->period != 0 )
         {
-            time_t i_time = (time_t) ( schedule->i_period / 1000000 );
+            div_t d;
             struct tm date;
 
-            date.tm_sec = (int)( i_time % 60 );
-            i_time = i_time / 60;
-            date.tm_min = (int)( i_time % 60 );
-            i_time = i_time / 60;
-            date.tm_hour = (int)( i_time % 24 );
-            i_time = i_time / 24;
-            date.tm_mday = (int)( i_time % 30 );
-            i_time = i_time / 30;
+            d = div(schedule->period, 60);
+            date.tm_sec = d.rem;
+            d = div(d.quot, 60);
+            date.tm_min = d.rem;
+            d = div(d.quot, 24);
+            date.tm_hour = d.rem;
             /* okay, okay, months are not always 30 days long */
-            date.tm_mon = (int)( i_time % 12 );
-            i_time = i_time / 12;
-            date.tm_year = (int)i_time;
+            d = div(d.quot, 30);
+            date.tm_mday = d.rem;
+            d = div(d.quot, 12);
+            date.tm_mon = d.rem;
+            date.tm_year = d.quot;
 
             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
@@ -1475,7 +1472,7 @@ static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
         {
             vlm_schedule_sys_t *s = vlm->schedule[i];
             vlm_message_t *msg_schedule;
-            mtime_t i_time, i_next_date;
+            time_t now, next_date;
 
             msg_schedule = vlm_MessageAdd( msg_child,
                                            vlm_MessageSimpleNew( s->psz_name ) );
@@ -1484,29 +1481,28 @@ static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_sys_t *media,
                                             "yes" : "no" ) );
 
             /* calculate next date */
-            i_time = vlm_Date();
-            i_next_date = s->i_date;
+            time(&now);
+            next_date = s->date;
 
-            if( s->i_period != 0 )
+            if( s->period != 0 )
             {
                 int j = 0;
-                while( s->i_date + j * s->i_period <= i_time &&
-                       ( s->i_repeat > j || s->i_repeat == -1 ) )
+                while( ((s->date + j * s->period) <= now) &&
+                       ( s->i_repeat > j || s->i_repeat < 0 ) )
                 {
                     j++;
                 }
 
-                i_next_date = s->i_date + j * s->i_period;
+                next_date = s->date + j * s->period;
             }
 
-            if( i_next_date > i_time )
+            if( next_date > now )
             {
-                time_t i_date = (time_t) (i_next_date / 1000000) ;
                 struct tm tm;
                 char psz_date[32];
 
                 strftime( psz_date, sizeof(psz_date), "%Y-%m-%d %H:%M:%S (%a)",
-                          localtime_r( &i_date, &tm ) );
+                          localtime_r( &next_date, &tm ) );
                 vlm_MessageAdd( msg_schedule,
                                 vlm_MessageNew( "next launch", "%s", psz_date ) );
             }
@@ -1640,7 +1636,7 @@ static char *Save( vlm_t *vlm )
         }
 
 
-        if( schedule->i_period != 0 )
+        if( schedule->period != 0 )
         {
             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
                 strlen( "period //-::\n" ) + 14;
@@ -1716,9 +1712,8 @@ static char *Save( vlm_t *vlm )
     {
         vlm_schedule_sys_t *schedule = vlm->schedule[i];
         struct tm date;
-        time_t i_time = (time_t) ( schedule->i_date / 1000000 );
 
-        localtime_r( &i_time, &date);
+        localtime_r( &schedule->date, &date);
         p += sprintf( p, "new %s schedule ", schedule->psz_name);
 
         if( schedule->b_enabled )
@@ -1734,24 +1729,24 @@ static char *Save( vlm_t *vlm )
                           date.tm_hour, date.tm_min, date.tm_sec);
         }
 
-        if( schedule->i_period != 0 )
+        if( schedule->period != 0 )
         {
-            p += sprintf( p, "setup %s ", schedule->psz_name );
+            div_t d;
 
-            i_time = (time_t) ( schedule->i_period / 1000000 );
+            p += sprintf( p, "setup %s ", schedule->psz_name );
 
-            date.tm_sec = (int)( i_time % 60 );
-            i_time = i_time / 60;
-            date.tm_min = (int)( i_time % 60 );
-            i_time = i_time / 60;
-            date.tm_hour = (int)( i_time % 24 );
-            i_time = i_time / 24;
-            date.tm_mday = (int)( i_time % 30 );
-            i_time = i_time / 30;
+            d = div(schedule->period, 60);
+            date.tm_sec = d.rem;
+            d = div(d.quot, 60);
+            date.tm_min = d.rem;
+            d = div(d.quot, 24);
+            date.tm_hour = d.rem;
+            d = div(d.quot, 30);
+            date.tm_mday = d.rem;
             /* okay, okay, months are not always 30 days long */
-            date.tm_mon = (int)( i_time % 12 );
-            i_time = i_time / 12;
-            date.tm_year = (int)i_time;
+            d = div(d.quot, 12);
+            date.tm_mon = d.rem;
+            date.tm_year = d.quot;
 
             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
                           date.tm_year, date.tm_mon, date.tm_mday,