Difference b/w time() and localtime(), what happens inside time.h in library

Feb 22, 2010 at 11:33am
what is the difference between time() and localtime(), bold parts in code:

1
2
3
4
5
6
7
8
9
10
#include <time.h>

//inside main function
..
time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );


what exactly happens inside the library?


time() in library(time.h) just returns the decleared arguments:
1
2
3
4
static __inline time_t __CRTDECL time(time_t * _Time)
{
    return _time64(_Time);
}


where and when does the value get copied in _Time.

similarly for localtime():
1
2
3
4
5
6
7
static __inline struct tm * __CRTDECL localtime(const time_t * _Time)
{
#pragma warning( push )
#pragma warning( disable : 4996 )
    return _localtime64(_Time);
#pragma warning( pop )
}
Last edited on Feb 22, 2010 at 11:39am
Feb 22, 2010 at 1:19pm
time returns an integral type which has the number of seconds since an epoch.

localtime decodes a time integral type into a record (struct tm) that breaks out the date/time components for easy access. localtime fills in a static instance of struct tm within the runtime library (actually, there's one per thread) and returns it's address. You simply use it without having to check for NULL or releasing it.
Feb 23, 2010 at 5:16am
thanks for the explaination.

one more: when all this happens at runtime library why do we have two different methods one for getting the total seconds elapsed from pre-defined datetime, then again one for putting in struct format ...instead just one method call and we have *tm??

and also with the fact that there is no need to have user defined/ different struct tm format.. i think thts not even allowed to avoid clash with what already defined in library.....

or am i going deep into something unnecessary ..??
curiosity is embedded in homosapiens !!!

Feb 23, 2010 at 8:28am
localtime doesn't return the current time. It returns a record breaking the fields of the time_t passed in; which can be any time, not just the current time.

time() always returns the current time.

localtime has a corresponding function gmtime which returns the Universal Time translation (used to be called Greenwich Mean Time).

There's a family of time functions. It's worth taking a look at all of them.
Feb 23, 2010 at 9:28am
thanks. it was worth reading.
Topic archived. No new replies allowed.