cftime function is not declared when compile by g++

Posted on 2004-11-15
Last Modified: 2013-12-26
#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++.

Question by:chumotan
    LVL 86

    Expert Comment

    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).
    LVL 86

    Expert Comment

    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.
    LVL 11

    Expert Comment

    There does seem to be a 'cftime()' function

    LVL 11

    Accepted Solution

    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>



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    Folder Comparison 12 50
    sameEnds challenge 3 78
    substring method in java 1 59
    mapBully challenge 6 51
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now