Advertisement
bkit4s0

[time[ use pointer struct

Jan 16th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4. typedef struct TIME
  5. {
  6.     int t_hour;
  7.     int t_min;
  8.     int t_sec;
  9. };
  10. void t_printf(TIME *t)
  11. {
  12.     if(t==NULL)
  13.         return;
  14.     printf("%d : ",t->t_hour);
  15.     printf("%d : ",t->t_min);
  16.     printf("%d\n", t->t_sec);
  17. }
  18. TIME *t_sub(TIME *t2, TIME *t1)
  19. {
  20.     TIME *tmp;
  21.     int m_esp, h_espf, s_esp, m_espf;
  22.     m_esp = h_espf = s_esp = m_espf = 0;
  23.  
  24.     tmp = (TIME *)malloc(sizeof(TIME));
  25.  
  26.     if( (t2->t_hour < t1->t_hour) || (t2->t_hour == t1->t_hour && t2->t_min < t1->t_min) ||
  27.         (t2->t_hour == t1->t_hour && t2->t_min == t1->t_min && t2->t_sec < t1->t_sec))
  28.         return NULL;
  29.     if(t2->t_min < t1->t_min)
  30.     {
  31.         m_esp = 60;
  32.         h_espf = -1;
  33.     }
  34.     if(t2->t_sec < t1->t_sec)
  35.     {
  36.         s_esp = 60;
  37.         m_espf = -1;
  38.     }
  39.     tmp->t_hour = t2->t_hour - t1->t_hour + h_espf;
  40.     tmp->t_min = t2->t_min - t1->t_min + m_esp + m_espf;
  41.     tmp->t_sec = t2->t_sec - t1->t_sec + s_esp;
  42.  
  43.     return tmp;
  44. }
  45. int main ()
  46. {
  47.    time_t rawtime;
  48.    struct tm *info;
  49.    char buffer[80];
  50.    TIME * t_pointer;
  51.    TIME * t_pointer2;
  52.    TIME * t_pointer3;
  53.    t_pointer = (TIME *)malloc(sizeof(TIME));
  54.    t_pointer2 = (TIME *)malloc(sizeof(TIME));
  55.    t_pointer3 = (TIME *)malloc(sizeof(TIME));
  56.  
  57.    t_pointer2->t_hour = 12;
  58.    t_pointer2->t_min = 00;
  59.    t_pointer2->t_sec = 00;
  60.  
  61.    time( &rawtime );
  62.    info = localtime( &rawtime );
  63.    t_pointer->t_hour = info->tm_hour;
  64.    t_pointer->t_min = info->tm_min;
  65.    t_pointer->t_sec = info->tm_sec;
  66.    //t_printf(t_pointer);
  67.  
  68.    t_pointer3 = t_sub(t_pointer2, t_pointer);
  69.    t_printf(t_pointer3);
  70.  
  71.    free(t_pointer);
  72.    free(t_pointer2);
  73.    free(t_pointer3);
  74.    //printf("Current local second: %d\n", info->tm_hour);
  75.    system("pause");
  76.    return(0);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement