News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

More GetLocalTime

Started by theiod, May 01, 2006, 04:19:13 AM

Previous topic - Next topic

theiod

I am an assembly newbie, and am having troubles getting my time formatted. As it stands now it keeps crashing somewhere inside my procedure to convert the time. Ollydebug hasn't helped. I'm working on getting the visual studio debugger to work now.

If any one can help me get the date and time formatted in this way I'd really appreciate the help.
I'm trying to use GetLocalTime to output the date and time in this format:

Sun, Apr 30, 2006
09:16:14 PM

I appreciate your help and time.

Thanks in advance.

Extreme Newbie

asmGhost


Ossa

#2
Here's a simple console example. It uses the date formats that you specified... note that I added CRLFs to the format string - if this is for a windows app, you will more than likely want to remove that. This will probably be what you want (instead of what I have):

szDFormat               db              "ddd',' MMM dd',' yyyy", 0
szTFormat               db              "hh':'mm':'ss tt", 0


If you have code already written, it might help to post it (just the relevent bits), as people can then give advice more suited to your problem.

Hope it helps,
Ossa

(As asmGhost pointed out, the reference will give details on how to modify this to do exactly what you like.)

; Include, etc

.386
.model FLAT, STDCALL
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

; String lengths

MAX_DATE_LEN            EQU             20
MAX_TIME_LEN            EQU             15

; Format strings

.data

szDFormat               db              "ddd',' MMM dd',' yyyy'", 0Dh, 0Ah, "'", 0
szTFormat               db              "hh':'mm':'ss tt'", 0Dh, 0Ah, "'", 0

; Run time data

.data?

stCurTime               SYSTEMTIME      <>
szDate                  db              MAX_DATE_LEN dup (?)
szTime                  db              MAX_TIME_LEN dup (?)

; Start code

.code
start:

; Get time

invoke GetSystemTime, addr stCurTime

; Convert

invoke GetDateFormat, LOCALE_USER_DEFAULT, NULL, addr stCurTime, addr szDFormat, addr szDate, MAX_DATE_LEN
invoke GetTimeFormat, LOCALE_USER_DEFAULT, NULL, addr stCurTime, addr szTFormat, addr szTime, MAX_TIME_LEN

; Output

invoke StdOut, addr szDate
invoke StdOut, addr szTime

; Exit

invoke ExitProcess, 0

; End code

end start


[edit]

Oops... just re-read the OP. To use GetLocalTime (instead of GetSystemTime as I used), replace

invoke GetSystemTime, addr stCurTime

with

invoke GetLocalTime, addr stCurTime

[/edit]
Website (very old): ossa.the-wot.co.uk

PBrennick

If you change ...

invoke GetDateFormat, LOCALE_USER_DEFAULT, NULL, addr stCurTime, addr szDFormat, addr szDate, MAX_DATE_LEN
invoke GetTimeFormat, LOCALE_USER_DEFAULT, NULL, addr stCurTime, addr szTFormat, addr szTime, MAX_TIME_LEN


to ...

invoke GetDateFormat, LOCALE_USER_DEFAULT, NULL, NULL, addr szDFormat, addr szDate, MAX_DATE_LEN
invoke GetTimeFormat, LOCALE_USER_DEFAULT, NULL, NULL, addr szTFormat, addr szTime, MAX_TIME_LEN


The time that is displayed in the console window will be formatted correctly according to your personal offset to GMT.

hth,
Paul


[attachment deleted by admin]
The GeneSys Project is available from:
The Repository or My crappy website

Ossa

Indeed, a much simpler method if all you want is the current time. However, if you want to display something other than the current time, then you must include the pointer to the SYSTEMTIME structure. An example of this might be displaying file times using the GetFileTime and FileTimeToSystemTime functions... It all depends on what the desired application is.

Ossa
Website (very old): ossa.the-wot.co.uk

PBrennick

Ossa,
You are right and it is important for someone who is learning to be aware of all the options.  I like your example as it is short and to the point which makes it a good learning example.  I have added it to my archives plus I have turned it into a project.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

theiod

I tried using the getdateformat and gettimeformat, but I'm using MASM615. I added PROTOs for them, but maybe I'm missing something. I get this error:

sTime.obj : error LNK2001: unresolved external symbol _GetDateFormat@24
sTime.obj : error LNK2001: unresolved external symbol _GetTimeFormat@24
sTime.exe : fatal error LNK1120: 2 unresolved externals

Any clues? Can I even use those libraries with masm 6.15?

Ossa

I take that you are using PBrennick's file? You aren't linking the correct lib files correctly apparently. It should work fine with your version of MASM.

Try the one attached instead - just double click the .bat file once you've extracted the files (assuming that masm is installed in the default directory).

Ossa

[attachment deleted by admin]
Website (very old): ossa.the-wot.co.uk

theiod

That worked when I ran the bat file.

I need to be able to make an exe though. Here is my make32.bat. When I try to assemble the program it dies. It doesn't know the include files. Infact I dont' have most of those seeing as I'm using MASM615 not MASM32

REM  make32.bat -  Custom batch file for assembling/linking the
REM  WinApp.asm program.
REM  Revised: 2/1/01

@echo off
cls

REM The following three lines can be customized for your system:
REM ********************************************BEGIN customize
PATH C:\Masm615
SET INCLUDE=C:\Masm615\include
SET LIB=C:\Masm615\lib
REM ********************************************END customize

ML -Zi -c -Fl -coff %1.asm
if errorlevel 1 goto terminate

REM add the /MAP option for a map file in the link command.

LINK32 %1.obj kernel32.lib user32.lib /SUBSYSTEM:WINDOWS
if errorLevel 1 goto terminate

dir %1.*

:terminate
pause

theiod

Correction I was running another app. The bat file doesn't work either. I don't have a masm32 I have a masm615 folder.

Ossa

In order to use any of the Windows API, you will need the correct includes that come with the MASM32 package (it still uses MASM 6.15, but just has all the related files that you will need). It IS possible to write all of the type/procedure definitions yourself, but it will be very annoying to do so. You will also need the .lib files - i think you can get these with some of the MS downloads.

I would recommend, however, that you just install MASM32.

Ossa
Website (very old): ossa.the-wot.co.uk

PBrennick

theiod,
Download and install masm32.  After it is installed, make a backup of the bin directory.  Once you have done that, copy all the files from your masm615 folder into \masm32\bin

Once you have done that, your copy of my project will build (once you restore it to the way it was originally.  I use version 6.15 pf ml.exe, also.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

jckl

You could also craete a struct to format the date and time like so.
DATETIME struct
   dmo BYTE 2 dup(?)
       BYTE '/'
   dda BYTE 2 dup(?)
       BYTE '/'
   dyr BYTE 4 dup(?)
       BYTE ' '
   dhour BYTE 2 dup(?)
         BYTE ':'
   dmin BYTE 2 dup(?)
        BYTE ':'
   dsec BYTE 2 dup(?)
        BYTE 0
DATETIME ends




Also to turn system time into the correct time zone you can use the following code.
   invoke GetTimeZoneInformation, ADDR TZI
   mov eax, TZI.Bias
   imul eax, 60
   sub SecsPast, eax
   mov eax, TZI.DaylightBias
   imul eax, 60
   sub SecsPast, eax

Mark Jones

Also Theiod, don't be confused by 16-bit and 32-bit code. 16-bit code is old DOS .com and .exe files. The MASM32 package will build 32-bit Windows code natively, which will not run in DOS. To build DOS code, MASM v6.15 can be used but you must use a 16-bit LINK.EXE. Just F.Y.I. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

theiod

Reply to Mark Jones:
I've written about a dozen 32-bit masm 6.15 programs that run in DOS.

Is there something else you are talking about?


Reply to all:
By the way I got MASM32 to work and that resolved it. Unfortunately I couldn't ever find a way to do it in straight MASM 6.15 code.

I really appreciate all your help. You guys are awesome.