time-t

Comparing time_t values using comparison operators

柔情痞子 提交于 2019-11-30 23:21:27
I have 2 time_t values and I want to find out which one is greater. time_t is internally __int64 on my platform. Can I use < , > and == operators to compare the values? I don't want to know the difference between the two time values. The code will just run on Windows, so I don't care about portability. Is it correct to compare the values in this way? According to section 7.27.1(3) of the C standard (which the C++ standard refers to in this case) time_t is a real type , which is defined in 6.2.5(17) as either integer or floating type. As long as you don't explicitly assume integers (i.e. by

Addition some interval to tm structs

北城以北 提交于 2019-11-30 18:24:08
I have one struct tm . And I need to add some fixed interval (given in xx years, xx months, xx days) to the tm struct. Is there any standard function to do this? The compiler I use is MSVC 2005 on Windows XP. There are two functions which convert time formats: mktime() which converts struct tm (representing local time) to time_t . localtime() which converts time_t to local time in struct tm . Interesing is the first one, which accepts out-of-range struct member values and as a side-product of conversion set them (and all others) appropriately. This may be used to correct field data values

How to convert a UTC date & time to a time_t in C++?

时光怂恿深爱的人放手 提交于 2019-11-30 04:40:07
问题 I want to convert a UTC date & time given in numbers for year, month, day, etc. to a time_t. Some systems offer functions like mkgmtime or timegm for this purpose but that is not standard and does not exist on my Solaris system. The only solution I have found so far involves setting the local time zone to UTC with setenv and then call mktime . However this approach is not thread-safe, slow, not portable and even generates a memory leak on my system. I have also seen approaches that tried to

Addition some interval to tm structs

旧时模样 提交于 2019-11-30 02:59:00
问题 I have one struct tm . And I need to add some fixed interval (given in xx years, xx months, xx days) to the tm struct. Is there any standard function to do this? The compiler I use is MSVC 2005 on Windows XP. 回答1: There are two functions which convert time formats: mktime() which converts struct tm (representing local time) to time_t . localtime() which converts time_t to local time in struct tm . Interesing is the first one, which accepts out-of-range struct member values and as a side

Format specifiers for implementation-defined types like time_t

扶醉桌前 提交于 2019-11-30 01:03:34
问题 I want to make my code more platform-/implementation-independent. I don't know what a time_t will be implemented as on the platform when the code is being compiled. How do I know the type of t to determine what format specifier to use? ... time_t t = time(NULL); printf("%s", t); ... 回答1: Generally, the way to display the value of a time_t is to break down its components to a struct tm using gmtime or localtime and display those or convert them as desired with strftime , or ctime to go

Standard conformant way of converting std::time_t to System::DateTime?

血红的双手。 提交于 2019-11-28 10:30:36
I have already found several answers related to converting a std::time_t value to System::DateTime and back. However, almost all answers seem to neglect that the type of std::time_t is actually undefined in the standard. Most solutions just cast std::time_t to whatever needed or apply arithmetic operations to a std::time_t object which is possible since it's an arithmetic type, but there is no specification about the result of such an operation. I know that most compilers define time_t as an int of some size but the fact alone that it has changed from int32 to int64 in many implementations

what is the difference between difftime and '-'?

会有一股神秘感。 提交于 2019-11-27 14:38:20
I have 2 variables of type time_t - varEnd and varStart. Now in order to see the difference between them Either I can do varEnd - varStart; or difftime(varEnd, varStart); and both returns number of seconds. Please let me know, if they have any difference? or which is the recommended one? The language specifies that time_t is an arithmetic type capable of representing times. It doesn't require it to represent times in any particular way. If time_t represents time as the number of seconds since some moment, the - operator will correctly compute the difference in seconds between two time_t values

What primitive data type is time_t? [duplicate]

心不动则不痛 提交于 2019-11-27 01:56:47
This question already has an answer here: What is time_t ultimately a typedef to? 10 answers I do not know the data type of time_t . Is it a float double or something else? Because if I want to display it I need the tag that corresponds with it for printf . I can handle the rest from there for displaying time_t but I need to know the data type that corresponds with it. Unfortunately, it's not completely portable. It's usually integral, but it can be any "integer or real-floating type". It's platform-specific. But you can cast it to a known type. printf("%lld\n", (long long) time(NULL));

Get the current time in C

时光毁灭记忆、已成空白 提交于 2019-11-26 21:46:34
I want to get the current time of my system. For that I'm using the following code in C: time_t now; struct tm *mytime = localtime(&now); if ( strftime(buffer, sizeof buffer, "%X", mytime) ) { printf("time1 = \"%s\"\n", buffer); } The problem is that this code is giving some random time. Also, the random time is different everytime. I want the current time of my system. Copy-pasted from here : /* localtime example */ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current local time and

what is the difference between difftime and '-'?

我是研究僧i 提交于 2019-11-26 16:49:47
问题 I have 2 variables of type time_t - varEnd and varStart. Now in order to see the difference between them Either I can do varEnd - varStart; or difftime(varEnd, varStart); and both returns number of seconds. Please let me know, if they have any difference? or which is the recommended one? 回答1: The language specifies that time_t is an arithmetic type capable of representing times. It doesn't require it to represent times in any particular way. If time_t represents time as the number of seconds