By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

how to put date and time in c

P: 32
Hi Guys..

i was wondering if any of you have tryed to put date and time on c programming? as i trying to get it.. seems doesnt work.. does any1 here have a code for it?
thanks
Mar 12 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
Hi Guys..

i was wondering if any of you have tryed to put date and time on c programming? as i trying to get it.. seems doesnt work.. does any1 here have a code for it?
thanks
post you code and we can try to help!
Mar 12 '07 #2

P: 32
post you code and we can try to help!
here is my coding.. at the end of the program you will see a code of timestamp()
dont know if that is rite or wrong.

void heading(char *string);
void centreline(char *string);
void gcls(void) ;
void gsolidrectangle(int x1, int y1, int x2, int y2,int col);
void timestamp(void);

//mouse int function
int GetX(void);
int GetY(void);
int GetKey(void);
void ShowCursor(void);
void HideCursor(void);
int ButtonStatus(void);
void SetMouseMode(int);
void PrintMousePosition(void);
void UpdateMousePosition(void);
void PrintMouseButtonStatus(void);
void UpdateMouseButtonStatus(void);
void CheckMouseButtonStatus(void);
void DrawOneButton(void);
void OnButtonClick(void);
void LabelButtons(void);
void RelabelButtons(int);
void About(void);
void InitializeMouse(void);
void Startup(void);
void LoadOne(void);
void RestrictMouse(int,int,int,int);


// cpu register structure for dos function calls.
struct REGPACK reg;
union REGS regs;


//Mouse function coding here..

int GetX(void)
{
// get mouse X position
reg.r_ax=3;
intr(51,&reg);
return(reg.r_cx);
}

int GetY(void)
{
// get mouse Y position
reg.r_ax=3;
intr(51,&reg);
return(reg.r_dx);
}


void ShowCursor(void)
{
// Turn on mouse cursor
reg.r_ax=1;
intr(51,&reg);
}

void HideCursor(void)
{
// Turn off mouse cursor.
reg.r_ax=2;
intr(51,&reg);
}

void RestrictMouse(int x1,int y1,int x2,int y2)
{
union REGS i,o;
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);

i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
void getmousepos(int *button,int *x,int *y)
{
char *ss;
union REGS i,o;
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}

void CheckMouseButtonStatus(void)
{
// gets mouse buttons pressed.

/* status=ButtonStatus();

LeftButton=0;
RightButton=0;
MiddleButton=0;

if(status==1){LeftButton=1;}
if(status==2){RightButton=1;}
if(status==3){LeftButton=1;RightButton=1;}
if(status==4){MiddleButton=1;}
if(status==5){LeftButton=1;MiddleButton=1;}
if(status==6){LeftButton=1;MiddleButton=1;}
if(status==7){LeftButton=1;RightButton=1;MiddleBut ton=1;}*/
}












int main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode,button=1,x,y;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\turboc");

/* read result of initialization */
errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}

/* do some graphics */
//Heading



//green background
gsolidrectangle(640,570,0,0,deepcyan );
//text
setcolor(black);
moveto(515,130);
outtext("Control Panel");
setcolor(black);
rectangle(500,150,620,350);
rectangle(510,160,610,180);//screen
gsolidrectangle(510,160,610,180,cyan); //colour screen
//1st row button
rectangle(515,210,540,190);
rectangle(572,210,545,190);
rectangle(605,210,581,190);
moveto(525,200);
outtext("1"); //text keypad 1
moveto(555,200);
outtext("2"); //text keypad 2
moveto(590,200);
outtext("3"); //text keypad 3

//2nd row button
rectangle(515,220,540,243);
rectangle(572,220,545,243);
rectangle(580,220,605,243);
moveto(525,230);
outtext("4"); //text keypad 4
moveto(555,230);
outtext("5"); //text keypad 5
moveto(590,230);
outtext("6"); //text keypad 6

//3rd row button
rectangle(540,275,515,250);
rectangle(574,275,545,250);
rectangle(605,275,580,250);
moveto(525,260);
outtext("7"); //text keypad 7
moveto(555,260);
outtext("8"); //text keypad 8
moveto(590,260);
outtext("9"); //text keypad 9

//4th row button
rectangle(540,305,515,279);
rectangle(574,305,545,280);
rectangle(605,305,580,280);
moveto(525,290);
outtext("*"); //text keypad *
moveto(555,290);
outtext("0"); //text keypad 0
moveto(590,290);
outtext("#"); //text keypad #


//background of rectangle
gsolidrectangle(10,100,100,120,cyan);
gsolidrectangle(10,140,100,160,green);
gsolidrectangle(10,180,100,200,yellow);
gsolidrectangle(10,220,100,240,deepmagenta);
gsolidrectangle(10,260,100,280,deepgreen);
gsolidrectangle(10,300,100,320,cyan);

setcolor(deepblue);
rectangle(10,100,100,120);
rectangle(10,140,100,160);
rectangle(10,180,100,200);
rectangle(10,220,100,240);
rectangle(10,260,100,280);
rectangle(10,300,100,320);

//switch box

moveto(390,130);
outtext("Switch Box");
setcolor(black);
rectangle(490,350,350,150);
gsolidrectangle(450,160,485,190,red);//sw switch select
circle(468,175,10);
moveto(355,170);
outtext("SW Select");
//bit0 switch
gsolidrectangle(450,220,485,195,red);//bit0 switch
circle(468,207,10);
moveto(360,205);
outtext("Bit-0");
//bit1 switch
gsolidrectangle(450,250,485,225,red);//bit1 switch select
circle(468,237,10);
moveto(360,235);
outtext("Bit-1");
//bit2 switch
gsolidrectangle(450,280,485,255,red);//bit2 switch
circle(468,267,10);
moveto(360,265);
outtext("Bit-2");
gsolidrectangle(450,310,485,285,red);//bit2 switch
circle(468,297,10);
moveto(360,295);
outtext("Bit-3");

//middle button for sw swtich
circle(468,175,5);
moveto(355,170);
//middle button for bit-0
circle(468,207,5);
moveto(360,205);
//middle button for bit-1
circle(468,237,5);
moveto(360,235);
//middle button for bit-2
circle(468,267,5);
moveto(360,265);
//middle button for bit-3
circle(468,297,5);
moveto(360,295);

//Instruction box
rectangle(345,450,620,360);
moveto(360,370);
outtext("1 - Press S Start Motor");
moveto(360,390);
outtext("2 - Press T stop motor");
moveto(360,410);
outtext("3 - Press O Heater on");
moveto(360,430);
outtext("4 - Press 1 Heater off");

//Temperature box
rectangle(350,80,620,110);
gsolidrectangle(480,85,580,100,red);
moveto(360,90);
outtext("Temperature:");

//ON and OFF Switches
rectangle(350,60,620,10);
moveto(360,20);
outtext("Switches ON/OFF");
circle(361,40,5);
moveto(370,40);
outtext("ON");
circle(420,40,5);
moveto(430,40);
outtext("OFF");

//restart button
circle(500,40,5);
moveto(510,40);
outtext("Restart");

//Exit Button
rectangle(48,428,123,398);
gsolidrectangle(50,425,120,400,red);
moveto(70,410);
outtext("EXIT");


ShowCursor();
RestrictMouse(0,0,getmaxx(),getmaxy());
//mouse position for exit button to exit

while(!kbhit())
{
getmousepos(&button,&x,&y);
if((button&1)==1)
{
getmousepos(&button,&x,&y);
if(x>=70 && x<=100 && y>=410 && y<=420)
exit(0);
}
}

//showing time and date

void timestamp()
{
time_t ltime; /* calendar time */
ltime=time(NULL); /* get current cal time */
printf("%s",asctime( localtime(&ltime) ) );
}
Mar 12 '07 #3

P: 51
Hi Guys..

i was wondering if any of you have tryed to put date and time on c programming? as i trying to get it.. seems doesnt work.. does any1 here have a code for it?
thanks

Hi Bhavesh,
Here is a sample code which will give you the current date and time.


#include<time.h>

int main(){
struct tm *tm;
time_t curtime;
char *ntm;

curtime = time(NULL);

tm = localtime(&curtime);
ntm = asctime(tm);

printf("Time = %s\n",ntm);
return 0;
}

Hope Ur problem is solved...

Regards,
chella
Mar 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.