using ctime() 
Author Message
 using 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.

--



Sat, 06 Oct 2001 03:00:00 GMT  
 using ctime()

Quote:
> Simply said you have to allocate a structure and call ctime() with the
> pointer to the structure and then ctime() returns the time values in the
> structure.

A code snippet explains it perhaps better:

        char *datum;
        time_t *tp;
        struct tm *t_ptr;

        datum = malloc(MAX_DATUM * sizeof(char));
        tp = malloc(sizeof(time_t));
        t_ptr = malloc(sizeof(struct tm));

        time(tp);

        t_ptr = localtime(tp);

Where t_ptr contains the values of the local time in the end.

Regards,
Richard

--



Sat, 06 Oct 2001 03:00:00 GMT  
 using ctime()

Quote:

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

Simply said you have to allocate a structure and call ctime() with the
pointer to the structure and then ctime() returns the time values in the
structure.

Quote:
> 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.

It's explained in appendix B in K&R 2nd edition, I can provide you an
example program that uses localtime() and returns the dutch value of the
current date, either independant or as a module.

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

As I understand ctime() conforms K&R C I guess it wil be ANSI too.

Regards,
Richard

--



Sat, 06 Oct 2001 03:00:00 GMT  
 using ctime()

Richard van den Berg schrieb in Nachricht ...

Quote:

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

>Simply said you have to allocate a structure and call ctime() with the
>pointer to the structure and then ctime() returns the time values in the
>structure.

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

>It's explained in appendix B in K&R 2nd edition, I can provide you an
>example program that uses localtime() and returns the dutch value of the
>current date, either independant or as a module.

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

>As I understand ctime() conforms K&R C I guess it wil be ANSI too.

>Regards,
>Richard

>--


Which date function you use depends on what you wnat to do whit date info. I
prefer localtime(), because I'm free in collecting an formatting the date.

If you don't like pointers, try this, it should clearly demonstrate the use
of ctime():

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

 int main(void)
 {
    time_t t;

    time(&t);
    printf("Today's date and time: %s\n", ctime(&t));
    return 0;
 }
<end code>

Regards
  G.Pohl

--



Mon, 08 Oct 2001 03:00:00 GMT  
 using ctime()

Quote:
>A code snippet explains it perhaps better:

> char *datum;
>        time_t *tp;
>        struct tm *t_ptr;

>        datum = malloc(MAX_DATUM * sizeof(char));
>        tp = malloc(sizeof(time_t));
>        t_ptr = malloc(sizeof(struct tm));

>        time(tp);

>        t_ptr = localtime(tp);

>Where t_ptr contains the values of the local time in the end.

Celeste asked for help to function ctime() and not localtime().

 Regards
  G.Pohl

--



Mon, 08 Oct 2001 03:00:00 GMT  
 using ctime()

Quote:

> >A code snippet explains it perhaps better:

> > char *datum;
> >        time_t *tp;
> >        struct tm *t_ptr;

> >        datum = malloc(MAX_DATUM * sizeof(char));
> >        tp = malloc(sizeof(time_t));
> >        t_ptr = malloc(sizeof(struct tm));

> >        time(tp);

> >        t_ptr = localtime(tp);

> >Where t_ptr contains the values of the local time in the end.
> Celeste asked for help to function ctime() and not localtime().

Yes, she asked this in relation to do some calculation with a given time
and the current time and it seems to me that it's done easier with the tm
structure members obtained with localtime(). Well now she has two
examples.

Regards,
Richard

--



Tue, 09 Oct 2001 03:00:00 GMT  
 using ctime()

Quote:

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

    time_t time0;
    char *time1;

    time0 = time(NULL);
    time1 = ctime(&time0);
    printf("%s",time1);

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

ctime() looks right for printing the time in ascii.

For blocking, I'd parse the input to a tm structure, convert it to a
time_t using mktime(), and get the difference using difftime().  The
block function is system dependent.

--

--



Tue, 09 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Need some explanation using ctime.

2. Using CTime

3. get systemtime without using CTime?

4. get systemtime without using CTime

5. error LNK2001: using CTime.Format

6. Convert to time_t without using CTime?

7. Using CTime Class in Windows DLL

8. Using CTime

9. Using CTime class in MFC DLL

10. RFX_DATE - How to enter a NULL Date using CTIME

11. using function ctime()

12. using date format having yy as century with CTime

 

 
Powered by phpBB® Forum Software