Date and Time

This is a discussion on Date and Time within the C Programming forums, part of the General Programming Boards category; Er........wat is the command for me to display the date of today, according to the system date onto the screen ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Date and Time

    Er........wat is the command for me to display the date of today, according to the system date onto the screen display?

    Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?
    Only by the cross are you saved...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >wat is the command for me to display the date of today
    The simplest is:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        time_t today;
    
        if ((today = time(0)) != (time_t)-1)
            printf("%s", ctime(&today));
    
        return 0;
    }
    >which isl ike a clock, whereby the time is updated real-time each second?
    Yes, here is the simplest way (which is utterly useless, so I didn't bother making it nice):
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        time_t    hereandnow;
        struct tm *today;
        char      buff[BUFSIZ];
    
        while (1)
        {
            hereandnow = time(0);
            today = localtime(&hereandnow);
    
            strftime(buff, BUFSIZ, "%I:%M:%S", today);
    
            printf("%s\r", buff);
            fflush(stdout);
        }
    
        return 0;
    }
    To have an interactive clock like this you need to either spawn another process, or make use of multithreading. Otherwise the program will run and all you will be able to do is look at it and do nothing else.
    My best code is written with the delete key.

  3. #3
    Banned
    Join Date
    May 2003
    Posts
    124
    I have 3 questions about your code:
    Code:
    while (1)
        {
            hereandnow = time(0);
            today = localtime(&hereandnow);
    
            strftime(buff, BUFSIZ, "%I:%M:%S", today);
    
            printf("%s\r", buff);
            fflush(stdout);
        }
    1) what is the condition of the while statement? It never stops.
    2) What %I, %M, %S represent?
    3) What's the use of fflush(stdout) ?

    Thanks in advance

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    19
    1) what is the condition of the while statement? It never stops.
    It should be interrupted from the keyboard(Ctrl-c, for example).
    3) What's the use of fflush(stdout) ?
    The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by AProg
    I have 3 questions about your code:
    1) what is the condition of the while statement? It never stops.
    Well, you asked "Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?". The only way to update every second is to run. If you want a way out, you have to add code that tells the loop when to break;

    2) What %I, %M, %S represent?
    Look up the function. The documentation explains all.

    3) What's the use of fflush(stdout) ?
    Look up the function. The documentation explains all.

    Try Googling for your answers
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Banned
    Join Date
    May 2003
    Posts
    124

    explanation - translation needed

    >The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.

    [italic]int fflush(FILE *stream);
    Flushes a stream.
    [/italic]
    I searched a lot about the above discription but my work established fruitless. You see, English is not my mother tonque, so i have some difficulties in understanding what "flush" means. I turned that word up in my dictionnary, but the tranlslated word didn't help me so much to understand what ffluch() does.

    Would you mind explaining me the purpose of fflush in other words pls?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Would you mind explaining me the purpose of fflush in other words pls?
    The input buffer is like filling a pail of water*. Most of the time it's useful to fill the pail to the top before you pour it out, you get more water for your work that way. But, there are times where you don't want a full pail, you just fill it a bit and the pour out what you have, you're calling fflush on the pail.

    * Input stream, get it?
    My best code is written with the delete key.

  8. #8
    Banned
    Join Date
    May 2003
    Posts
    124

    I got the point of your tail, but not of the fflush()..

    Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?
    What if you want to water a little plant and a full pail is too much? Sure, you could fill it up and use a little, but then you would have left over water and need to pour it out elsewhere (what a waste). Sure, you could save it, but then you wouldn't have a pail to use for other things.
    My best code is written with the delete key.

  10. #10
    Banned
    Join Date
    May 2003
    Posts
    124

    ...

    It's ok with the tail, but how is that relevant to the fflush.
    Could you say: "Flushes a stream" whithout using the world "flushes" and not changing the meaning?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >"Flushes a stream" whithout using the world "flushes" and not changing the meaning?
    I haven't changed the meaning once, "flush the stream" means "empty the contents". In my analogy, the pail is the buffer, when you empty the pail, you are flushing the buffer. It really isn't that difficult.
    My best code is written with the delete key.

  12. #12
    Banned
    Join Date
    May 2003
    Posts
    124
    >"empty the contents"
    Sorry for asking again, but what contents do you refer to?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Sorry for asking again, but what contents do you refer to?
    The contents of the buffer. The following may not write directly to stdout:

    printf("Hello, world!");

    Because there's no newline character printed and no call to fflush, the string may be placed in a temporary buffer because calling a low level write function to place the string directly onto stdout can be expensive. When the buffer is full, then it is written as a whole to stdout. You can cause this buffer to be emptied prematurely by placing a newline at the end of the string:

    printf("Hello, world!\n");

    or calling fflush on stdout:

    printf("Hello, world!");
    fflush(stdout);
    My best code is written with the delete key.

  14. #14
    Banned
    Join Date
    May 2003
    Posts
    124
    >printf("Hello, world!");
    >fflush(stdout);

    So, in this example, it would be the same if i placed " printf( "\n" ); ", istead of " fflush( stdout ); " ?

  15. #15
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    no because you wouldn't get a newline with fflush.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 09:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. How to get and set windows time and date
    By huwan in forum Windows Programming
    Replies: 18
    Last Post: 05-13-2008, 11:33 AM
  4. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21