comparing timestamp using typedef,structure and fuctions
#include <stdio.h>
typedef struct timestamp{
int day;
int month;
int year;
int hour;
int minute;
int second;
}tsstruct;
void display(tsstruct d1,tsstruct d2)
{
printf("First timestamp is:%d-%d-%d %d:%d:%d\n",d1.day,d1.month,d1.year,d1.hour,d1.minute,d1.second);
printf("Second timestamp is:%d-%d-%d %d:%d:%d\n",d2.day,d2.month,d2.year,d2.hour,d2.minute,d2.second);
}
int timestampcomp(tsstruct ts1, tsstruct ts2)
{
if (ts1.year>ts2.year)
{
return 1;
}
if(ts1.year<ts2.year)
{
return -1;
}
if (ts1.month>ts2.month)
{
return 1;
}
if(ts1.month<ts2.month)
{
return -1;
}
if (ts1.day>ts2.day)
{
return 1;
}
if(ts1.day<ts2.day)
{
return -1;
}
if (ts1.hour>ts2.hour)
{
return 1;
}
if(ts1.hour<ts2.hour)
{
return -1;
}
if (ts1.minute>ts2.minute)
{
return 1;
}
if(ts1.minute<ts2.minute)
{
return -1;
}
if (ts1.second>ts2.second)
{
return 1;
}
if(ts1.second<ts2.second)
{
return -1;
}
return 0;
}
int main()
{
tsstruct ts1={24,2,1979,12,02,34};
tsstruct ts2={24,2,1980,12,02,34};
display(ts1,ts2);
int a=timestampcomp(ts1,ts2);
if (a==0)
{
printf("Both timestamps are same\n");
}
else
{
printf("The timestamps are not same\n");
}
return 0;
}
Comments
Post a Comment