How to get the current date information only?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • YiMkiE

    How to get the current date information only?

    I am a newbie of C and I need to do a program to get the current date
    information only, without the time. I have my code here:

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

    int main ()
    {
    time_t rawtime;
    struct tm * timeinfo;
    char* t;

    rawtime = time (NULL);
    timeinfo = localtime (&rawtime);
    t = asctime(timeinf o);
    printf(t);

    return 0;
    }

    How to output only the year, month and day of t?

  • Ian Collins

    #2
    Re: How to get the current date information only?

    YiMkiE wrote:
    I am a newbie of C and I need to do a program to get the current date
    information only, without the time. I have my code here:
    >
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    >
    int main ()
    {
    time_t rawtime;
    struct tm * timeinfo;
    char* t;
    >
    rawtime = time (NULL);
    timeinfo = localtime (&rawtime);
    t = asctime(timeinf o);
    printf(t);
    >
    return 0;
    }
    >
    How to output only the year, month and day of t?
    >
    Use the appropriate fields in 'timeinfo'.

    --
    Ian Collins.

    Comment

    • Robert Gamble

      #3
      Re: How to get the current date information only?

      YiMkiE wrote:
      I am a newbie of C and I need to do a program to get the current date
      information only, without the time. I have my code here:
      >
      #include <stdio.h>
      #include <time.h>
      #include <string.h>
      >
      int main ()
      {
      time_t rawtime;
      struct tm * timeinfo;
      char* t;
      >
      rawtime = time (NULL);
      timeinfo = localtime (&rawtime);
      t = asctime(timeinf o);
      printf(t);
      >
      return 0;
      }
      >
      How to output only the year, month and day of t?
      Lookup the strftime function.

      Robert Gamble

      Comment

      • YiMkiE

        #4
        Re: How to get the current date information only?

        Do you mean the tm_year? How to refer to that?
        I know this is a stupid question..

        Ian Collins 寫道:
        YiMkiE wrote:
        I am a newbie of C and I need to do a program to get the current date
        information only, without the time. I have my code here:

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

        int main ()
        {
        time_t rawtime;
        struct tm * timeinfo;
        char* t;

        rawtime = time (NULL);
        timeinfo = localtime (&rawtime);
        t = asctime(timeinf o);
        printf(t);

        return 0;
        }

        How to output only the year, month and day of t?
        Use the appropriate fields in 'timeinfo'.

        --
        Ian Collins.

        Comment

        • Nelu

          #5
          Re: How to get the current date information only?

          "YiMkiE" <yimkie@gmail.c omwrites:
          I am a newbie of C and I need to do a program to get the current date
          information only, without the time. I have my code here:
          >
          #include <stdio.h>
          #include <time.h>
          #include <string.h>
          >
          int main ()
          {
          time_t rawtime;
          struct tm * timeinfo;
          char* t;
          >
          rawtime = time (NULL);
          timeinfo = localtime (&rawtime);
          t = asctime(timeinf o);
          printf(t);
          >
          return 0;
          }
          >
          How to output only the year, month and day of t?
          >
          You either can lookup the members of the struct tm structure
          and display only the fields you want or you can use the strftime
          function that generates formatted text using different date/time
          format specifiers.

          --
          Ioan - Ciprian Tandau
          tandau _at_ freeshell _dot_ org (hope it's not too late)
          (... and that it still works...)

          Comment

          • Morris Dovey

            #6
            Re: How to get the current date information only?

            YiMkiE (in 1152151850.5605 07.233480@a14g2 00...legr oups.com)
            said:

            | I am a newbie of C and I need to do a program to get the current
            | date information only, without the time. I have my code here:
            |
            | #include <stdio.h>
            | #include <time.h>
            | #include <string.h>
            |
            | int main ()
            | {
            | time_t rawtime;
            | struct tm * timeinfo;
            | char* t;
            |
            | rawtime = time (NULL);
            | timeinfo = localtime (&rawtime);
            | t = asctime(timeinf o);
            | printf(t);
            |
            | return 0;
            | }
            |
            | How to output only the year, month and day of t?

            Just write your own function to build the string you want:

            char *YiMkiE_date(co nst struct tm *t)
            { char *mon_name[12] =
            { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            };
            static char date[] = "YYYY MMM DD\n";

            sprintf(date,"% d %s %d\n", 1900 + t->tm_year,
            mon_name[t->tm_mon], t->tm_mday);
            return date;
            }

            Easy, isn't it?

            --
            Morris Dovey
            DeSoto Solar
            DeSoto, Iowa USA



            Comment

            • YiMkiE

              #7
              Re: How to get the current date information only?

              Thanks all, I have solved the date problem. But now I am getting
              another trouble.
              I need to concatenate a string and the date string I got to form a file
              name.

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

              int main()
              {
              int e;
              FILE *fp;
              char fname[13] = "AMH_TdySal es-";
              time_t rawtime;
              struct tm * timeinfo;
              char t[9];

              rawtime = time (NULL);
              timeinfo = localtime (&rawtime);
              strftime(t ,10 , "%Y%m%d", timeinfo);

              strncat(fname, t, 8);
              printf(fname);

              fp = fopen(fname, "r");
              if (fp == NULL)
              e = 0;
              fclose(fp);

              fp = fopen("log.txt" , "a");
              if (e == 0)
              fprintf(fp, "%s - File not found!!\n", fname);
              fclose(fp);

              return 0;
              }

              After concatenating the two, the end of line (or array?) character
              appears in the middle, causing error. How to remove that?

              Comment

              • jjf@bcs.org.uk

                #8
                Re: How to get the current date information only?


                Morris Dovey wrote:
                YiMkiE (in 1152151850.5605 07.233480@a14g2 00...legr oups.com)
                said:
                >
                | I am a newbie of C and I need to do a program to get the current
                | date information only, without the time. I have my code here:
                |
                | #include <stdio.h>
                | #include <time.h>
                | #include <string.h>
                |
                | int main ()
                | {
                | time_t rawtime;
                | struct tm * timeinfo;
                | char* t;
                |
                | rawtime = time (NULL);
                | timeinfo = localtime (&rawtime);
                | t = asctime(timeinf o);
                | printf(t);
                |
                | return 0;
                | }
                |
                | How to output only the year, month and day of t?
                >
                Just write your own function to build the string you want:
                >
                char *YiMkiE_date(co nst struct tm *t)
                { char *mon_name[12] =
                { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
                };
                static char date[] = "YYYY MMM DD\n";
                >
                sprintf(date,"% d %s %d\n", 1900 + t->tm_year,
                mon_name[t->tm_mon], t->tm_mday);
                return date;
                }
                >
                Easy, isn't it?
                Not compared with using strftime(), which also looks after locales for
                you. Why re-invent the wheel?

                Comment

                • Chris Torek

                  #9
                  Re: How to get the current date information only?

                  In article <1152165638.293 271.208950@p79g 2000cwp.googleg roups.com>
                  YiMkiE <yimkie@gmail.c omwrote:
                  >#include <stdio.h>
                  >#include <time.h>
                  >#include <string.h>
                  >
                  >int main()
                  >{
                  int e;
                  FILE *fp;
                  char fname[13] = "AMH_TdySal es-";
                  This array has room for 13 "char"s. The initializer:

                  "AMH_TdySal es-\0"
                  0 1
                  1234567890123** *ran off the end here

                  *does* fit, just barely, by C's rule that says that the terminating
                  '\0' of a string literal initializer is discarded when initializing
                  an array whose size is just big enough to hold everything except the
                  '\0'.

                  Or, to put it another way, had you written:

                  char fname[] = "AMH_TdySal es-";

                  the array would have size *14*, not 13.

                  The end result is that fname[] contains a sequence of bytes that
                  is *not* terminated with a '\0', and is therefore not a string
                  (by definition).
                  time_t rawtime;
                  struct tm * timeinfo;
                  char t[9];
                  The array "t" has size 9 (in "C bytes", aka chars).
                  rawtime = time (NULL);
                  timeinfo = localtime (&rawtime);
                  strftime(t ,10 , "%Y%m%d", timeinfo);
                  The array "t" has size 9. Why did you tell strftime() to write at
                  most *ten* characters into a 9-character array? However, %Y needs
                  4, %m needs 2, and %d needs 2; and 4+2+2 = 8 -- so strftime() will
                  write the appropriate 8 characters, then add a ninth '\0' character
                  to make the result a string, and this does fit.
                  strncat(fname, t, 8);
                  The strncat() function needs its first argument to be a string --
                  a sequence of "char"s terminated by a '\0' character. fname does
                  not hold a string, so the effect is undefined.

                  Even if it were OK, this tells strncat to add at most 8 characters
                  to the original string. If the original string has 13 non-'\0'
                  characters followed by a '\0', the resulting string will have 13+8
                  = 21 non-'\0' characters followed by a '\0'. So it needs at least
                  22 bytes of space.
                  --
                  In-Real-Life: Chris Torek, Wind River Systems
                  Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                  email: forget about it http://web.torek.net/torek/index.html
                  Reading email is like searching for food in the garbage, thanks to spammers.

                  Comment

                  • YiMkiE

                    #10
                    Re: How to get the current date information only?

                    Solved!
                    Thanks very much Chris!

                    Chris Torek 寫道:
                    In article <1152165638.293 271.208950@p79g 2000cwp.googleg roups.com>
                    YiMkiE <yimkie@gmail.c omwrote:
                    #include <stdio.h>
                    #include <time.h>
                    #include <string.h>

                    int main()
                    {
                    int e;
                    FILE *fp;
                    char fname[13] = "AMH_TdySal es-";
                    >
                    This array has room for 13 "char"s. The initializer:
                    >
                    "AMH_TdySal es-\0"
                    0 1
                    1234567890123** *ran off the end here
                    >
                    *does* fit, just barely, by C's rule that says that the terminating
                    '\0' of a string literal initializer is discarded when initializing
                    an array whose size is just big enough to hold everything except the
                    '\0'.
                    >
                    Or, to put it another way, had you written:
                    >
                    char fname[] = "AMH_TdySal es-";
                    >
                    the array would have size *14*, not 13.
                    >
                    The end result is that fname[] contains a sequence of bytes that
                    is *not* terminated with a '\0', and is therefore not a string
                    (by definition).
                    >
                    time_t rawtime;
                    struct tm * timeinfo;
                    char t[9];
                    >
                    The array "t" has size 9 (in "C bytes", aka chars).
                    >
                    rawtime = time (NULL);
                    timeinfo = localtime (&rawtime);
                    strftime(t ,10 , "%Y%m%d", timeinfo);
                    >
                    The array "t" has size 9. Why did you tell strftime() to write at
                    most *ten* characters into a 9-character array? However, %Y needs
                    4, %m needs 2, and %d needs 2; and 4+2+2 = 8 -- so strftime() will
                    write the appropriate 8 characters, then add a ninth '\0' character
                    to make the result a string, and this does fit.
                    >
                    strncat(fname, t, 8);
                    >
                    The strncat() function needs its first argument to be a string --
                    a sequence of "char"s terminated by a '\0' character. fname does
                    not hold a string, so the effect is undefined.
                    >
                    Even if it were OK, this tells strncat to add at most 8 characters
                    to the original string. If the original string has 13 non-'\0'
                    characters followed by a '\0', the resulting string will have 13+8
                    = 21 non-'\0' characters followed by a '\0'. So it needs at least
                    22 bytes of space.
                    --
                    In-Real-Life: Chris Torek, Wind River Systems
                    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                    email: forget about it http://web.torek.net/torek/index.html
                    Reading email is like searching for food in the garbage, thanks to spammers.

                    Comment

                    • Keith Thompson

                      #11
                      Re: How to get the current date information only?

                      "YiMkiE" <yimkie@gmail.c omwrites:
                      Solved!
                      Thanks very much Chris!
                      Please read <http://www.caliburn.nl/topposting.html >. Thanks.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                      Comment

                      • YiMkiE

                        #12
                        Re: How to get the current date information only?


                        Keith Thompson 寫道:
                        "YiMkiE" <yimkie@gmail.c omwrites:
                        Solved!
                        Thanks very much Chris!
                        >
                        Please read <http://www.caliburn.nl/topposting.html >. Thanks.
                        >
                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.
                        ooops, sorry!
                        will pay attention to this!

                        Comment

                        • Default User

                          #13
                          Re: How to get the current date information only?

                          YiMkiE wrote:
                          >
                          Keith Thompson 寫道:
                          >
                          "YiMkiE" <yimkie@gmail.c omwrites:
                          Solved!
                          Thanks very much Chris!
                          Please read <http://www.caliburn.nl/topposting.html >. Thanks.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org
                          <http://www.ghoti.net/~kstSan Diego Supercomputer Center
                          <* <http://users.sdsc.edu/~kstWe must do something. This is
                          something. Therefore, we must do this.
                          >
                          ooops, sorry!
                          will pay attention to this!
                          Also trim the quoted material. In particular, remove .sigs (the bits
                          after the --). A good newsreader does that for you, alas you are using
                          Google so you have to do it manually.



                          Brian

                          Comment

                          • YiMkiE

                            #14
                            Re: How to get the current date information only?

                            I am using google. How to read this group in outlook express or some
                            other newsreader? (though this may be a bit off topic)

                            Comment

                            • Dann Corbit

                              #15
                              Re: How to get the current date information only?

                              "YiMkiE" <yimkie@gmail.c omwrote in message
                              news:1152241312 .748381.107470@ k73g2000cwa.goo glegroups.com.. .
                              >I am using google. How to read this group in outlook express or some
                              other newsreader? (though this may be a bit off topic)
                              <totally OT response>
                              Click on one of these:

                              </totally OT response>


                              Comment

                              Working...