Menu

Time functions

2001-03-31
2012-09-26
  • Nobody/Anonymous

    How can I print the actual time in my Dos-program?

     
    • Nobody/Anonymous

      You can use the <time.h> functions:

      time_t time(time_t *tp);
      size_t strftime(char *s, size_t smax, const chat *fmt, const struct tm *tp);

      The first gives you a number holding the time, the second gives you a formatted string like printf holding the information that you want (day of the week, hour, etc)

      if you want to know more, ask me.

      --Azdo--

       
      • Nobody/Anonymous

        Hi, I'm also looking into doing this. Anyone kind enough to share a sample code? Thx in advance.

         
    • upcase

      upcase - 2003-01-07

      Try this:
      #include <stdio.h>
      #include <time.h>

      int main()
      {  time_t actual;
          struct tm *loctime;
          char string[60];
         
          actual = time(NULL);
          loctime = localtime (&actual);

          strftime(string , 60, "The current time is %H:%M:%S",loctime);
          printf("%s\n",string);
      }

      It prints the actual time :-)
      For more on this see www.cppreference.com

      Bye

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.