Question about SYSTEMTIME

R

ralph

Hi,
I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
it?
I start like that:

SYSTEMTIME time_old, time_now;

GetLocalTime(&time_old);
GetLocalTime(&time_now);

And now I must have in time_old the SYSTEMTIME - 15 minutes, is there
any implemented function in C to do in (it must work in windows not in
unix).

I'll be very gratefull for any sugestions.

Thank you very much for help

Ralph
 
K

Keith Thompson

ralph said:
I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
it?
I start like that:

SYSTEMTIME time_old, time_now;

GetLocalTime(&time_old);
GetLocalTime(&time_now);

And now I must have in time_old the SYSTEMTIME - 15 minutes, is there
any implemented function in C to do in (it must work in windows not in
unix).

There is no SYSTEMTIME type or GetLocalTime() function in standard C.
Since you say the problem is Windows-specific, you need to ask in a
Windows-specific newsgroup.
 
M

MQ

ralph said:
Hi,
I want to get the (SYSTEMTIME -15) minuts in windows in C. How can I do
it?
I start like that:

SYSTEMTIME time_old, time_now;

GetLocalTime(&time_old);
GetLocalTime(&time_now);

Did you look at the format of the SYSTEMTIME structure? It has fields
like wHour, wMinute, wSecond, etc. I imagine the code would be
something like

if(time_old.wMinute < 15)
{
time_old.wHour--;
time_old.wMinute = 45 + time_old.wMinute;
/* not complete, more must be added */
}
else
{
time_old.wMinute -= 15;
}

I'll leave it as an exercise for you to add the rest of the code for
handling day, week, month, year, etc boundaries, which is actually not
that trivial... Consult MSDN for the details of the SYSTEMTIME
structure. And before you get bombarded by cranky clc regulars about
the inappropriateness of the post in this purely C newsgroup, I would
suggest posting platform-specific (eg Windows) questions on an MS
forum. I don't have a problem with it, but you will find some do... :)

cheers
MQ
 
I

Ian Malone

MQ said:
Did you look at the format of the SYSTEMTIME structure? It has fields
like wHour, wMinute, wSecond, etc. I imagine the code would be
something like

if(time_old.wMinute < 15)
{
time_old.wHour--;
time_old.wMinute = 45 + time_old.wMinute;
/* not complete, more must be added */
}
else
{
time_old.wMinute -= 15;
}

I'll leave it as an exercise for you to add the rest of the code for
handling day, week, month, year, etc boundaries, which is actually not
that trivial... Consult MSDN for the details of the SYSTEMTIME
structure. And before you get bombarded by cranky clc regulars about
the inappropriateness of the post in this purely C newsgroup, I would
suggest posting platform-specific (eg Windows) questions on an MS
forum. I don't have a problem with it, but you will find some do... :)

As an alternative either:
1. Use whatever routine this platform has for converting SYSTEMTIME
to a single number (in seconds, milliseconds or whatever) and
subtract the right amount, then convert back.
2. Use the C function time to get the time in seconds, subtract
15*60, use localtime to convert the result to struct_tm.
 
M

MQ

Ian said:
As an alternative either:
1. Use whatever routine this platform has for converting SYSTEMTIME
to a single number (in seconds, milliseconds or whatever) and
subtract the right amount, then convert back.
2. Use the C function time to get the time in seconds, subtract
15*60, use localtime to convert the result to struct_tm.

Yes, this is probably easier. Let the OS/libraries do the work

MQ
 
K

Keith Thompson

Ian Malone said:
As an alternative either:
1. Use whatever routine this platform has for converting SYSTEMTIME
to a single number (in seconds, milliseconds or whatever) and
subtract the right amount, then convert back.
2. Use the C function time to get the time in seconds, subtract
15*60, use localtime to convert the result to struct_tm.

The latter assumes that time_t represents the time in seconds. It
commonly does, but there is no such guarantee (or implication) in the
C standard.

If you need portability, you can convert a time_t value to a struct tm
using localtime(), manipulate the members of the struct tm, then
convert it back using mktime().

If you don't care about absolute portability (e.g., if your platform
happens to make guarantees about the representation of time_t, and
your code is already platform-specific anyway), you can probably get
away with performing arithmetic directly on a time_t. Or you can
consult your system's documentation for solutions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top