Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<stdlib.h>
- #include <time.h>
- typedef struct TIME
- {
- int t_year;
- int t_mon;
- int t_day;
- };
- void t_printf(TIME *t)
- {
- if(t==NULL)
- return;
- printf("%d/",t->t_year);
- printf("%d/",t->t_mon);
- printf("%d\n", t->t_day);
- }
- TIME *t_sub(TIME *t2, TIME *t1)
- {
- TIME *tmp;
- int m_esp, h_espf, s_esp, m_espf;
- m_esp = h_espf = s_esp = m_espf = 0;
- tmp = (TIME *)malloc(sizeof(TIME));
- if( (t2->t_year < t1->t_year) || (t2->t_year == t1->t_year && t2->t_mon < t1->t_mon) ||
- (t2->t_year == t1->t_year && t2->t_mon == t1->t_mon && t2->t_day < t1->t_day))
- return NULL;
- if(t2->t_mon < t1->t_mon)
- {
- m_esp = 12;
- h_espf = -1;
- }
- if(t2->t_day < t1->t_day)
- {
- s_esp = 30;
- m_espf = -1;
- }
- // not exactly days between 2 date
- tmp->t_year = t2->t_year - t1->t_year + h_espf;
- tmp->t_mon = t2->t_mon - t1->t_mon + m_esp + m_espf;
- tmp->t_day = t2->t_day - t1->t_day + s_esp;
- return tmp;
- }
- int main ()
- {
- time_t rawtime;
- struct tm *info;
- char buffer[80];
- TIME * t_pointer;
- TIME * t_pointer2;
- TIME * t_pointer3;
- t_pointer = (TIME *)malloc(sizeof(TIME));
- t_pointer2 = (TIME *)malloc(sizeof(TIME));
- t_pointer3 = (TIME *)malloc(sizeof(TIME));
- t_pointer2->t_year = 2016;
- t_pointer2->t_mon = 3;
- t_pointer2->t_day = 16;
- time( &rawtime );
- info = localtime( &rawtime );
- t_pointer->t_year = info->tm_year + 1900; // tm_year only use when year since 1900
- t_pointer->t_mon = info->tm_mon + 1; // tm_mon start at 0
- t_pointer->t_day = info->tm_mday;
- //t_printf(t_pointer);
- t_pointer3 = t_sub(t_pointer2, t_pointer);
- t_printf(t_pointer3);
- free(t_pointer);
- free(t_pointer2);
- free(t_pointer3);
- //printf("Current local second: %d\n", info->tm_hour);
- system("pause");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment