Getting DateTime in Win32

Getting DateTime in Win32


Is there a way to retrieve the system's date and time in a Win32 fashion or is it just a matter of making a call to date_and_time or getdat?



14 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.



	! note -- all SYSTEMTIME components are INTEGER*2
	!  integer(WORD) wYear		 ! knowns  WORD 
	!  integer(WORD) wMonth		 ! knowns  WORD 
	!  integer(WORD) wDayOfWeek		 ! knowns  WORD 
	!  integer(WORD) wDay			 ! knowns  WORD 
	!  integer(WORD) wHour		 ! knowns  WORD 
	!  integer(WORD) wMinute		 ! knowns  WORD 
	!  integer(WORD) wSecond		 ! knowns  WORD 
	!  integer(WORD) wMilliseconds	 ! knowns  WORD 

	CALL GetLocalTime (st)


How did you learn about this function/type/call? I have a Programming Windows book which I hoped would have some info on Win32 APIs, but it doesn't seem to contain the info I need.


Well, Petzold is a tutorial book rather than reference.

The primary referential material is Microsoft "Platform SDK" documentation.
- it is bundled with CVF online help
- however (probably due to licensing issues) it is not bundled with IVF documentation. You can find it in MSDN if you installed it with VS.NET
- it is available online on MSDN site
- You can freely download an older, but quite useful and concise version from Borland



Thanks Jugoslav! If I wanted to retrieve the information from this, I would do the following...

Year_Now = st.wYear


Man, Win32 library is HUGE! I know for those that have been working with it for a long time that's kind of like a "duh" statement, but from a newbie standpoint, it's like being given a haystack and trying to find the needle. The good thing is that there is a way to search it online and offline.


Components within defined types are punctuated by % or period.

If your variable is a standard F90 integer, then

Year_Now = INT4(st%wYear)

Right. (Except, you can save yourself a latter hassle by using standard form st%wYear).

Windows API is huge indeed. If you can find a "book-chapter" version of documentation (such as MSDN or CVF version), you will find that docs are fairly well organized (Introduction, detailed explanations and reference chapter) and routines are mostly logically named (except when not :-) ). However, that doesn't make it much easier to grab (nor every programmer needs all of it -- my personal rough estimate is that I didn't use more than 30% of it). Petzold is IMO an excellent starting book, but it mostly discuses typical GUI aspects. Jerry Richter's "Advanced Windows" is more comprehensive but also more advanced.



Awesome! Thanks guys!

I'm looking in MSDN, but I haven't found whether there is a function for calculating the Julian date. There are a few calls that I don't think I can apply to the Fortran code. I'm not sure what the limitations are in using functions in other languages.

I searched the forum and found code that calculates the Julian date, but I didn't see any reference to a Win32 API call that would retrieve this information.

Does anyone know if this exists?

Not that I know of, but there are zillions of Julian Date routines out there (in Fortran) that will take the values you get from DATE_AND_TIME and create a Julian date for you. This is a classic beginning programming assignment.

Retired 12/31/2016


	FUNCTION julian_date (yyyy, mm, dd) RESULT (julian)
		!          converts calendar date to Julian date
		!          cf Fliegel & Van Flandern, CACM 11(10):657, 1968
		!          example: julian_date(1970,1,1)=2440588
		INTEGER,INTENT(IN)           :: yyyy,mm,dd
		INTEGER                      :: julian
		julian = dd - 32075 + 1461*(yyyy + 4800 + (mm - 14)/12)/4 +  &
				 367*(mm - 2 - ((mm - 14)/12)*12)/12 -               &
				 3*((yyyy + 4900 + (mm - 14)/12)/100)/4
	END FUNCTION julian_date

	SUBROUTINE get_ymd (jd, yyyy, mm, dd)
		!          expands a Julian date into a calendar date
		!          cf Fliegel & Van Flandern, CACM 11(10):657, 1968
		INTEGER,INTENT(IN)           :: jd
		INTEGER,INTENT(OUT)          :: yyyy,mm,dd
		INTEGER                      :: l,n
		l		= jd + 68569
		n		= 4*l/146097
		l		= l - (146097*n + 3)/4
		yyyy	= 4000*(l + 1)/1461001
		l		= l - 1461*yyyy/4 + 31
		mm		= 80*l/2447
		dd		= l - 2447*mm/80
		l		= mm/11
		mm		= mm + 2 - 12*l
		yyyy	= 100*(n - 49) + yyyy + l

	INTEGER FUNCTION dow (yyyy,mm,dd)
		!          Day_Of_Week: (0=Sunday,1=Monday...6=Saturday)
		!          cf J.D.Robertson, CACM 15(10):918
		INTEGER,INTENT(IN)           :: yyyy,mm,dd
		dow = MOD((13*(mm+10-(mm+10)/13*12)-1)/5+dd+77           &
			  +5*(yyyy+(mm-14)/12-(yyyy+(mm-14)/12)/100*100)/4   &

	INTEGER FUNCTION ndiy (yyyy,mm,dd)
		!          day count in year
		!          cf J.D.Robertson, CACM 15(10):918
		INTEGER,INTENT(IN)           :: yyyy,mm,dd
		ndiy = 3055*(mm+2)/100-(mm+10)/13*2-91               &
			   +(1-(MOD(yyyy,4)+3)/4+(MOD(yyyy,100)+99)/100  &

Actually, SystemTimeToFileTime and FileTimeToSystemTime are Win32 routines for conversion between Julian date (FILETIME = INTEGER(8)) and SYSTEMTIME structure.



Yeah, I remember having to do this for a programming assignment about 10 years ago... or has it been longer... time flies :) Of course, I haven't been developing that long. I've worn so many hats since then. WithWin32 programming, I kept hearingthat there wereAPIs for everything you wanted to do. Unless Microsoft didn't want to take away from the professor's programming lessons, I was thinking there has to be an API for calculating the Julian date. I was looking around, but I didn't see anything that matched.

Paul-Curtis, thanks for the code samples. This helps me in not having to reinvent the wheel ;) Jugoslav, I saw the APIs that you mentioned, but I didn't think they would apply. I'll take another look at them. Thanks for pointing them out :)

Yes, I didn't know that the term "Julian date" has well-defined starting point (checking... Jan 1 4713 BC) -- I thought the term refers to any system of time counting from an arbitrary initial date. FILETIME certainly fulfills the latter definition, but it's not a "Julian date". Indeed, Win32 API doesn't have one.



Leave a Comment

Please sign in to add a comment. Not a member? Join today