Advertisement
bkit4s0

[time] date use pointer struct

Jan 17th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4. typedef struct TIME
  5. {
  6.     int t_year;
  7.     int t_mon;
  8.     int t_day;
  9. };
  10. void t_printf(TIME *t)
  11. {
  12.     if(t==NULL)
  13.         return;
  14.     printf("%d/",t->t_year);
  15.     printf("%d/",t->t_mon);
  16.     printf("%d\n", t->t_day);
  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_year < t1->t_year) || (t2->t_year == t1->t_year && t2->t_mon < t1->t_mon) ||
  27.         (t2->t_year == t1->t_year && t2->t_mon == t1->t_mon && t2->t_day < t1->t_day))
  28.         return NULL;
  29.     if(t2->t_mon < t1->t_mon)
  30.     {
  31.         m_esp = 12;
  32.         h_espf = -1;
  33.     }
  34.     if(t2->t_day < t1->t_day)
  35.     {
  36.         s_esp = 30;
  37.         m_espf = -1;
  38.     }
  39.     // not exactly days between 2 date
  40.     tmp->t_year = t2->t_year - t1->t_year + h_espf;
  41.     tmp->t_mon = t2->t_mon - t1->t_mon + m_esp + m_espf;
  42.     tmp->t_day = t2->t_day - t1->t_day + s_esp;
  43.  
  44.     return tmp;
  45. }
  46. int main ()
  47. {
  48.    time_t rawtime;
  49.    struct tm *info;
  50.    char buffer[80];
  51.    TIME * t_pointer;
  52.    TIME * t_pointer2;
  53.    TIME * t_pointer3;
  54.    t_pointer = (TIME *)malloc(sizeof(TIME));
  55.    t_pointer2 = (TIME *)malloc(sizeof(TIME));
  56.    t_pointer3 = (TIME *)malloc(sizeof(TIME));
  57.  
  58.    t_pointer2->t_year = 2016;
  59.    t_pointer2->t_mon = 3;
  60.    t_pointer2->t_day = 16;
  61.  
  62.    time( &rawtime );
  63.    info = localtime( &rawtime );
  64.    t_pointer->t_year = info->tm_year + 1900; // tm_year only use when year since 1900
  65.    t_pointer->t_mon = info->tm_mon + 1; // tm_mon start at 0
  66.    t_pointer->t_day = info->tm_mday;
  67.    //t_printf(t_pointer);
  68.  
  69.    t_pointer3 = t_sub(t_pointer2, t_pointer);
  70.    t_printf(t_pointer3);
  71.  
  72.    free(t_pointer);
  73.    free(t_pointer2);
  74.    free(t_pointer3);
  75.    //printf("Current local second: %d\n", info->tm_hour);
  76.    system("pause");
  77.    return(0);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement