HELP: C prog to get mm/dd/yy format

HELP: C prog to get mm/dd/yy format

Post by Chanh Nguy » Sun, 19 Feb 1995 01:41:55



Hi,

I am having a little problem. I want to get today date in mm/dd/yy format
and my codes returned wrong date. Please help if you know what is wrong
with my code or if you have done this before.

Chanh Nguyen

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
main()
{
  time_t clock;
  struct tm *tmptr;

  tmptr = localtime(&clock);
  printf("%d/%d/%d\n",tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_year);

Quote:}

 
 
 

HELP: C prog to get mm/dd/yy format

Post by David Ko » Wed, 22 Feb 1995 22:24:27


: I am having a little problem. I want to get today date in mm/dd/yy format
: and my codes returned wrong date. Please help if you know what is wrong
: with my code or if you have done this before.

:   tmptr = localtime(&clock);
:   printf("%d/%d/%d\n",tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_year);

You need to call gettimeofday() first.  This call gets the time and then
localtime() converts it based on the setting of the TZ variable.  

------------------------------------------------------------------
David Kohl                        HP-UX Workstation Expert Center

------------------------------------------------------------------

 
 
 

HELP: C prog to get mm/dd/yy format

Post by Ian Jon » Wed, 22 Feb 1995 23:48:01


|> Hi,
|>
|> I am having a little problem. I want to get today date in mm/dd/yy format
|> and my codes returned wrong date. Please help if you know what is wrong
|> with my code or if you have done this before.
|>
|> Chanh Nguyen
|>
|>
|> #include <stdio.h>
|> #include <string.h>
|> #include <sys/types.h>
|> #include <time.h>
|> main()
|> {
|>   time_t clock;
|>   struct tm *tmptr;
|>
|>   tmptr = localtime(&clock);
|>   printf("%d/%d/%d\n",tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_year);
|>
|> }
|>

Pardon me but wouldn't

         date +%m/%d/%y

do the job for you?

Ian

=========================================================================

"Insanity is hereditary - You get it from your kids"
=========================================================================

 
 
 

HELP: C prog to get mm/dd/yy format

Post by Michael J. Chinni (CCAC-S » Thu, 23 Feb 1995 03:25:11


|> Hi,
|>
|> I am having a little problem. I want to get today date in mm/dd/yy format
|> and my codes returned wrong date. Please help if you know what is wrong
|> with my code or if you have done this before.
|>
|> Chanh Nguyen
|>
|>
|> #include <stdio.h>
|> #include <string.h>
|> #include <sys/types.h>
|> #include <time.h>
|> main()
|> {
|>   time_t clock;
|>   struct tm *tmptr;
|>
|>   tmptr = localtime(&clock);
|>   printf("%d/%d/%d\n",tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_year);
|>
|> }
|>

You're missing code to SET the clock variable.  Localtime takes a time (such as returned by time()) and converts it into a structure.  It does not set this variable.  Yu need to add a call to time() (or gettimeofday()) before the call to localtime().

 
 
 

HELP: C prog to get mm/dd/yy format

Post by Nancy Rudi » Thu, 23 Feb 1995 03:21:11


This should do it:

 time_t tval;
 struct tm *tm_ptr, *localtime();
 char   buff[BUFSIZ];

 tval = time((time_t *) 0);
 tm_ptr = localtime(&tval);
 sprintf(buff,"%02d%02d%02d",tm_ptr->tm_year,++(tm_ptr->tm_mon),tm_ptr->tm_mday)
;
--
There's nothing you can post that can't be posted.
Nothing you can mail that can't be mailed.
Nowhere you can ftp that isn't where you're meant to ftp.

 
 
 

HELP: C prog to get mm/dd/yy format

Post by apak.. » Fri, 03 Mar 1995 18:36:59



> I am having a little problem. I want to get today date in mm/dd/yy format
> and my codes returned wrong date. Please help if you know what is wrong
> with my code or if you have done this before.

Why don't you just invoke the UNIX 'date' program?

..
system("/bin/date +m/d/y > /tmp/foo_date");
FILE *f=fopen("/tmp/foo_date", "r");
fgets(f, 25, buffer);
fclose f;
system("/bin/rm /tmp/foo_date");

If you're application isn't time critical, this is real simple.
Or better yet, write in PERL!

Alex
----

 
 
 

HELP: C prog to get mm/dd/yy format

Post by David Reeve » Tue, 07 Mar 1995 05:49:06




> > I am having a little problem. I want to get today date in mm/dd/yy format
> > and my codes returned wrong date. Please help if you know what is wrong
> > with my code or if you have done this before.

> Why don't you just invoke the UNIX 'date' program?

> ...
> system("/bin/date +m/d/y > /tmp/foo_date");
> FILE *f=fopen("/tmp/foo_date", "r");
> fgets(f, 25, buffer);
> fclose f;
> system("/bin/rm /tmp/foo_date");

> If you're application isn't time critical, this is real simple.
> Or better yet, write in PERL!

> Alex
> ----


I believe it would be more efficient to use the time syscall and
the localtime and a little dash of formating for the mm/dd/yy
output. :)

No need to perform high overhead system calls w/ temp file production.

 
 
 

1. Changing all date fields from mm/dd/yy to dd/mm/yy

Please, I'm looking for a way to change all date fields in a specified
file, like this:

solaris> cat file

05/23/06      03/12/06

07/10/06                   11/12/06           09/20/06

      03/11/06

I need change all date field from mm/dd/yy to dd/mm/yy, like this:

solaris> ./script < cat file

23/05/06        12/03/06

10/07/06                 12/11/06             20/09/06

       11/03/06

Thanks !

2. can't print cause of dvips!!!

3. How to translate date in dd/mm/yy format

4. RPM upgrade problem

5. Files modified since yy/mm/dd hh:mm

6. Stats comp.os.linux.networking (last 7 days)

7. how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

8. 2 UNIX Boot Questions

9. sort MM/DD/YY in reverse chron order with -k...

10. Sort by First Date Field (MM/DD/YY)

11. How to convert dd/mm/yy to unix epoch time?

12. Need f77 idate(mm,dd,yy) function

13. How to convert Posix date to YY/MM/DD ?