Author Topic: Possible issue with localtime changing in Windows  (Read 5108 times)

Offline Lefteris

  • Multiple posting newcomer
  • *
  • Posts: 14
Possible issue with localtime changing in Windows
« on: May 18, 2012, 08:46:41 am »
Hello!

I encountered a possible interesting issue while programming with C::B 10.05 in Windows 7.
I was testing a program that somewhere deep inside apparently was changing the local time.

I have recreated the issue with this small example program:

Code
#include <windows.h>
int main()
{
    //get the time
    SYSTEMTIME st;
    GetLocalTime(&st);
    //put it 1 hour in the past ( I know this is not the right way to do it but let's make an
    //assumption that this won't change the day for the purpose of the test)
    st.wHour -=1;
    //btw in my system Avira thinks this contains a trojan called cuckold.A . Please for the purposes of this test disable avira.
    SetLocalTime(&st);
    return 0;
}

The program I was testing was not raising any flags, but this little 4 lines example program actually raised a flag in Avira Anti-Virus and got stopped. If this happens to you disable it just for the test here. You can see the code, nothing malicious happens except for going 1 hour in the past.

After compiling and running this from inside codeblocks it works fine and does indeed change the time to 1 hour in the past. But as a result you can see that the execution of the program took negative minutes and seconds.

The interesting thing happens if you attempt to make any changes on this program and run again. You may think that the newly changed program will run but it will not. If you press compile and run it will not compile but it will just run this previous program that changed the hour, and go yet 1 more hour to the past. Took me sometime to realize what was happening since I was debugging at the time and the program was stepping into totally different functions than the code was saying.

I have not had the pleasure to look into C::B code but I suppose that it must getting really confused by things being in the past, so any changes you make to a source file are not detected as changes and nothing new is compiled, so the old program runs again.

The reason I made this post is to ask if anyone else has come upon this interesting issue. I can't think of any way to fix this so I don't have any suggestions but I just wanted to know if people here are aware of the potential of this happening.

I suppose the only solution would be to just make sure running a program from inside C::B does not put the date in the past.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Possible issue with localtime changing in Windows
« Reply #1 on: May 18, 2012, 01:27:30 pm »
The reason is plain simple:

We use the file's time stamp to decide, whether a compilation unit need to be re-compiled or not. If you change the system time, you screw C::B's build system.

As this would screw any build system in the world, if you really need to set you system time to the past then you need to do a full re-build all the time to ensure everything gets compiled. In that case, C::B will not do the file time check.

You see the drawback: You always have to build everything... that's why we do file time checking... ;-)

This is not a bug btw.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Possible issue with localtime changing in Windows
« Reply #2 on: May 18, 2012, 01:50:24 pm »
[...] if you really need to set you system time to the past then you need to do a full re-build all the time to ensure everything gets compiled. In that case [...]

Or wait an hour before the next build, so you might get more time for real life stuff.  ;D

Offline Lefteris

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Possible issue with localtime changing in Windows
« Reply #3 on: May 18, 2012, 06:32:57 pm »
As I thought. Yeah it should not be considered as a bug of course. Just noticed it and thought I should write something. The reasoning makes sense :P

Thanks for replying.

P.S. I realize now it was a stupid question, but I attribute it to my total lack of knowledge of how a build system works internally  ;D
« Last Edit: May 18, 2012, 06:44:24 pm by Lefteris »