1

I am writing a WIN32 C-program for a Motorola MC-55A PDA using Windows Mobile 5.0 SDK. I'm trying to get the current date and time to get a DDMMYYYYHHMMSS unique string that I can use. I've checked the tutorials and tried to use time_t and SYSTEMTIME but its coming back with the error : " illegal use of this type as an expression" (see code below). I've included the required header files including "time.h" but the error persists with the use of time_t and SYSTEMTIME . Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <windowsx.h>

.
.
.
case WM_INITDIALOG:

//Below fails with error : 'time_t' : illegal use of this type as an expression  
//C:\Program Files (x86)\Windows Mobile 5.0 SDK R2\PocketPC\include\ARMV4I\stdlib.h : 
//see declaration of 'time_t
time_t current_time = time(NULL);
printf("Hours since January 1, 1970 = %ld\n", current_time/3600);

//This also fails
time_t now;
time(&now);


//SYSTEMTIME below also fails with error : 'SYSTEMTIME' : illegal use of this type as an expression  
//C:\Program Files (x86)\Windows Mobile 5.0 SDK R2\PocketPC\include\ARMV4I\winbase.h : 
//see declaration of 'SYSTEMTIME'
GetSystemTime(&st);
GetLocalTime(&lt);                  
printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);

How do I get the current date and time?

Ezani
  • 535
  • 3
  • 18
  • Does [this](https://stackoverflow.com/q/9903582/1889329) answer your question? – IInspectable Mar 04 '20 at 10:14
  • 1
    Do you try to explicitly add a lib reference when using `GetSystemTime`/`GetLocalTime`? `#pragma comment(lib, "Kernel32.lib")` – Drake Wu Mar 05 '20 at 01:21
  • Hey thanks again, Drake. This works! I've upvoted and will use GetSystemTime/GetLocalTime instead as a workaround (got a tight deadline). (But do you know why I can't use time_t?) – Ezani Mar 05 '20 at 07:10
  • 1
    Maybe you could try to add `/MD`or`/MDd` in compiler options to add the reference of the C Run-Time Libraries. I have checked the dependency of my test.exe which call the `time()`, the `time` was located at ucrtd.dll, you could refer to this doc for the details: https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=vs-2019 – Drake Wu Mar 05 '20 at 07:53
  • Thanks once again Drake Wu! – Ezani Mar 05 '20 at 10:23

2 Answers2

1

A workaround is using GetSystemTime or/and GetLocalTime functions by first adding a pragma for Kernel32.lib where the 2 functions are defined at the very top of your code.

//This works

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <windowsx.h>
#pragma comment(lib, "Kernel32.lib")

.
.
.
case WM_INITDIALOG:

     GetSystemTime(&st);
     GetLocalTime(&lt);                 
     printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
     printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);
Ezani
  • 535
  • 3
  • 18
0

Although you haven't posted your complete code, it appears that you are declaring the current_time, st and it variables in an incorrect/disallowed place (after case WM_INITDIALOG:). You should either declare these variables before the switch statement, or add enclosing { ... } braces inside that case.

For example, code like this is ill-formed:

switch (a) {
    case 1:
        int b = a/2; // illegal here!
        //...
        break;
    default:
        break;
}

but this will work:

int b; // Note: This declaration MAY need to be at the beginning of the enclosing function!
switch (a) {
    case 1:
        b = a/2; // works
        //...
        break;
    default:
        break;
}

and so will this:

switch (a) {
    case 1: { // These "{ ... }" create a new scope
        int b = a/2; // works
        //...
        break;
    }
    default:
        break;
}

The reason for this is that, in your code, the declaration/definition of the aforementioned variables is skipped by cases other than WM_INITDIALOG. See this Stack Overflow Q/A for further discussion: Why can't variables be declared in a switch statement?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • The code in question uses C, presumably C89. With C89 there are very few places where you can declare variables. – IInspectable Mar 04 '20 at 10:23
  • @IInspectable Good point! I've added a comment to the first 'fix' I suggested, but the second one (creating a local scope) should work, even in C89. – Adrian Mole Mar 04 '20 at 10:29
  • @Ezani Do you get the same error messages, or different ones? And did you try both suggested fixes? – Adrian Mole Mar 04 '20 at 10:29
  • Tried both fixes, yes, the code syntax errors are gone but I'm getting unresolved externals linking errors with time_t, _wstrdate, _wstrtime and SYSTEMTIME but I have already included time.h. I've edited/updated my question slightly. – Ezani Mar 04 '20 at 10:31
  • 1
    @eza: You haven't changed your question *"slightly"*. You have asked a fundamentally different question. The compiler and linker are different tools. A compiler error is solved a different way from a linker error. Please change your question back to convey its original intent, the one that this answer addresses. – IInspectable Mar 04 '20 at 10:42
  • Yes, I've removed all the variable declarations inside the select statement and there are no more syntax errors but I'm getting unresolved externals due to linking errors (see my edited question). It seems the header files doesn't recognize time_t and SYSTEMTIME (and even the tm struct). Nothing to do with coding errors anymore after I made the edits you suggested. – Ezani Mar 04 '20 at 10:44
  • 1
    @eza: You are invited to make editorial changes to your question. However, changing a question in a way that invalidates a published answer is not something you should do. Please revert your question to what it initially asked about. If you have an issue with linker errors, [ask a question](https://stackoverflow.com/questions/ask) after you have done research and made sure, that [this](https://stackoverflow.com/q/12573816/1889329) doesn't apply to your case. – IInspectable Mar 04 '20 at 11:16
  • AdrianMole, @IInspectable, I tried to post my new error in this comments section so as to leave my original question intact, but the comments section do not accept the ENTER key so I could not do paragraphing. So OKAY, I will revert back to my ORIGINAL question, IInspectable, and put the new error in a new post. No offense meant. – Ezani Mar 05 '20 at 03:27
  • Ok, reverted to original. – Ezani Mar 05 '20 at 03:34
  • @IInspectable, thanks for the linker errors link (for unresolved externals)! – Ezani Mar 05 '20 at 03:42