about time and time_t
Hello everyone,
In MSDN for time function,
time_t time(
time_t *timer
);
http://msdn2.microsoft.com/en-us/library/1f4c8f33(VS.80).aspx
the return value of time is time_t, but in the Return Value section, it mentioned,
Return the time as seconds elapsed since midnight, January 1, 1970. There is no error return.
So, I am wondering whether time_t could be represented as elapsed number of seconds correctly and safely -- no data lost if we convert return value to unsigned long?
Like,
unsigned long second = time (NULL);
thanks in advance,
George
[643 byte] By [
George2] at [2007-11-20 11:44:07]

# 1 Re: about time and time_t
In fact time_t is a typedef of either int or long, I dont remember right now. You can safely convert time_t to (unsigned) long and vice versa.
# 2 Re: about time and time_t
Thanks GNiewerth,
In fact time_t is a typedef of either int or long, I dont remember right now. You can safely convert time_t to (unsigned) long and vice versa.
Your answer is helpful.
regards,
George
# 3 Re: about time and time_t
There is a reason why there are separate types like time_t and size_t, so I would always recommend not casting to maintain portability. size_t is equivalent to unsigned long on most of todays systems, both 32bit and 64bit, but there is no guarantee it is and will be like that on all system. time_t will have to be 64bit for any system that wants to survive the next 30 years, but not every system will have to implement a 32bit long type and I am sure there will be some embedded systems which will not.
Why do you want to cast it anyways, why not use time_t whenever you have a time_t?
# 4 Re: about time and time_t
Thanks treuss,
There is a reason why there are separate types like time_t and size_t, so I would always recommend not casting to maintain portability. size_t is equivalent to unsigned long on most of todays systems, both 32bit and 64bit, but there is no guarantee it is and will be like that on all system. time_t will have to be 64bit for any system that wants to survive the next 30 years, but not every system will have to implement a 32bit long type and I am sure there will be some embedded systems which will not.
Why do you want to cast it anyways, why not use time_t whenever you have a time_t?
Because I mixed time_t with other numeric data types (in some numeric formula), like long, so I have to do the conversion. Any comments?
regards,
George
# 5 Re: about time and time_t
Why not convert long to time_t first ?
# 6 Re: about time and time_t
Because I mixed time_t with other numeric data types (in some numeric formula), like long, so I have to do the conversion. Any comments?I wouldn't say so. You can add any integer to a time_t and the result will again be time_t. If you take the difference between two time_t's the result might be an integer of which only the application knows how big it has to be and if signed or unsigned. So the following does calculations but no conversions:time_t today = time(0);
time_t yesterday = today - 86400;
unsigned int distance_of_all_my_trouble = today - yesterday;
# 7 Re: about time and time_t
Thanks Zaccheus,
Why not convert long to time_t first ?
Because the function which accepts time_t as an input parameter is a numeric calculation function, and I think make all data types in numeric type (like long) is more clean and clear.
regards,
George
# 8 Re: about time and time_t
Thanks treuss,
I wouldn't say so. You can add any integer to a time_t and the result will again be time_t. If you take the difference between two time_t's the result might be an integer of which only the application knows how big it has to be and if signed or unsigned. So the following does calculations but no conversions:time_t today = time(0);
time_t yesterday = today - 86400;
unsigned int distance_of_all_my_trouble = today - yesterday;
Sorry that in my post, conversion means compiler warnings, for example, your code has the following warnings in Visual Studio 2005,
warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data
regards,
George
# 9 Re: about time and time_t
Right, because an 'unsigned int' is not strictly a 'time_t'. If you add a cast, then the warning goes away because you are telling the compiler that you know that these are different types.
Viggy
# 10 Re: about time and time_t
Thanks Viggy,
Adding a cast you mean,
unsigned_int_type_variable = (unsigned int) (time_t_type_variable);
~~~~~~~~~
Right (see the *~~~~* part)? ;)
Right, because an 'unsigned int' is not strictly a 'time_t'. If you add a cast, then the warning goes away because you are telling the compiler that you know that these are different types.
Viggy
regards,
George
# 11 Re: about time and time_t
Adding a cast you mean,
unsigned_int_type_variable = (unsigned int) (time_t_type_variable);
George
You should use the C++ static_cast:
unsigned int uiTime = static_cast<unsigned int>( time( NULL ) );
# 12 Re: about time and time_t
Thanks GNiewerth,
You should use the C++ static_cast:
unsigned int uiTime = static_cast<unsigned int>( time( NULL ) );
What is the benefit of your static cast method campared with my method below?
unsigned_int_type_variable = (unsigned int) (time_t_type_variable);
regards,
George
# 13 Re: about time and time_t
#1 you can easily spot there is a cast because of the explicit static_cast keyword.
#2 its safer than the C-style cast. C-style cast just convert anything into anything else, where C++ style casts have some requirements that prevent you from performing invalid casts.
# 14 Re: about time and time_t
Hi GNiewerth,
#1 you can easily spot there is a cast because of the explicit static_cast keyword.
#2 its safer than the C-style cast. C-style cast just convert anything into anything else, where C++ style casts have some requirements that prevent you from performing invalid casts.
I am programming C. ;)
But I am very interested to learn #2. Could you show me a sample about how static_cast prevents invalid type conversion please? Or recommend me some URLs?
regards,
George
# 15 Re: about time and time_t
CodeProject article on static_cast ( http://www.codeproject.com/cpp/static_cast.asp)
Type casting article on cplusplus.com ( http://www.cplusplus.com/doc/tutorial/typecasting.html)
# 16 Re: about time and time_t
Thanks GNiewerth,
CodeProject article on static_cast ( http://www.codeproject.com/cpp/static_cast.asp)
Type casting article on cplusplus.com ( http://www.cplusplus.com/doc/tutorial/typecasting.html)
Very useful information.
regards,
George