The solution pointed out by colleague @bigown from a data abstraction point of view is valid, but he reinvented the wheel because the standard library already has all the date and time abstractions and all the functions needed to manipulate it, the
The most appropriate to replace the ?
would be the type time_t
defined by the default library time.h
. This type represents the number of seconds that has passed since the date of January 1, 1970 at 00:00:00 UTC, is considered the zero mark of the calendar system used by computers.
Although the Gregorian calendar facilitates chronological reasoning
for humans, when it is desired to make logical or
calculations with dates on computers, this type of calendar
making work difficult.
For example, for us, to know what happened first, if something went in
10/04/1977 12:45:15 or something on 10/03/1976 13:09:12 is something almost
automatic but to solve this on a computer all 6 fields
independently, although
almost instantaneously, it is a
extra that the processor could avoid if it made use of another format
of date. Imagine a database with thousands of records and the
processor getting a command to put everything in order
chronological, if we can make the comparisons with a single operation
per record instead of 6 operations / record the final time also
will tend to be 6 times smaller.
The following example illustrates the conversion of a value of type time_t
to a human readable date and time:
#include <stdio.h>
#include <time.h>
int main( int argc, char * argv[] )
{
time_t agora;
char datahora[100];
/* Recupera a quantidade de segundos desde 01/01/1970 */
agora = time(NULL);
/* Formata a data e a hora da forma desejada */
strftime( datahora, sizeof(datahora), "%d.%m.%Y - %H:%M:%S", localtime( &agora ) );
printf( "Data/Hora: %s\n", datahora );
return 0;
}
Already another, it illustrates how to read a date and time in human format for the number of seconds from zero:
#include <stdio.h>
#include <time.h>
char datahora[] = "01.01.2016 - 12:00:00";
int main( int argc, char * argv[] )
{
struct tm tm;
int ano, mes;
sscanf( datahora, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );
tm.tm_year = ano - 1900;
tm.tm_mon = mes - 1;
printf( "Segundos desde 01/01/1970: %ld\n", mktime( &tm ) );
return 0;
}
The best part is when we need to compare dates, we can use comparison operators, in the same way we compare integers:
#include <stdio.h>
#include <time.h>
char datahora1[] = "12.01.2000 - 12:00:00";
char datahora2[] = "01.01.2015 - 13:10:20";
int main( int argc, char * argv[] )
{
time_t t1;
time_t t2;
struct tm tm;
int ano, mes;
sscanf( datahora1, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );
tm.tm_year = ano - 1900;
tm.tm_mon = mes - 1;
t1 = mktime( &tm );
sscanf( datahora2, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );
tm.tm_year = ano - 1900;
tm.tm_mon = mes - 1;
t2 = mktime( &tm );
if( t1 < t2 )
{
printf( "Data1 esta antes de Data2!\n");
}
else if( t1 > t2 )
{
printf( "Data1 esta depois Data2!\n");
}
else
{
printf( "Data1 eh iguaal a Data2!\n");
}
return 0;
}
I hope I have helped!