[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7416
  • Last Modified:

GetSystemTime returns 01/01/1601

This is the code. Runs on hundreds of thousands of PCs worldwide, but one pc, returns 01/01/1601 instead of a proper date.

char * currentdate()
{
      SYSTEMTIME stime;
      char * sresult = createstring(19, BUFSIZE);                                    // freed in main
      GetSystemTime(&stime);
      sprintf(sresult, "%02i-%02i-%0004i %02i:%02i:%02i\0", stime.wDay, stime.wMonth, stime.wYear, stime.wHour, stime.wMinute, stime.wSecond);
      return sresult;
}


Do you think its the PC or the code thats wrong ?????  Its at a customer site so I'd like to find out as much as possible before asking them to do tests

thanks
0
plq
Asked:
plq
1 Solution
 
AxterCommented:
Did the customer check what the time settings on the PC to validate it's correct?

The customer should check both the Windows reported time settings, and the CMOS time settings.
0
 
plqAuthor Commented:
The pc was turned off when we tried to do a net time on it,

this customer has 25000 pcs so I didn't really want to mess them around until I'm sure the code is ok
0
 
AxterCommented:
>>-%0004i

That part doesn't look right to me.
Should be something like the following:
%4.4i

But I don't see that causing the problem.

I don't see anything in your code that would cause you to get the wrong date.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AxterCommented:
>>char * sresult = createstring(19, BUFSIZE);                              

Actually, the above looks like a problem.  I counted 21 bytes that you would need for your buffer, and above code is only allocating 19.
0
 
grg99Commented:
You're allocating a 19 byte buffer and sprintf'ing 21 bytes into it.    That's a bad thing.

That's why one should always use snprintf with the actual buffer length as the limit parameter.
.

0
 
AxterCommented:
I counted 20 formated characters in the string, which included \0
sprintf is going to throw another terminating character after the \0 character, which would make it a total of 21 characters.
0
 
AxterCommented:
FYI:
>>That's why one should always use snprintf with the actual buffer length as the limit parameter.

snpintf is not part of the C++ standard, and therefore some C++ compilers don't support it.

When ever I have to use an sprintf function, I always try to allocated twice what I need.
It's better to pad the buffer, then to get a bug just for being short by one or two bytes.
0
 
plqAuthor Commented:
Nope, 19 is just a context used in debug mode so we can match each allocation against each free. In release mode its ignored. BUFSIZE is the size of the buffer. I think thats either 1024 or 2048
0
 
AxterCommented:
>>Nope, 19 is just a context used in debug mode so we can match each allocation against each free. In release mode its ignored. BUFSIZE is the size of the buffer. I think thats either
>>1024 or 2048

Then the problem is with the system's time settings.

0
 
AxterCommented:
FYI:
SYSTEMTIME is a 64 bit structure representing number of 100-nanosecond intervals since January 1, 1601

The value you're getting is zero.
0
 
plqAuthor Commented:
Hmmm actually, I think it had a time of 06:06:06, so maybe the battery is broken (surprised, its an xp box), and 06.. is the hours its been turned on.
0
 
jkrCommented:
This also can happen if the TimeZoneAdjustment is incorrect. To test that, you could 'SetSystemTime()'. If the battery is broken, the system clock also should display incorrect values. Is that the case?
0
 
plqAuthor Commented:
OK, I think having you guys give the code the thumbs up is enough grounds to ask the customer to go to the PC and analyse it in more depth.

I'll update when they've had a chance to look.

thanks
Paul
0
 
Fippy_DarkpawCommented:
The other possibility is that your computer was sent through a  time machine from someone in the past.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now