tags:

views:

354

answers:

2

I want to get the current date (day, mon and year). I found out there are some functions in C to do that like ctime (get the string of time), localtime and gmtime. I tried with following code but the output are different. I get this output:

The date and time is Tue Apr 20 2010 (which is correct)

The year is : 110

The year is : 110.

Does anybody know why?

int main(int argc, char** argv)
{   
   time_t now;
   if((now = time(NULL)) == (time_t)-1)
   {
      puts("Failure in getting time");
   }
   else {
      printf("The date and time is: %s\n", ctime(&now));
      printf("The year is: %ld\n", localtime(&now)->tm_year);
      printf("The year is: %ld\n", gmtime(&now)->tm_year);
   }
   getchar();
}
+7  A: 

Look at the man page for ctiime - the year field is years from 1900.

http://linux.die.net/man/3/ctime

tm_year
The number of years since 1900.
Paul Tomblin
Thanks, I got it. I did it wrongly for both mon and year in my program
vodkhang
@vodkhang, a lot of people make that mistake. That's why the first line of defence is to read the man page.
Paul Tomblin
A: 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<time.h>
void main()
{
int li,lp,j,k;
char dStr [9];
char tStr [9];
_strdate(dStr);
_strtime(tStr );
clrscr();
for(li=10;li<71;li++)
{
gotoxy(li,10);

textcolor(RED);
delay(30);
cprintf("\/");
}
for(li=70;li>=10;li--)
{
gotoxy(li,32);
textcolor(BLUE);
delay(30);
cprintf("\\");
}
for(lp=11;lp<=31;lp++)
{
gotoxy(10,lp);
textcolor(MAGENTA);
delay(50);
cprintf("\/");
}
for(lp=31;lp>=11;lp--)
{
gotoxy(70,lp);
textcolor(YELLOW);
delay(50);
cprintf("\/");
}
gotoxy(15,20);
textcolor(BLINK + GREEN);
cprintf("Welcome To The Shree Ram Software Private Limited\n");
gotoxy(52,13);
textcolor(GREEN);
cprintf("Date is %s ", dStr);
gotoxy(52,14);
textcolor(GREEN);
cprintf( "Time is %s ", tStr);
getch();
}
Mnoj Khandelwal
if you need to ask question, ask here http://stackoverflow.com/questions/ask and do read FAQ and welcome to SO.
N 1.1
-1: conio.h, dos.h aren't standard; main returns an int; clrscr, gotoxy, textcolor, cprintf, getch are not standard. **Indent your code**
pmg