cftime function is not declared when compile by g++

#include <stdio.h>
#include <time.h>
main() {

                char  temp[2048];
                char  timestamp[20];
                struct timeval  timeval;
        gettimeofday( &timeval, 0 );
        cftime( timestamp, (char *)"%h %d %H:%M:%S", &(timeval.tv_sec) );

Then the folloing errors output when i compile by using g++,
>g++ -g In function `int main()': error: `cftime' undeclared (first use this function) error: (Each undeclared identifier is reported only once for each fu
nction it appears in.)

This is not a problem if i use gcc and reanme the program from to test.c.
However, i need to use g++.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is no such thing as 'cftime()' - I assume you meant 'ctime()':

CTIME(3)            Linux Programmer's Manual            CTIME(3)

       asctime,  ctime,  gmtime,  localtime,  mktime  - transform
       binary date and time to ASCII

       #include <time.h>

       char *asctime(const struct tm *timeptr);

       char *ctime(const time_t *timep);

       struct tm *gmtime(const time_t *timep);

       struct tm *localtime(const time_t *timep);

       time_t mktime(struct tm *timeptr);

       extern char *tzname[2];

So, change

        cftime( timestamp, (char *)"%h %d %H:%M:%S", &(timeval.tv_sec) );

to read

        ctime( timestamp, (char *)"%h %d %H:%M:%S", &(timeval.tv_sec) );

and it should work.

       long int timezone;
       extern int daylight;

       The ctime(), gmtime() and localtime() functions  all  take
       an  argument of data type time_t which represents calendar
       time.  When interpreted as an absolute time value, it rep­
       resents  the  number  of seconds elapsed since 00:00:00 on
       January 1, 1970, Coordinated Universal Time (UTC).
Ooops, sorry, editing error - apart from the man page, that should have beeen

There is no such thing as 'cftime()' - I assume you meant 'ctime()'
So, change

       cftime( timestamp, (char *)"%h %d %H:%M:%S", &(timeval.tv_sec) );

to read

       ctime( timestamp, (char *)"%h %d %H:%M:%S", &(timeval.tv_sec) );

and it should work.
There does seem to be a 'cftime()' function

I wonder if the following would work

1) use
extern "C" {
#include <time.h>

instead of the    #include <time.h>


#include <time>

instead of  #include <time.h>



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.