News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Comparing results from GetDateFormat kill date

Started by jt, August 01, 2013, 08:43:50 AM

Previous topic - Next topic

jt

Hello all,

Back in 2011 xwwwx posted a topic/question concerning comparing results from GetDateFormat. I'm very new to MASM and from what I can tell the whole solution wasn't fully explained "at least in a way that I understand".
http://www.masmforum.com/board/index.php?PHPSESSID=786dd40408172108b65a5a36b09c88c0&topic=17743;prev_next=next

I inherited a MASM32 application and need to limit its capabilities to so it will only execute until a future date. More or less a kill date.  I want to compare the current date to a string I store in the application. I'm not concerned with anyone modifying it to change the date.

Example code from the forms that I made a few small changes to is below. The author is a guest and I'm not sure how to contact him or her. I need guidance or a pointer as to where I should do the comparison as this is very new to me



.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

SYSTEMTIME STRUCT
  wYear             WORD      ?
  wMonth            WORD      ?
  wDayOfWeek        WORD      ?
  wDay              WORD      ?
  wHour             WORD      ?
  wMinute           WORD      ?
  wSecond           WORD      ?
  wMilliseconds     WORD      ?
SYSTEMTIME ENDS

.data
szDFormat db "MMMddyyyy", 0Dh, 0Ah, 0
kdate db "Jul312013", 00h

MsgBoxCaption  db "Alert",0
MsgBoxText       db "Running",0


.data?
stCurTime SYSTEMTIME <>
szDate db 12 dup (?)

.code

    _ep:
   
    invoke GetLocalTime, addr stCurTime
    invoke GetDateFormat, LOCALE_USER_DEFAULT, NULL, addr stCurTime, addr szDFormat, addr szDate, 12   
    ;invoke MessageBox, NULL, addr szDate, NULL, MB_OK
   
invoke ExitProcess,0

end _ep

invoke lstrcmp, addr szDate, addr kdate
.if eax <= 0
invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK
invoke ExitProcess,0
.endif


jj2007

Have you thought of simply using wYear*365+wMonth*30+wDay?

include \masm32\include\masm32rt.inc

.data?
stime SYSTEMTIME <>

.code
killtime dd 2013, 9, 3 ; 3 September is Kill Date

start: invoke GetSystemTime, addr stime
movzx eax, stime.wYear
imul ebx, eax, 365
movzx eax, stime.wMonth
imul eax, eax, 30
add ebx, eax
movzx eax, stime.wDay
add ebx, eax
print str$(ebx), 9, "current", 13, 10
imul esi, killtime, 365
imul eax, killtime[4], 30
add esi, eax
add esi, killtime[8]
print str$(esi), 9, "limit", 13, 10
sub esi, ebx
print str$(esi), 9, "days left", 13, 10
exit

end start

dedndave

also, you can force a format that does not use the locale format
for example....

INVOKE  GetTimeFormat,LOCALE_USER_DEFAULT,TIME_FORCE24HOURFORMAT or TIME_NOTIMEMARKER,
        NULL,NULL,addr Buffer,sizeof Buffer


i think that's right   :P

jj2007

Reminded me that my Time$ and Date$ format was not flexible... but that can be changed:

include \masm32\MasmBasic\MasmBasic.inc
        Init
        PrintLine Date$, ", ", Time$, Tb$, "(default)"

        MbTimeHms equ "HH:mm"        ; 14:15
        MbDateDmy equ "d MMMM yy"        ; 3 August 13
        PrintLine Date$, ", ", Time$

        MbTimeHms equ "hh:mm tt"        ; 14:15 PM
        MbDateDmy equ "MMMM 3, yyyy"        ; August 3, 2013
        PrintLine Date$, ", ", Time$

        MbTimeHms equ "H 'hours, 'mm' minutes and 's' seconds'"        ; 6 hours, 25 minutes and 58 seconds
        MbDateDmy equ "d' of 'MMMM', 'yyyy"        ; 3 of August 13

        Inkey Date$, ", ", Time$
        Exit
end start

Output:
01.08.2013, 06:32:26    (default)
1 August 13, 06:32
August 1, 2013, 06:32 AM
1 of August, 2013, 6 hours, 32 minutes and 26 seconds

GetDateFormat & GetTimeFormat are fairly flexible, it seems :biggrin:
But the red one above - "hh:mm tt" aka 11:22 PM - fails miserably: no AM or PM :(

Interestingly enough, MSDN's GetTimeFormat doesn't know these time formats any more :eusa_boohoo:
QuoteFor information about the format picture string, see the Remarks section.

Which, in turn, points to the Remarks section of GetTimeFormatEx. When applying their example for the U.S. format (AM or PM), GetTimeFormat succeeds but the AM is simply not there. Weird.

I grabbed mine from the old Win32.hlp, and they work on XP SP3.
(requires MasmBasic plus the attached modified include file)

jt

jj2007,

Thank you for the example code and I had not though of using "wYear*365+wMonth*30+wDay?". I figured it would be easy to compare the system time with a future date and run until that date was reached.

I was able to compile the example but when executing through the cmd line I'm not receiving any output. Not sure why? I'm not sure if it matters but my platform is win7 x64. For the MasmBasic.inc example my build environment doesn't meet the dependencies and I'm not sure if it will be compatible. The original app was built on MASM32.


Thank you for your help!


jt

dedndave,

Thank you for the example I'll attempt to incorporate it once I figure out how to do this. =)   

jj2007

Quote from: jt on August 01, 2013, 04:27:02 PMI was able to compile the example but when executing through the cmd line I'm not receiving any output. Not sure why?

You need to assemble it as a console app.

\Masm32\RichMasm\RichMasm.exe has autodetection (i.e., when you hit F6, the editor sees Print and Inkey and concludes "must be a console app"), but if you use \Masm32\qeditor.exe, you must use the menu Project/Console build all, and "Run program" afterwards.

P.S.: Building a console app as Windows happens to the best of us, so you are in good company. Launch Task Manager and check if your app is hanging around there consuming 50% CPU. If you can identify it, kill it. Then try again with console build all.

dedndave

you don't really have to mess with adding and multiplying

you can use GetSystemTime or GetLocalTime to get the current time
for what you are doing, it doesn't matter much which one you use
they both fill a SYSTEMTIME structure

you can then use the SystemTimeToFileTime function
it fills a FILETIME structure, which is essentially a qword integer that can be compared fairly easily

http://msdn.microsoft.com/en-us/library/windows/apps/ms724390%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/ms724338%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/ms724948%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/ms724950%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/ms724284%28v=vs.85%29.aspx

jt

jj2007, dedndave,

I think I figured it out. =)

This code appears to work and I've got it working in my application. Thanks for all the help!


include \masm32\include\masm32rt.inc

.data
MsgBoxCaption  db "Alert",0
MsgBoxText     db "Running",0
MsgBoxText1    db "App Exp",0

.data?
stime SYSTEMTIME <>

.code
killtime dd 2013, 8, 1 ; 3 September is Kill Date

main:
invoke GetSystemTime, addr stime
movzx eax, stime.wYear
imul ebx, eax, 365
movzx eax, stime.wMonth
imul eax, eax, 30
add ebx, eax
movzx eax, stime.wDay
add ebx, eax
print str$(ebx), 9, "current", 13, 10
fn MessageBox,0,str$(ebx),"current",MB_OK

imul esi, killtime, 365
imul eax, killtime[4], 30
add esi, eax
add esi, killtime[8]
print str$(esi), 9, "limit", 13, 10
fn MessageBox,0,str$(esi),"Kill Limit",MB_OK

sub esi, ebx
print str$(esi), 9, "days left", 13, 10
fn MessageBox,0,str$(esi),"Days Left",MB_OK


.if esi == 0
invoke MessageBox, NULL, addr MsgBoxText1, addr MsgBoxCaption, MB_OK
invoke ExitProcess,0
.endif

invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK

exit

end main




dedndave