Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > abt time functions

Reply
Thread Tools

abt time functions

 
 
SSG
Guest
Posts: n/a
 
      07-06-2005
Hi All!
I want to know how to display the system time without
using--system("date").
can anyone give me the idea to handle this operation.

By
S.S.G

 
Reply With Quote
 
 
 
 
chellappa
Guest
Posts: n/a
 
      07-06-2005
hi ssg,
this is program using display date and time ..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
// system("date");
time_t rt;
struct tm * ti; // this structure have all int variable of tm
members... like tm_hour
char *p;
time (&rt); // this one return 0:0:0 gst 1979
ti = localtime ( &rt ); // get local time store to tm structure
printf ( "Current date and time are: %s", asctime (ti) );
//converting date structure to string
printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
using that structure
}

By
Sree Krishna
TooMuch Semiconductors Solutions
INDIA

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      07-06-2005
chellappa wrote:
> hi ssg,
> this is program using display date and time ..
> #include<stdio.h>
> #include<stdlib.h>
> #include<time.h>
> int main(void)
> {
> // system("date");
> time_t rt;
> struct tm * ti; // this structure have all int variable of tm
> members... like tm_hour


> char *p;


What is this doing here?

> time (&rt); // this one return 0:0:0 gst 1979


Huh?
No check for error here.

> ti = localtime ( &rt ); // get local time store to tm structure


No check for error.

> printf ( "Current date and time are: %s", asctime (ti) );


You need a \n at the end of your format string.

> //converting date structure to string
> printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply


Again, you need a \n at the end of the format string.
Why the double colons?

> using that structure
> }


Robert Gamble

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-06-2005
SSG wrote:
> Hi All!
> I want to know how to display the system time without
> using--system("date").
> can anyone give me the idea to handle this operation.


Please get an elementary C textbook. Use the one in the library if you
can't afford one.

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t now = time(0);
printf("The time is %s", ctime(&now));
return 0;
}



 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-06-2005
chellappa wrote on 06/07/05 :
> #include<stdio.h>
> #include<stdlib.h>
> #include<time.h>
> int main(void)
> {
> // system("date");
> time_t rt;
> struct tm * ti; // this structure have all int variable of tm
> members... like tm_hour
> char *p;
> time (&rt); // this one return 0:0:0 gst 1979
> ti = localtime ( &rt ); // get local time store to tm structure
> printf ( "Current date and time are: %s", asctime (ti) );
> //converting date structure to string
> printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
> using that structure
> }


A little enhancement...

#include<stdio.h>
#include<time.h>

int main (void)
{
/* this structure have all int variable of tm members... like
tm_hour */
struct tm ti;
time_t rt;

time (&rt);

/* get local time store to tm structure */
ti = *localtime (&rt);

printf ("Current date and time are: %s", asctime (&ti));

/* converting date structure to string
simply using that structure
*/
printf ("%02d:%02d:%02d\n", ti.tm_hour, ti.tm_min, ti.tm_sec);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-06-2005
Robert Gamble wrote:

> chellappa wrote:
>> printf ( "Current date and time are: %s", asctime (ti) );

>
> You need a \n at the end of your format string.


Only if the output is to be double-spaced. The
string returned by asctime() has a '\n' at the end.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      07-06-2005
Eric Sosman wrote:
> Robert Gamble wrote:
>
> > chellappa wrote:
> >> printf ( "Current date and time are: %s", asctime (ti) );

> >
> > You need a \n at the end of your format string.

>
> Only if the output is to be double-spaced. The
> string returned by asctime() has a '\n' at the end.


That's me not engaging my brain

Robert Gamble

 
Reply With Quote
 
Stephen Mayes
Guest
Posts: n/a
 
      07-06-2005

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:gMSye.17030$(E-Mail Removed) hlink.net...
> SSG wrote:
>> Hi All!
>> I want to know how to display the system time without
>> using--system("date").
>> can anyone give me the idea to handle this operation.

>
> Please get an elementary C textbook. Use the one in the library if you
> can't afford one.


Why must one even travel to a library? Why are not older editions of K&R
available online?

>
> #include <stdio.h>
> #include <time.h>
>
> int main(void)
> {
> time_t now = time(0);
> printf("The time is %s", ctime(&now));
> return 0;
> }
>
>
>



 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      07-06-2005
"Stephen Mayes" writes:

>> Please get an elementary C textbook. Use the one in the library if you
>> can't afford one.

>
> Why must one even travel to a library? Why are not older editions of K&R
> available online?


Copyrights can last a long time in the US. Mickey Mouse is still covered by
copyright and he goes back to 1928 or so. Complain to your congressman if
you think copyrights last too long. Otherwise come back in a hundred years.
The current edition of K&R is copyrighted 1988. The version prior to that
is hardly useful to write programs in the current environment. Not that any
of that makes any difference WRT your question.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-06-2005
Stephen Mayes wrote on 06/07/05 :
>> Please get an elementary C textbook. Use the one in the library if you
>> can't afford one.

>
> Why must one even travel to a library? Why are not older editions of K&R
> available online?



This a legal on-line C-book:

http://publications.gbdirect.co.uk/c_book/

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Need suggestion abt FFs without RST for pipelined datapath. john.deepu@gmail.com VHDL 5 03-03-2005 02:45 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Abt OOPS and .NET Anandraj ASP .Net 2 08-13-2003 01:16 PM
Re: Abt Datareader Count Derek LaZard ASP .Net 0 07-08-2003 02:35 PM



Advertisments