Thread: Help - C program (Date/Time)

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Help - C program (Date/Time)

    Hello,

    I am trying to write a piece of code in C that will return to me the current year that is set by the system clock. The C program is written on a Unix system. So, basically, in the C program I want to call some function or use some method that will return to me the current year to a variable; something like:

    char *temp;

    temp = "command to return the year"

    Can anyone help me with this? Thanks.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>temp = "command to return the year"
    It isn't quiiiiiite that easy, but close :-)
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
      time_t current;
      struct tm *current_tm;
    
      if (time(&current) == (time_t)-1)
      {
        return 1;
      }
    
      if ((current_tm = localtime(&current)) == 0)
      {
        return 1;
      }
    
      printf("The current year is -- %d\n", current_tm->tm_year + 1900);
    
      return 0;
    }
    *Cela*

  3. #3
    #####
    Guest

    how>

    how would u do that on a windows system using the int86() command? ps this question is totally unrelated to the one asked

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    Thank you Cela for the response. Your method would work fine but I forgot to mention that I am forced to work on a Non-ANSI compliant compiler (1988) and cannot even find the time.h header file so the structs shouldn't work. I was hoping to somehow "pipe" the return string from a UNIX command to the variable (using "date" command?).

    ie:

    int main()

    char *temp;

    /* Unix Commands */
    system("set DATE_VAR = 'date');
    /* date format UNIX command: Wed Jan 29 12:00:02 CST 2003 */
    system("export DATE_VAR");
    /***************/

    temp = getenv("DATE_VAR");
    printf("\n Date is : %s \n",temp);


    of course the compiler will *cry foul* b/c of the assigment of the string to temp, but that is ok. My problem is that "export" is not a valid command in csh (KSH its ok) so temp is always returned as NULL. This verison of C is part of work so I have no choice before anyone starts ripping me for using an old verison

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I am forced to work on a Non-ANSI compliant compiler (1988)
    Okay, I can't guarantee that this will work for you. I assume that you have to use K&R C which I'm not terribly familiar with and that your stdio.h header has popen(), FILE *'s, fgets(), and pclose(). Also, if you have strrchr() in string.h then replace the one I rolled for you :-) If you don't have fgets() then you should have gets(), if you don't have popen() then the only other thing I can think of is to redirect the date command to your program and use it like this
    Code:
    int main()
    {
      char temp[100];
    
      /* Input directed from 'date' */
      fgets(temp, sizeof temp, stdin);
    
      printf("The current year is -- %s", parse(temp));
    
      return 0;
    }
    Anyway, here's the other one :-)
    Code:
    #include <stdio.h>
    
    /* date format UNIX command: Wed Jan 29 12:00:02 CST 2003 */
    #define DELIM ' '
    
    char *strrchr(s, c)
      char *s;
      int c;
    {
      char *sc;
    
      for (sc = 0; ; s++)
      {
        if (*s == c)
        {
          sc = s;
        }
        if (*s == '\0')
        {
          return sc;
        }
      }
    }
    
    char *parse(s)
      char *s;
    {
      return strrchr(s, DELIM) + 1;
    }
    
    int main()
    {
      char temp[100];
      FILE *pipe = popen("date", "r");
    
      fgets(temp, sizeof temp, pipe);
    
      printf("The current year is -- %s", parse(temp));
    
      pclose(pipe);
    
      return 0;
    }
    *Cela*

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>system("set DATE_VAR = 'date');
    >>/* date format UNIX command: Wed Jan 29 12:00:02 CST 2003 */
    >>system("export DATE_VAR");
    No point in doing that, each call the system is a seperate shell. If you want to set environment stuff, lookup the appropriate function to do it directly (maybe setenv?) Either way, I'd suggest your going about things the wrong way.

    >> FILE *pipe = popen("date", "r");
    Why issue that, and the have all that parsing hassle. Just issue the date command with the appropriate parameters to get it to return the year only. See here.

    If you haven't got popen(), a quick hack it to system() the date command and redirect the output to a file, then fopen() that file and read in the data. But I wouldn't recommend it (oops I just did! )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    int main()
    {
    char temp[100];
    FILE *pipe = popen("date", "r");

    fgets(temp, sizeof temp, pipe);

    printf("The current year is -- %s", parse(temp));

    pclose(pipe);

    return 0;
    }


    Cela, you're amazing (but you already knew that right )
    Worked perfectly. Thanks again for your help.

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Just issue the date command with the appropriate parameters to get it to return the year only.
    Naturally I'd do that, if I'd ever used UNIX before. I was just fumbling along hoping I was right :-)
    *Cela*

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    Hammer,

    >> FILE *pipe = popen("date", "r");
    >>Why issue that, and the have all that parsing hassle. Just >>issue the date command with the appropriate parameters to >>get it to return the year only. See here.

    Your right, I actually only needed the last 2 digits for the current year which uses the "%y" parameter for date which you so graciously pointed me to

    >>If you haven't got popen(), a quick hack it to system() the date >>command and redirect the output to a file, then fopen() that >>file and read in the data. But I wouldn't recommend it (oops I >>just did! )
    I had thought about this initially but was curious to find out if I could get around generated a file and reading from it.

    Regardless, thanks for all of you help, looks as if my problem is solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM