Working with date_t and time_t
-
I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific timezone, I am doing calculations and have number of hours to add or substract. So I don't know: how can I initialize tm struct with the date value I have? how to add or substract hours in tc struct variable to get required date? my intention is Date received "24-9-2016 13:30" and 5 Hours to add so final Date: "24-9-2016 18:30"
//Temporarily init time to local
time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
.
.
.
//Not sure how can I subtract or add hours in this struc to get desired date valueplease provide inputs.
-
I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific timezone, I am doing calculations and have number of hours to add or substract. So I don't know: how can I initialize tm struct with the date value I have? how to add or substract hours in tc struct variable to get required date? my intention is Date received "24-9-2016 13:30" and 5 Hours to add so final Date: "24-9-2016 18:30"
//Temporarily init time to local
time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
.
.
.
//Not sure how can I subtract or add hours in this struc to get desired date valueplease provide inputs.
I am not sure what this has to do with Managed C++. However, the time, _time32, _time64[^] function describes the content of a
time_t
value, which you can easily initialise with a user defined time. I have not come across adate_t
type, where is it defined? -
I am not sure what this has to do with Managed C++. However, the time, _time32, _time64[^] function describes the content of a
time_t
value, which you can easily initialise with a user defined time. I have not come across adate_t
type, where is it defined? -
I am receiving a date structure in "24-9-2016 13:30" format. Now I want to convert time value to specific timezone, I am doing calculations and have number of hours to add or substract. So I don't know: how can I initialize tm struct with the date value I have? how to add or substract hours in tc struct variable to get required date? my intention is Date received "24-9-2016 13:30" and 5 Hours to add so final Date: "24-9-2016 18:30"
//Temporarily init time to local
time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
.
.
.
//Not sure how can I subtract or add hours in this struc to get desired date valueplease provide inputs.
Since all those fields are integers you can add, subtract or replace any of them. You should check that the values are valid in terms of the calendar, although the
mktime
,_mktime32
,_mktime64
[^] functions will attempt to normalise atm
structure into a valid time. There are many other useful functions, and you should study the documentation to see which ones will help you. -
Since all those fields are integers you can add, subtract or replace any of them. You should check that the values are valid in terms of the calendar, although the
mktime
,_mktime32
,_mktime64
[^] functions will attempt to normalise atm
structure into a valid time. There are many other useful functions, and you should study the documentation to see which ones will help you.Yes, all these are integers, so I can perform addition or subtraction, but it is not handled well in below cases- if hours are added Like 4.5 then 13 hours should become 18:30 Or more than 24 then date should modify In above cases I will need to add additional code depending on hours value, which is bad, isn't there any system method which will modify entire structure depending on the value assigned
-
Yes, all these are integers, so I can perform addition or subtraction, but it is not handled well in below cases- if hours are added Like 4.5 then 13 hours should become 18:30 Or more than 24 then date should modify In above cases I will need to add additional code depending on hours value, which is bad, isn't there any system method which will modify entire structure depending on the value assigned
-
If you want to add partial hours, days etc. Then use
mktime
(as I advised in my previous comment) to convert yourtm
structure into atime_t
value. you can then just add or subtract the number of seconds in your time increment.ok, so here is what I did now-
//Temporarily init time to local
time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
//completed it...int hoursToAdd = 4.5;
initStruct->tm_hour = initStruct->tm_hour + hoursToAdd;
time_t myTime = mktime(initStruct);//this return -1
printf( "%s", ctime( &myTime));//this prints null..
any idea whats going wrong here?
-
ok, so here is what I did now-
//Temporarily init time to local
time_t tempTime
time(&tempTime);
struct tm *initStruct = localtime(&tempTime);//initialize it with local time
//now modify it to user defined date
initStruct ->tm_year = 2016;
initStruct->tm_mon = 9;
initStruct->tm_hour = 13;
//completed it...int hoursToAdd = 4.5;
initStruct->tm_hour = initStruct->tm_hour + hoursToAdd;
time_t myTime = mktime(initStruct);//this return -1
printf( "%s", ctime( &myTime));//this prints null..
any idea whats going wrong here?
-
int hoursToAdd = 4.5;
That is not a valid statement so it will not even compile. You cannot assign a floating point number to an integer variable.
-
Yes, my bad. well in that case my original question reopens how can I assign partial hour to tm struct to enable it to give valid date value.
You really (and I mean really) need to go and study the documentation which explains what each field represents. If you have some number of hours and some number of minutes then you need to add them separately to each field. You should also step through your code with the debugger so you can see at each step what are the values of the various fields.
-
You really (and I mean really) need to go and study the documentation which explains what each field represents. If you have some number of hours and some number of minutes then you need to add them separately to each field. You should also step through your code with the debugger so you can see at each step what are the values of the various fields.