The solution pointed out by colleague @Maniero, from the point of view of data abstraction is valid, however, he reinvented the wheel, because the standard library already has all the abstractions of date and time and all the functions necessary to manipulate them.
Most suitable to replace the ?
would be the kind time_t
defined by the standard library time.h
. This type represents the amount of seconds that has passed since the date of January 1, 1970 at 00:00:00 (UTC), is considered the zero milestone of the calendar system used by computers.
Although the Gregorian calendar facilitates chronological reasoning
for humans, when logical comparisons are desired, or
calculations with dates on computers, this kind of calendar ends
hindering the work.
For example, for us, to know what happened first, if it was something in
10/04/1977 12:45:15 or something at 10/03/1976 13:09:12 is something almost
but to solve this on a computer all 6 fields
would have to be analysed independently, although
performed almost instantaneously does not cease to be a job
extra that the processor could avoid if it made use of another format
date. Let’s imagine a database with thousands of records and the
processor receiving a command to put everything in order
chronological, if we can make the comparisons with a single operation
by registration 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 the type time_t
for a date and time humanly readable:
#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, illustrates how to read a date and time in human format for the amount of seconds from ground 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, 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’ve helped!
Getting your introduction to C Data Structures? Good luck. :)
– Pablo Almeida
I thought you wanted some kind of date, not to stick the date in the frame anyway. I think you really need to read the material I gave you. This is Ambi.
– Maniero
I go after the material I pass moustache what I have to do is not over yet, I have to do that struct and then pass it to a function , in function it must be filled and I must have another function that prints what was filled in in the first function. I will try to study, because I still do not know how to do these maneuvers with the functions and what to learn will increase in my code, if not run you seek to help with the community here
– ALFAEX
CPF as
double
== problems when CPF starts with zero ;)– Oralista de Sistemas
i switched to char Cpf just like the bigown told me and now I understand why I can’t do in kind double thanks Renan in class I’ve seen my teacher using this type for Cpf I’ll talk to her about it
– ALFAEX