using function ctime() 
Author Message
 using function ctime()

Hello,

Has anyone used the function ctime() in one of their programs?  Could you
please tell me how you used it?  I don't quite understand how ctime() works
when called in a program...

I'm to write a function that takes in a time specified by the user (e.g.
12:00), output the current time (using ctime()  <--???), and block for the
number of seconds difference between the two.

Is there a better time function that I can use other than ctime() ?

Thank you very much.



Fri, 05 Oct 2001 03:00:00 GMT  
 using function ctime()
Read about ctime in your compiler's documentation. Then create some test
code to see how it works. Once you have some code, we will be happy to help
you.
--

Paul Lutus
www.arachnoid.com

Quote:

>Hello,

>Has anyone used the function ctime() in one of their programs?  Could you
>please tell me how you used it?  I don't quite understand how ctime() works
>when called in a program...

>I'm to write a function that takes in a time specified by the user (e.g.
>12:00), output the current time (using ctime()  <--???), and block for the
>number of seconds difference between the two.

>Is there a better time function that I can use other than ctime() ?

>Thank you very much.



Fri, 05 Oct 2001 03:00:00 GMT  
 using function ctime()
Here is one use of ctime().  I will leave the rest for you to figure out.

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

void printtime()
{
    long time(),now;
    char *ctime();

    time(&now);

    printf("Time is %s",ctime(&now));

Quote:
}

--
****************************************
Richard Armstrong
State Of The Art Consulting, Inc.
http://www.stateoart.com
****************************************

Quote:
> Hello,

> Has anyone used the function ctime() in one of their programs?  Could you
> please tell me how you used it?  I don't quite understand how ctime()
works
> when called in a program...

> I'm to write a function that takes in a time specified by the user (e.g.
> 12:00), output the current time (using ctime()  <--???), and block for the
> number of seconds difference between the two.

> Is there a better time function that I can use other than ctime() ?

> Thank you very much.



Sat, 06 Oct 2001 03:00:00 GMT  
 using function ctime()
Thank you very much for your replies.  I didn't know if I'll get the answer
correctly if I do this (which is what I came up with) and that's why I
wanted to know more about the function.  Could you please take a look at
this code and point out my mistakes?  I'm sure there are plenty, if not
entirely wrong...

I expect the user to type:   ./timetest.out   10:00     (for example) to
start the program.  If the system time is 9:59, the program will block for
60 seconds and then indicate that it has woken up after this time has
elapsed...

#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

int argc; char *argv[];
main (argc, argv)
{
    char usertime[7];
    char *currtime;
    int om, tm, oh, th;
    int diff, usersec, currsec;

    if (argc != 2) {
        printf(stderr, "%s: Usage:  timetest.out  <time>\n", argv[0]);
        exit(0);
    }

    strcpy(usertime, argv[1]);
    currtime = ctime();

    printf("It is now %s\n", currtime);

    om = atoi(currtime[15]);
    tm = atoi(currtime[14]);
    oh = atoi(currtime[12]);
    th = atoi(currtime[11]);
    currsec = (om*60) + (tm*600) + (oh*3600) + (th*36000);

    om = atoi(usertime[4]);
    tm = atoi(usertime[3]);
    oh = atoi(usertime[1]);
    th = atoi(usertime[0]);
    usersec = (om*60) + (tm*600) + (oh*3600) + (th*36000);

    diff = usersec - currsec;

    printf("I will now block %d seconds until %s\n", diff, usertime);
    sleep(diff);

    printf("I just woke up.\n");
    currtime = ctime();
    printf("It is now %s\n", currtime);
    printf("I will now proceed to Step 2.\n");

    return 0;

Quote:
}



Sun, 07 Oct 2001 03:00:00 GMT  
 using function ctime()

Quote:

> Here is one use of ctime().  I will leave the rest for you to figure out.

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

> void printtime()
> {
>     long time(),now;
>     char *ctime();

1. The 'now' should be of time_t typetime_t now;

2. The correct prototype of time() is
time_t time( time_t *timer );

3. The correct prototype of ctime() is
char *ctime( const time_t *time );

Quote:
>     time(&now);

>     printf("Time is %s",ctime(&now));
> }

Try this:

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

int main( void )
{
  time_t now;

  time( &now );
  printf( "Today's date and time: %s\n", ctime( &now ) );
  return 0;

Quote:
}



Mon, 08 Oct 2001 03:00:00 GMT  
 using function ctime()
Groovy hepcat Richard Armstrong was jivin' on Tue, 20 Apr 1999
15:14:23 -0600 in comp.lang.c.
Re: using function ctime()'s a cool scene! Dig it!

Quote:
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>

  stdlib.h and string.h are not needed for this function.

Quote:
>#include <time.h>

>void printtime()
>{
>    long time(),now;

  Would you like to have another go at that? Since time() is already
declared in time.h, and returns a time_t, not (necessarily) a long,
this is wrong. The variable now will also need to be a time_t. So try
this instead:

    time_t now;

Quote:
>    char *ctime();

>    time(&now);
>    printf("Time is %s",ctime(&now));

  And date, and day of the week.
Quote:
>}

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 12 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. using ctime()

2. Need some explanation using ctime.

3. Looking for ctime's inverse function

4. using date format having yy as century with CTime

5. CTIME using

6. Using CTime

7. Can CTime be used in a DLL

8. get systemtime without using CTime?

9. get systemtime without using CTime

10. Linker Error on asctime, ctime, localtime functions...

11. error LNK2001: using CTime.Format

12. Convert to time_t without using CTime?

 

 
Powered by phpBB® Forum Software