PDA

Click to See Complete Forum and Search --> : month and year


craig_phillips2
Apr 14th, 2002, 04:45 PM
how can i get the current month and year as int values using C programming methods as opposed to C++.

I cant use C++ because it is for a procedural programming assignment, not OO. I want to write them to my struct:

struct temp_rec
{
int iYear;
int iMonth;
int iTemp;
int iLoc;
char* sLoc;
};

temp_rec trRec;

void get_temp()
{
temp_rec* pRec = &trRec;
char sVal[20];
int iVal = 0;

do{
clrscr();
printf("Please enter the location: ");
gets(sVal);
pRec->sLoc = sVal;
if((iVal = get_loc_id(sVal)) == 0)
{
printf("Department ID not found!");
do{
printf("\nPlease enter Department ID: ");
gets(sVal);
iVal = atoi(sVal);
printf("Debug iVal = %d",iVal);
getch();
}while((!chk_dept(iVal)) || iVal < 1)
}
pRec->iLoc = iVal;
pRec->iYear = // The current year
pRec->iMonth = // The current month
printf("Please enter temperature: ");
gets(sVal);
iVal = atoi(sVal);
printf("Debug iVal = %d",iVal);
getch();
printf("\n\nYou are about to add the following details:\n"
"\nDepartment ID: %d"
"\nDepartment Name: %s"
"\nTemperature: %d"
"\n\nTo be added for %s %d."
"\n\nIs this information correct? ",
pRec->iLoc,pRec->sLoc,pRec->iTemp,get_month(pRec->iMonth), pRec->iYear);
gets(sVal);
}while(!UCase(sVal[0]) == 'Y');
}

CornedBee
Apr 15th, 2002, 09:22 AM
There are functions in the time.h header.

time_t time;
tm *pTime;

time(&time);
pTime = gmtime(&time);
pTime-> ...


tm is a struct that holds all values you want as fields.

jim mcnamara
Apr 15th, 2002, 09:27 AM
#include <time.h>
#include <stdio.h>
int main(void){
struct tm *t;
time_t lt;
int year, month;
lt = time(NULL);
t = localtime(&lt);
year = t->tm_year + 1900;
month = t->tm_mon + 1;
printf("year = %d month = %d \n",year,month);
return 0;
}