abt time functions

Discussion in 'C Programming' started by SSG, Jul 6, 2005.

  1. SSG

    SSG Guest

    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
     
    SSG, Jul 6, 2005
    #1
    1. Advertising

  2. SSG

    chellappa Guest

    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
     
    chellappa, Jul 6, 2005
    #2
    1. Advertising

  3. 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
     
    Robert Gamble, Jul 6, 2005
    #3
  4. 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;
    }
     
    Martin Ambuhl, Jul 6, 2005
    #4
  5. 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
     
    Emmanuel Delahaye, Jul 6, 2005
    #5
  6. SSG

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jul 6, 2005
    #6
  7. 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
     
    Robert Gamble, Jul 6, 2005
    #7
  8. "Martin Ambuhl" <> wrote in message
    news:gMSye.17030$...
    > 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;
    > }
    >
    >
    >
     
    Stephen Mayes, Jul 6, 2005
    #8
  9. SSG

    osmium Guest

    "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.
     
    osmium, Jul 6, 2005
    #9
  10. 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?"
     
    Emmanuel Delahaye, Jul 6, 2005
    #10
  11. Stephen Mayes wrote:

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


    In this newsgroup, very few of us would suggest that you engage in
    illegal behavior. Don't tell us if you do find and use such an illegal
    copy; some people here have been known to report these violations of law.
     
    Martin Ambuhl, Jul 6, 2005
    #11
  12. On Wed, 6 Jul 2005 13:58:33 -0700, in comp.lang.c , "osmium"
    <> wrote:

    >"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.


    Not just in the US - the Berne Convention requires fifty years after
    the owner's death.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jul 6, 2005
    #12
  13. SSG

    /dev/geek Guest

    Just thought I'd mention a couple things...

    > time (&rt);


    Since time() returns -1 on failure, we need to do some error checking:

    time_t rt;

    if ((rt = time(NULL)) == -1) {
    perror("time()");
    exit(EXIT_FAILURE);
    }

    > ti = *localtime (&rt);


    Again, some error checking is needed here. Since localtime() returns NULL on
    failure, it's possible that your code might dereference a NULL pointer.

    A better function should be available, localtime_r(), which allows easier error
    checking and is also threadsafe.

    Cheers.

    --

    # /dev/geek
     
    /dev/geek, Jul 7, 2005
    #13
  14. SSG

    Eric Sosman Guest

    /dev/geek wrote:
    > Just thought I'd mention a couple things...
    >
    >> time (&rt);

    >
    >
    > Since time() returns -1 on failure, we need to do some error checking:
    >
    > time_t rt;
    >
    > if ((rt = time(NULL)) == -1) {


    It will probably never make any difference, but the
    pedant-approved way to write this test is

    if ((rt = time(NULL)) == (time_t)-1) {

    Here's why: We do not actually know what hides behind
    the `time_t' mask; all we know is that it is an arithmetic
    type capable of representing times. "Arithmetic type" could
    be any signed or unsigned integer type or even a floating-point
    type; in C99 I suppose it could be a complex type. (Hawking
    has argued on cosmological grounds that time is complex.)

    Now, suppose `time_t' is an unsigned integer type that
    is narrower than `int' -- `unsigned char' or `unsigned short'
    or one of C99's extended types, say. Then if time() fails,
    rt will have a positive narrower-than-`int' value (it will
    be sometype_MAX), and this value will remain positive after
    being promoted for comparison with the `int' value -1. The
    comparison will report "unequal" and the failure will go
    undetected.

    Admittedly, this is all pretty far-fetched. The original
    reason time() took an argument was that the "returned" value
    was too large for the 16-bit `int' of the era and `long' hadn't
    been invented yet, so it's unlikely that you'll ever find a
    `time_t' that's narrower than `int'. Indeed, some systems are
    beginning to adopt a `time_t' that's wider than `long'! Still,
    there's always the remote possibility that your code might need
    to run on the DeathStation 9000, where `time_t' occupies three
    eleven-bit bytes and an `int' occupies five ...

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 7, 2005
    #14
  15. SSG

    /dev/geek Guest

    > It will probably never make any difference, but the
    > pedant-approved way to write this test is


    Aye, I purposely left that off. :) My intent was to show a basic error
    checking method, not to be pedantic.

    Cheers.

    --

    # /dev/geek
     
    /dev/geek, Jul 7, 2005
    #15
  16. SSG

    Suman Guest

    Eric Sosman wrote:
    > /dev/geek wrote:
    > > Just thought I'd mention a couple things...
    > >
    > >> time (&rt);

    > >
    > >
    > > Since time() returns -1 on failure, we need to do some error checking:
    > >
    > > time_t rt;
    > >
    > > if ((rt = time(NULL)) == -1) {

    >
    > It will probably never make any difference, but the
    > pedant-approved way to write this test is
    >
    > if ((rt = time(NULL)) == (time_t)-1) {
    >
    > Here's why: We do not actually know what hides behind
    > the `time_t' mask; all we know is that it is an arithmetic
    > type capable of representing times. "Arithmetic type" could
    > be any signed or unsigned integer type or even a floating-point
    > type;


    In which case, the above pedant-approved line is no longer much
    pedant-approved.Ain't it so?

    > in C99 I suppose it could be a complex type. (Hawking
    > has argued on cosmological grounds that time is complex.)

    [snip]
     
    Suman, Jul 8, 2005
    #16
  17. "Suman" <> writes:
    > Eric Sosman wrote:

    [...]
    >> It will probably never make any difference, but the
    >> pedant-approved way to write this test is
    >>
    >> if ((rt = time(NULL)) == (time_t)-1) {
    >>
    >> Here's why: We do not actually know what hides behind
    >> the `time_t' mask; all we know is that it is an arithmetic
    >> type capable of representing times. "Arithmetic type" could
    >> be any signed or unsigned integer type or even a floating-point
    >> type;

    >
    > In which case, the above pedant-approved line is no longer much
    > pedant-approved.Ain't it so?


    Why not? time() returns (time_t)-1 on error; if it's floating-point,
    that's -1.0. Unless the floating-point subsystem is so badly screwed
    up that -1.0 != -1.0, the above line should be safe.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 8, 2005
    #17
  18. SSG

    Suman Guest

    Keith Thompson wrote:
    > "Suman" <> writes:
    > > Eric Sosman wrote:

    > [...]
    > >> It will probably never make any difference, but the
    > >> pedant-approved way to write this test is
    > >>
    > >> if ((rt = time(NULL)) == (time_t)-1) {
    > >>
    > >> Here's why: We do not actually know what hides behind
    > >> the `time_t' mask; all we know is that it is an arithmetic
    > >> type capable of representing times. "Arithmetic type" could
    > >> be any signed or unsigned integer type or even a floating-point
    > >> type;

    > >
    > > In which case, the above pedant-approved line is no longer much
    > > pedant-approved.Ain't it so?

    >
    > Why not? time() returns (time_t)-1 on error; if it's floating-point,
    > that's -1.0. Unless the floating-point subsystem is so badly screwed
    > up that -1.0 != -1.0, the above line should be safe.

    Because direct comparison on floating point numbers are almost always
    best avoided. Or, that is what I thought was the right thing to do in
    general.
     
    Suman, Jul 8, 2005
    #18
  19. SSG

    Richard Bos Guest

    "Suman" <> wrote:

    > Keith Thompson wrote:
    > > "Suman" <> writes:
    > > > Eric Sosman wrote:
    > > >> Here's why: We do not actually know what hides behind
    > > >> the `time_t' mask; all we know is that it is an arithmetic
    > > >> type capable of representing times. "Arithmetic type" could
    > > >> be any signed or unsigned integer type or even a floating-point
    > > >> type;
    > > >
    > > > In which case, the above pedant-approved line is no longer much
    > > > pedant-approved.Ain't it so?

    > >
    > > Why not? time() returns (time_t)-1 on error; if it's floating-point,
    > > that's -1.0. Unless the floating-point subsystem is so badly screwed
    > > up that -1.0 != -1.0, the above line should be safe.

    > Because direct comparison on floating point numbers are almost always
    > best avoided. Or, that is what I thought was the right thing to do in
    > general.


    It _is_ true in general. In this specific case, though, the Standard
    speaks literally of (time_t)(-1), and -1.0 must be exactly representable
    in any kind of float, so this is safe.

    Richard
     
    Richard Bos, Jul 8, 2005
    #19
  20. SSG

    Suman Guest

    Richard Bos wrote:
    > "Suman" <> wrote:
    >
    > > Keith Thompson wrote:
    > > > "Suman" <> writes:
    > > > > Eric Sosman wrote:
    > > > >> Here's why: We do not actually know what hides behind
    > > > >> the `time_t' mask; all we know is that it is an arithmetic
    > > > >> type capable of representing times. "Arithmetic type" could
    > > > >> be any signed or unsigned integer type or even a floating-point
    > > > >> type;
    > > > >
    > > > > In which case, the above pedant-approved line is no longer much
    > > > > pedant-approved.Ain't it so?
    > > >
    > > > Why not? time() returns (time_t)-1 on error; if it's floating-point,
    > > > that's -1.0. Unless the floating-point subsystem is so badly screwed
    > > > up that -1.0 != -1.0, the above line should be safe.

    > > Because direct comparison on floating point numbers are almost always
    > > best avoided. Or, that is what I thought was the right thing to do in
    > > general.

    >
    > It _is_ true in general. In this specific case, though, the Standard
    > speaks literally of (time_t)(-1),

    I am not too sure I follow you, can you be more specific?
    > and -1.0 must be exactly representable
    > in any kind of float, so this is safe.

    Aye, aye! Sir!
     
    Suman, Jul 8, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek LaZard

    Re: Abt Datareader Count

    Derek LaZard, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,746
    Derek LaZard
    Jul 8, 2003
  2. Replies:
    5
    Views:
    559
    Ray Andraka
    Mar 3, 2005
  3. prasad

    abt floating point numbers

    prasad, Jan 27, 2006, in forum: VHDL
    Replies:
    1
    Views:
    581
    David Bishop
    Feb 4, 2006
  4. samrat

    question abt speech apllication

    samrat, Nov 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    charles
    Nov 5, 2003
  5. flamesrock
    Replies:
    8
    Views:
    475
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page