help-get current date using time.h 
Author Message
 help-get current date using time.h

i belive that the following code mite work but do not relea know.

tm* current_time;

time_t* time_ptr;

time(time_ptr);
current_time=locattime(time_ptr);

I do not know if the time function from time.h is correct marcus..



Mon, 02 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:

> i belive that the following code mite work but do not relea know.

There are a few problems.

Quote:

> tm* current_time;

declare as:struct tm* current_time;

Quote:

> time_t* time_ptr;

time() does not provide a shared data object (static).You most provide
time with a pointer to an object.
so declare:
time_t day;

Quote:

> time(time_ptr);

And here do:time(&day);

Quote:
> current_time=locattime(time_ptr);

current_time = localtime(&day);

Quote:

> I do not know if the time function from time.h is correct marcus..

 Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
   struct tm *t;
   time_t day;

   time(&day);
   if(day == (time_t)-1) exit(EXIT_FAILURE);
   t = localtime(&day);
   printf("It's %s",asctime(t));
   return 0;
   }

Also, refer to faq 13.12

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Tue, 03 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:

> > time_t* time_ptr;

> > time(time_ptr);
> check for a reurn value of -1 to ensure time has been read
>   if((time_t)-1 == time(time_ptr))

Although many of the Standard C functions dealing with timehave shared data
objects,  for struct tm * and char *, none
including time() are defined as having shared data data objects
for time_t. The argument for time() must be a null pointer or
a pointer to a type time_t object.  the above will fail because
the argument, time_ptr, is not pointing any object. So, instead
of declaring a time_t pointer declare an object:
time_t now;
then use the & operator (address of).
if((time_t)-1 == time(&now))

Quote:
>   {
>       /* no time available */
>   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html


Wed, 04 Apr 2001 03:00:00 GMT  
 help-get current date using time.h
See example

int main(void)
{
  time_t t;
  struct time d_time;
  struct date d_date;
  struct tm *local;

  getdate(&d_date);
  gettime(&d_time);

  t = dostounix(&d_date, &d_time);
  local = localtime(&t);
  printf("Time and Date: %s\n", asctime(local));

Quote:
}

> i belive that the following code mite work but do not relea know.

> tm* current_time;

> time_t* time_ptr;

> time(time_ptr);
> current_time=locattime(time_ptr);

> I do not know if the time function from time.h is correct marcus..



Thu, 05 Apr 2001 03:00:00 GMT  
 help-get current date using time.h


Quote:
>See example

#include <time.h>

Quote:
>int main(void)
>{
>  time_t t;
>  struct time d_time;
>  struct date d_date;
>  struct tm *local;

>  getdate(&d_date);
>  gettime(&d_time);

>  t = dostounix(&d_date, &d_time);

These three functions are non-standard (as are struct time and struct date).
time() is standard and therefore the better alternative. It happens to be
simpler here too:

   t = time(NULL);

Quote:
>  local = localtime(&t);
>  printf("Time and Date: %s\n", asctime(local));

It is probably worth noting that the string generated by asctime() (and
ctime() ) includes a new-line character at the end. So it would probably
be better to omit the one in the printf format string.

   return 0;

Quote:
>}


>> i belive that the following code mite work but do not relea know.

>> tm* current_time;

>> time_t* time_ptr;

>> time(time_ptr);

This is an error in C irrespective of what the time() function does.
You've defined an uninitialised variable time_ptr and are then reading
its uninitialised (and therefore indeterminate) value in order to pass
it to time(). Reading the value of an uninitialised variable is always
an error. What time() requires that you pass it is either a pointer to
a valid time_t object where it can write a value, or a null pointer.
So you could write this as:

   time_t* t;

   time(&t);

Quote:
>> current_time=locattime(time_ptr);

   current_time=locattime(&t);

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


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



Thu, 05 Apr 2001 03:00:00 GMT  
 help-get current date using time.h
Groovy hepcat Marcus Wunderlich was jivin' on 15 Oct 1998 16:57:17 GMT
in comp.lang.c.
help-get current date using time.h's a cool scene! Dig it!

Quote:
>tm* current_time;

  There is no tm type. Try struct tm instead.

Quote:
>time_t* time_ptr;

>time(time_ptr);

  time_ptr must be initialised for this to work. A better way is to
create a time_t, not a time_t*, and simply assign the return from
time() to it. Pass NULL as an argument to time().

time_t t;
t = time(NULL);

Quote:
>current_time=locattime(time_ptr);

  current_time will now contain a struct containing members
representing different parts of the local time and date. struct tm is
defined as follows:

struct tm
{
  int tm_sec;    /* seconds after the minute (0 - 60) */
  int tm_min;    /* minutes after the hour (0 - 60) */
  int tm_hour;   /* hours since midnight (0 - 23) */
  int tm_mday;   /* day of month (0 - 31) */
  int tm_mon;    /* month since January (0 - 11) */
  int tm_year;   /* years since 1900 */
  int tm_wday;   /* weekday (days since sunday) (0 - 6) */
  int tm_yday;   /* day since 1st january (0 - 365) */
  int tm_isdst;  /* daylight saving flag (0 = standard time, +ve =
daylight saving time, -ve = not available) */

Quote:
}

  So, to display the current local time, you could do something like
this:

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

int main(void)
{
  time_t t;
  struct tm *lt;

  t = time(NULL);
  lt = localtime(&t);

  printf("The time (HH:MM:SS) is %02d:%02d:%02d.\n", lt->tm_hour,
lt->tm_min, lt->tm_sec);

  return 0;

Quote:
}

--

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

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



Sun, 08 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:

> >> tm* current_time;

> >> time_t* time_ptr;

> >> time(time_ptr);

> This is an error in C irrespective of what the time() function does.
> You've defined an uninitialised variable time_ptr and are then reading
> its uninitialised (and therefore indeterminate) value in order to pass
> it to time(). Reading the value of an uninitialised variable is always
> an error. What time() requires that you pass it is either a pointer to
> a valid time_t object where it can write a value, or a null pointer.
> So you could write this as:

>    time_t* t;

>    time(&t);

> >> current_time=locattime(time_ptr);

>    current_time=locattime(&t);

Your code example does not follow your explanation. t still refers to an
uninitialized value to time. In effect, you have repeated the original
error with the addition of supplying a pointer to a pointer argument to
time(). Also, I know the chances are slim, but time() may put a value,
(time_t)-1, that is not a measurement of time but an indicator that
time is not available. I thing it is good practice to check this value
before you supply it as an argument to localtime().
So, one way to write this:
#include <time.h>
#include <stdio.h>
....
struct tm *current_time;
time_t t; /* not a pointer */

if((t = time(NULL)) == (time_t)-1) {
   /* TODO: code to deal with unavailable time */
   }
else {
   current_time = localtime(&t);
   printf("It's %s",asctime(current_time));
   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Mon, 09 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:


> > >> tm* current_time;

> > >> time_t* time_ptr;

> > >> time(time_ptr);

> > This is an error in C irrespective of what the time() function does.
> > You've defined an uninitialised variable time_ptr and are then reading
> > its uninitialised (and therefore indeterminate) value in order to pass
> > it to time(). Reading the value of an uninitialised variable is always
> > an error. What time() requires that you pass it is either a pointer to
> > a valid time_t object where it can write a value, or a null pointer.
> > So you could write this as:

> >    time_t* t;

> >    time(&t);

> > >> current_time=locattime(time_ptr);

> >    current_time=locattime(&t);

> Your code example does not follow your explanation. t still refers to an
> uninitialized value to time. In effect, you have repeated the original

[snip]

t is uninitialized when time() is called. But this does not matter, since
the address of t is passed to time(), rather than t itself. This, &t, IS a
variable of definite and useful value, even though t is not, because &t
points to the start of an allocated object of appropriate type.

Daniel Barker,
Institute of Cell and Molecular Biology,
Swann Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
UK



Mon, 09 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:

> > >    time_t* t;

> > >    time(&t);

> > > >> current_time=locattime(time_ptr);

> > >    current_time=locattime(&t);

> > Your code example does not follow your explanation. t still refers to an
> > uninitialized value to time. In effect, you have repeated the original

> [snip]

> t is uninitialized when time() is called. But this does not matter, since
> the address of t is passed to time(), rather than t itself. This, &t, IS a
> variable of definite and useful value, even though t is not, because &t
> points to the start of an allocated object of appropriate type.

In the above declaration,
time_t* t;
t is not an object. t is a pointer. The pointer has not initialized to point
to an
object of type time_t. It can point anywhere. In this case time() will result
in
behavior undefined. It may work or it may not. I would think the compiler
will give warnings of incompatiable types between expected argument type
and actual argument type.

Since the poster included the correct explanation, I believe the code
example was mistyped and the declaration was meant to be:
time_t t;

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Tue, 10 Apr 2001 03:00:00 GMT  
 help-get current date using time.h

Quote:

> > >    time_t* t;
> > >    time(&t);
> > > >> current_time=locattime(time_ptr);

> > >    current_time=locattime(&t);

> t is uninitialized when time() is called. But this does not matter, since
> the address of t is passed to time(), rather than t itself. This, &t, IS a
> variable of definite and useful value, even though t is not, because &t
> points to the start of an allocated object of appropriate type.

Take another look at the declaration of the time_t variable t. Hopefully,you
will notice that t has been declared as a pointer. This being the
case, using &t as an argument in time() and localtime() is an error.
Your comments would have been accurate if t was declared as:

time_t t;

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Wed, 11 Apr 2001 03:00:00 GMT  
 help-get current date using time.h


Quote:


>> > >    time_t* t;

>> > >    time(&t);

>> > > >> current_time=locattime(time_ptr);

>> > >    current_time=locattime(&t);

>> > Your code example does not follow your explanation. t still refers to an
>> > uninitialized value to time. In effect, you have repeated the original

Yes, an extraneous * appears to have crept in in time_t* t; I must have
copied it since it isn't my normal formatting style.

Quote:
>> t is uninitialized when time() is called. But this does not matter, since
>> the address of t is passed to time(), rather than t itself. This, &t, IS a
>> variable of definite and useful value, even though t is not, because &t
>> points to the start of an allocated object of appropriate type.

>In the above declaration,
>time_t* t;
>t is not an object. t is a pointer. The pointer has not initialized to point
>to an

Pointers are perfectly good objects: all pointer types are object types.

Quote:
>object of type time_t. It can point anywhere. In this case time() will result
>in
>behavior undefined.

Since &t is being passed it would result in a constraint violation assuming
time() has been properly prototyped.

Quote:
>It may work or it may not. I would think the compiler
>will give warnings of incompatiable types between expected argument type
>and actual argument type.

>Since the poster included the correct explanation, I believe the code
>example was mistyped and the declaration was meant to be:
>time_t t;

Correct. :-)

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


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



Thu, 26 Apr 2001 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Getting the current date/time

2. Displaying the current time and date in a dialog using COleDateTime

3. Touching a file (set to current time and date)

4. Question about Get Server Current Date and time, thank you

5. current date time

6. current date/time format question ????

7. Get current time and date form an Server

8. Need help creating filenames using date and time

9. C time/date to Visual Basic time/date

10. Date Time Picker - Date AND Time

11. Getting current system time in milliseconds

12. getting time/date

 

 
Powered by phpBB® Forum Software