[WinAPI] GetXxxFormat Functions

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

[WinAPI] GetXxxFormat Functions

02 Nov 2015, 09:14

GetXxxFormat Functions (National Language Support Functions) (msdn-docs)
- GetCurrencyFormat & GetCurrencyFormatEx (Formats a number string as a currency string)
- GetNumberFormat & GetNumberFormatEx (Formats a number string as a number string)
- GetDurationFormat & GetDurationFormatEx (Formats a duration of time as a time string)
- GetDateFormat & GetDateFormatEx (Formats a date as a date string)
- GetTimeFormat & GetTimeFormatEx (Formats time as a time string)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

GetCurrencyFormat (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetCurrencyFormat
; Return .....: Formats a number string as a currency string for a locale specified by identifier.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getcurrencyformata
; ===============================================================================================================================

GetCurrencyFormat(Value, Locale := 0x0400)
{
	if (Size := DllCall("GetCurrencyFormat", "uint", Locale, "uint", 0, "str", Value, "ptr", 0, "ptr", 0, "int", 0)) {
		VarSetCapacity(CurrencyStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetCurrencyFormat", "uint", Locale, "uint", 0, "str", Value, "ptr", 0, "str", CurrencyStr, "int", Size))
			return CurrencyStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetCurrencyFormat(1149.99)                        ; -> 1.149,99 €       ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetCurrencyFormat(1149.99, 0x0409)                ; -> $1,149.99        ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormat(1149.99, 0x0809)                ; -> £1,149.99        ( LANG_ENGLISH      | SUBLANG_ENGLISH_UK )
MsgBox % GetCurrencyFormat(1149.99, 0x0407)                ; -> 1.149,99 €       ( LANG_GERMAN       | SUBLANG_GERMAN     )


GetCurrencyFormatEx (Vista or later) (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetCurrencyFormatEx
; Return .....: Formats a number string as a currency string for a locale specified by name.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getcurrencyformatex
; ===============================================================================================================================

GetCurrencyFormatEx(Value, LocaleName := "!x-sys-default-locale")
{
	if (Size := DllCall("GetCurrencyFormatEx", "str", LocaleName, "uint", 0, "str", Value, "ptr", 0, "ptr", 0, "int", 0)) {
		VarSetCapacity(CurrencyStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetCurrencyFormatEx", "str", LocaleName, "uint", 0, "str", Value, "ptr", 0, "str", CurrencyStr, "int", Size))
			return CurrencyStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetCurrencyFormatEx(1149.99)                      ; -> 1.149,99 €       ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetCurrencyFormatEx(1149.99, "en-US")             ; -> $1,149.99        ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormatEx(1149.99, "en-GB")             ; -> £1,149.99        ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetCurrencyFormatEx(1149.99, "de-DE")             ; -> 1.149,99 €       ( LANG_GERMAN       | SUBLANG_GERMAN     )
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

GetNumberFormat (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetNumberFormat
; Return .....: Formats a number string as a number string customized for a locale specified by identifier.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getnumberformata
; ===============================================================================================================================

GetNumberFormat(Value, Locale := 0x0400)
{
	if (Size := DllCall("GetNumberFormat", "uint", Locale, "uint", 0, "str", Value, "ptr", 0, "ptr", 0, "int", 0)) {
		VarSetCapacity(NumberStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetNumberFormat", "uint", Locale, "uint", 0, "str", Value, "ptr", 0, "str", NumberStr, "int", Size))
			return NumberStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetNumberFormat(1149.99)                          ; -> 1.149,99         ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetNumberFormat(1149.99, 0x0409)                  ; -> 1,149.99         ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormat(1149.99, 0x0809)                  ; -> 1,149.99         ( LANG_ENGLISH      | SUBLANG_ENGLISH_UK )
MsgBox % GetNumberFormat(1149.99, 0x0407)                  ; -> 1.149,99         ( LANG_GERMAN       | SUBLANG_GERMAN     )


GetNumberFormatEx (Vista or later) (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetNumberFormatEx
; Return .....: Formats a number string as a number string customized for a locale specified by name.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getnumberformatex
; ===============================================================================================================================

GetNumberFormatEx(Value, LocaleName := "!x-sys-default-locale")
{
	if (Size := DllCall("GetNumberFormatEx", "str", LocaleName, "uint", 0, "str", Value, "ptr", 0, "ptr", 0, "int", 0)) {
		VarSetCapacity(NumberStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetNumberFormatEx", "str", LocaleName, "uint", 0, "str", Value, "ptr", 0, "str", NumberStr, "int", Size))
			return NumberStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetNumberFormatEx(1149.99)                        ; -> 1.149,99         ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetNumberFormatEx(1149.99, "en-US")               ; -> 1,149.99         ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormatEx(1149.99, "en-GB")               ; -> 1,149.99         ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetNumberFormatEx(1149.99, "de-DE")               ; -> 1.149,99         ( LANG_GERMAN       | SUBLANG_GERMAN     )
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:05

GetDurationFormat (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetDurationFormat
; Return .....: Formats a duration of time as a time string for a locale specified by identifier.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getdurationformat
; ===============================================================================================================================

GetDurationFormat(Duration, Format := "", Locale := 0x0400)
{
	if (Size := DllCall("GetDurationFormat", "uint", Locale, "uint", 0, "ptr", 0, "int64", Duration * 10000000, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0)) {
		VarSetCapacity(DurationStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetDurationFormat", "uint", Locale, "uint", 0, "ptr", 0, "int64", Duration * 10000000, "ptr", (Format ? &Format : 0), "str", DurationStr, "int", Size))
			return DurationStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetDurationFormat(43170)                                        ; -> 11:59:30
MsgBox % GetDurationFormat(43170, "mm' Minutes")                         ; -> 719 Minutes
MsgBox % GetDurationFormat(43170, "hh' Hours and 'mm' Minutes")          ; -> 11 Hours and 59 Minutes


GetDurationFormatEx (Vista or later) (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetDurationFormatEx
; Return .....: Formats a duration of time as a time string for a locale specified by name.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getdurationformatex
; ===============================================================================================================================

GetDurationFormatEx(Duration, Format := "hh:mm:ss", LocaleName := "!x-sys-default-locale")
{
	if (Size := DllCall("GetDurationFormatEx", "str", LocaleName, "uint", 0, "ptr", 0, "int64", Duration * 10000000, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0)) {
		VarSetCapacity(DurationStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetDurationFormatEx", "str", LocaleName, "uint", 0, "ptr", 0, "int64", Duration * 10000000, "ptr", (Format ? &Format : 0), "str", DurationStr, "int", Size))
			return DurationStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetDurationFormatEx(43170)                                      ; -> 11:59:30
MsgBox % GetDurationFormatEx(43170, "mm' Minutes")                       ; -> 719 Minutes
MsgBox % GetDurationFormatEx(43170, "hh' Hours and 'mm' Minutes")        ; -> 11 Hours and 59 Minutes


Possible Format Values

Code: Select all

d            -> days
h or H       -> hours
hh or HH     -> hours; if less than ten, prepend a leading zero
m            -> minutes
mm           -> minutes; if less than ten, prepend a leading zero
s            -> seconds
ss           -> seconds; if less than ten, prepend a leading zero
f            -> fractions of a second
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 02:16

GetDateFormat (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetDateFormat
; Return .....: Formats a date as a date string for a locale specified by the locale identifier.
;               The function formats either a specified date or the local system date.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/datetimeapi/nf-datetimeapi-getdateformata
; ===============================================================================================================================

GetDateFormat(Date, Format := "", Locale := 0x0400)
{
	VarSetCapacity(SYSTEMTIME, 16, 0)
	NumPut(SubStr(Date,  1, 4), SYSTEMTIME,  0, "ushort") ; Year
	NumPut(SubStr(Date,  5, 2), SYSTEMTIME,  2, "ushort") ; Month
	NumPut(SubStr(Date,  7, 2), SYSTEMTIME,  6, "ushort") ; Day
	NumPut(SubStr(Date,  9, 2), SYSTEMTIME,  8, "ushort") ; Hour
	NumPut(SubStr(Date, 11, 2), SYSTEMTIME, 10, "ushort") ; Minutes
	NumPut(SubStr(Date, 13, 2), SYSTEMTIME, 12, "ushort") ; Seconds

	if (Size := DllCall("GetDateFormat", "uint", Locale, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0)) {
		VarSetCapacity(DateStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetDateFormat", "uint", Locale, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "str", DateStr, "int", Size))
			return DateStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetDateFormat(A_Now)                              ; -> 01.09.2020                    ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetDateFormat(A_Now, "dddd',' dd. MMMM yyyy")     ; -> Dienstag, 01. September 2020  ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetDateFormat(A_Now, "", 0x0409)                  ; -> 09/01/2020                    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetDateFormat(A_Now, "yyyy, gg")                  ; -> 2020, n. Chr.                 ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )


GetDateFormatEx (Vista or later) (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetDateFormatEx
; Return .....: Formats a date as a date string for a locale specified by name.
;               The function formats either a specified date or the local system date.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/datetimeapi/nf-datetimeapi-getdateformatex
; ===============================================================================================================================

GetDateFormatEx(Date, Format := "", LocaleName := "!x-sys-default-locale")
{
	VarSetCapacity(SYSTEMTIME, 16, 0)
	NumPut(SubStr(Date,  1, 4), SYSTEMTIME,  0, "ushort") ; Year
	NumPut(SubStr(Date,  5, 2), SYSTEMTIME,  2, "ushort") ; Month
	NumPut(SubStr(Date,  7, 2), SYSTEMTIME,  6, "ushort") ; Day
	NumPut(SubStr(Date,  9, 2), SYSTEMTIME,  8, "ushort") ; Hour
	NumPut(SubStr(Date, 11, 2), SYSTEMTIME, 10, "ushort") ; Minutes
	NumPut(SubStr(Date, 13, 2), SYSTEMTIME, 12, "ushort") ; Seconds

	if (Size := DllCall("GetDateFormatEx", "str", LocaleName, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0, "ptr", 0)) {
		VarSetCapacity(DateStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetDateFormatEx", "str", LocaleName, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "str", DateStr, "int", Size, "ptr", 0))
			return DateStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetDateFormatEx(A_Now)                            ; -> 01.09.2020                    ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetDateFormatEx(A_Now, "dddd',' dd. MMMM yyyy")   ; -> Dienstag, 01. September 2020  ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetDateFormatEx(A_Now, "", "en-US")               ; -> 09/01/2020                    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetDateFormatEx(A_Now, "yyyy, gg", "en-US")       ; -> 2020, A.D.                    ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )


Possible Format Values
Defined in Day, Month, Year, and Era Format Pictures.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI GetXxxFormat Functions

03 Nov 2015, 03:10

GetTimeFormat (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetTimeFormat
; Return .....: Formats time as a time string for a locale specified by identifier.
;               The function formats either a specified time or the local system time.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/datetimeapi/nf-datetimeapi-gettimeformata
; ===============================================================================================================================

GetTimeFormat(Time, Format := "", Locale := 0x0400)
{
	VarSetCapacity(SYSTEMTIME, 16, 0)
	NumPut(SubStr(Time,  1, 4), SYSTEMTIME,  0, "ushort") ; Year
	NumPut(SubStr(Time,  5, 2), SYSTEMTIME,  2, "ushort") ; Month
	NumPut(SubStr(Time,  7, 2), SYSTEMTIME,  6, "ushort") ; Day
	NumPut(SubStr(Time,  9, 2), SYSTEMTIME,  8, "ushort") ; Hour
	NumPut(SubStr(Time, 11, 2), SYSTEMTIME, 10, "ushort") ; Minutes
	NumPut(SubStr(Time, 13, 2), SYSTEMTIME, 12, "ushort") ; Seconds

	if (Size := DllCall("GetTimeFormat", "uint", Locale, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0)) {
		VarSetCapacity(TimeStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetTimeFormat", "uint", Locale, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "str", TimeStr, "int", Size))
			return TimeStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetTimeFormat(A_Now)                              ; -> 11:46:49       ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetTimeFormat(A_Now, "", 0x0409)                  ; -> 11:46:58 AM    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetTimeFormat(A_Now, "HH':'mm")                   ; -> 11:46          ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetTimeFormat(A_Now, "hh':'mm':'ss tt", 0x0409)   ; -> 11:46:58 AM    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )


GetTimeFormatEx (Vista or later) (msdn-docs)

Code: Select all

; ===============================================================================================================================
; Function ...: GetTimeFormatEx
; Return .....: Formats time as a time string for a locale specified by name.
;               The function formats either a specified time or the local system time.
; Link .......: https://docs.microsoft.com/en-us/windows/win32/api/datetimeapi/nf-datetimeapi-gettimeformatex
; ===============================================================================================================================

GetTimeFormatEx(Time, Format := "", LocaleName := "!x-sys-default-locale")
{
	VarSetCapacity(SYSTEMTIME, 16, 0)
	NumPut(SubStr(Time,  1, 4), SYSTEMTIME,  0, "ushort") ; Year
	NumPut(SubStr(Time,  5, 2), SYSTEMTIME,  2, "ushort") ; Month
	NumPut(SubStr(Time,  7, 2), SYSTEMTIME,  6, "ushort") ; Day
	NumPut(SubStr(Time,  9, 2), SYSTEMTIME,  8, "ushort") ; Hour
	NumPut(SubStr(Time, 11, 2), SYSTEMTIME, 10, "ushort") ; Minutes
	NumPut(SubStr(Time, 13, 2), SYSTEMTIME, 12, "ushort") ; Seconds

	if (Size := DllCall("GetTimeFormatEx", "str", LocaleName, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "ptr", 0, "int", 0)) {
		VarSetCapacity(TimeStr, Size << !!A_IsUnicode, 0)
		if (DllCall("GetTimeFormatEx", "str", LocaleName, "uint", 0, "ptr", &SYSTEMTIME, "ptr", (Format ? &Format : 0), "str", TimeStr, "int", Size))
			return TimeStr
	}
	return false
}

; ===============================================================================================================================

MsgBox % GetTimeFormatEx(A_Now)                              ; -> 11:46:49       ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetTimeFormatEx(A_Now, "", "en-US")                 ; -> 11:46:58 AM    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )
MsgBox % GetTimeFormatEx(A_Now, "HH':'mm")                   ; -> 11:46          ( LANG_USER_DEFAULT | SUBLANG_DEFAULT    )
MsgBox % GetTimeFormatEx(A_Now, "hh':'mm':'ss tt", "en-US")  ; -> 11:46:58 AM    ( LANG_ENGLISH      | SUBLANG_ENGLISH_US )


Possible Format Values

Code: Select all

h      -> Hours with no leading zero for single-digit hours; 12-hour clock
hh     -> Hours with leading zero for single-digit hours; 12-hour clock
H      -> Hours with no leading zero for single-digit hours; 24-hour clock
HH     -> Hours with leading zero for single-digit hours; 24-hour clock
m      -> Minutes with no leading zero for single-digit minutes
mm     -> Minutes with leading zero for single-digit minutes
s      -> Seconds with no leading zero for single-digit seconds
ss     -> Seconds with leading zero for single-digit seconds
t      -> One character time marker string, such as A or P
tt     -> Multi-character time marker string, such as AM or PM
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Albireo
Posts: 1743
Joined: 16 Oct 2013, 13:53

Re: WinAPI - GetXxxFormat Functions

18 May 2016, 17:10

Thank You!
It works fine.

Do you still intend to supplement with "GetDateFormatEx" and "GetTimeFormatEx"?
Last edited by Albireo on 16 Sep 2020, 15:54, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

12 Sep 2017, 02:50

@Albireo: jNizM did do a GetDateFormat example here:
Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 935#p49935
I was looking forward to some date/time format examples! But when I got there, the cupboard was bare.

I was trying to find out if GetTimeFormat can handle milliseconds, the answer appears to be no. Although I have some date/time and milliseconds examples here:
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

@jNizM: You're welcome to adapt any of these examples.

Another useful link:
Language Identifier Constants and Strings (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Here are some examples with GetDateFormat and GetTimeFormat:

Code: Select all

q:: ;date format: dddd, dd MMMM yyyy
vDate := A_Now
vFormat := "dddd',' dd MMMM yyyy"
VarSetCapacity(SYSTEMTIME, 16, 0)
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)", "$1,$2,,$3,$4,$5,$6")
Loop, Parse, vDate, % ","
	NumPut(A_LoopField, &SYSTEMTIME, A_Index*2-2, "UShort")
;LANG_USER_DEFAULT := 0x400
vSize := DllCall("GetDateFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Ptr,0, Int,0)
VarSetCapacity(vDate2, vSize*(A_IsUnicode?2:1), 0)
if !(DllCall("GetDateFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Str,vDate2, Int,vSize))
	return
MsgBox, % vDate2
return

w:: ;time format: HH:mm:ss
vDate := A_Now
VarSetCapacity(SYSTEMTIME, 16, 0)
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)", "$1,$2,,$3,$4,$5,$6")
Loop, Parse, vDate, % ","
	NumPut(A_LoopField, &SYSTEMTIME, A_Index*2-2, "UShort")
;LANG_USER_DEFAULT := 0x400
vSize := DllCall("GetTimeFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,0, Ptr,0, Int,0)
VarSetCapacity(vDate2, vSize*(A_IsUnicode?2:1), 0)
if !(DllCall("GetTimeFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,0, Str,vDate2, Int,vSize))
	return
MsgBox, % vDate2
return

e:: ;time format: hh:mm:ss tt
vDate := A_Now
vFormat := "hh':'mm':'ss tt"
VarSetCapacity(SYSTEMTIME, 16, 0)
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)", "$1,$2,,$3,$4,$5,$6")
Loop, Parse, vDate, % ","
	NumPut(A_LoopField, &SYSTEMTIME, A_Index*2-2, "UShort")
;LANG_USER_DEFAULT := 0x400
vSize := DllCall("GetTimeFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Ptr,0, Int,0)
VarSetCapacity(vDate2, vSize*(A_IsUnicode?2:1), 0)
if !(DllCall("GetTimeFormat", UInt,0x400, UInt,0, Ptr,&SYSTEMTIME, Ptr,&vFormat, Str,vDate2, Int,vSize))
	return
MsgBox, % vDate2
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

18 Dec 2017, 11:57

It might be worth including this here. StrFormatByteSize64 to display a file size in a friendly format.

Code: Select all

q:: ;friendly display file size
vText := ""
vSizeFile := 123456789
VarSetCapacity(vText, vSize := 260*2, 0) ;(no suggested size in MSDN)
DllCall("shlwapi\StrFormatByteSize64", Int64,vSizeFile, Str,vText, UInt,vSize, Ptr)
MsgBox, % vText ;117 MB
return
Link:
FormatBytes() - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3567
[EDIT:] Changed from StrFormatByteSize to StrFormatByteSize64. StrFormatByteSize handles UInt, StrFormatByteSize64 handles Int64.
Last edited by jeeswg on 19 Dec 2017, 06:36, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI - GetXxxFormat Functions

19 Dec 2017, 06:16

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 06:53

Regarding the above:
@ jNizM: At your github, the StrFormatByteSize64() function is still called StrFormatByteSizeEx(). Is that intended or just a copy/paste leftover?

Has anyone noticed the huge stupid change M$ did in these byte-formatting APIs?
StrFormatKBSize
StrFormatByteSizeA
StrFormatByteSizeW
StrFormatByteSize64
StrFormatByteSizeEx

Quote from MSDN:
In Windows 10, size is reported in base 10 rather than base 2. For example, 1 KB is 1000 bytes rather than 1024.
Imagine the confusion when any two people, one using Win10 and the other anything else, try to compare results of the same whatever code (AHK, C/C++/C#, etc) that's using those APIs.

Maybe M$ intend to make the BYTE 10 bits long instead of 8. Maybe they want to move everything to decimal base…

Forgot to say thank you for these GetXxxFormat functions, I needed them so many times in the past and never stumbled into these APIs. Or maybe I did and just forgot. Anyways, thank you jNizM! :thumbup:
Part of my AHK work can be found here.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 07:05

@Drugwash: Yes, the changes are very confusing.

FormatBytes() - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3567
No, the documentation is clearly wrong. On Windows 10.0.10586, SKAN's example code and Explorer itself both show 1GB as 1024*1024*1024 bytes.
StrFormatByteSize64 function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
StrFormatByteSize64 can be used for either ANSI or Unicode characters. However, while StrFormatByteSize64A can be called directly, StrFormatByteSize64W is not defined. When StrFormatByteSize64 is called with a Unicode value, StrFormatByteSizeW is used.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: WinAPI - GetXxxFormat Functions

20 Dec 2017, 08:11

Ah, thanks, I was beating a dead horse. :oops:
Anyway, the documentation is even worse as it doesn't mention terabytes, petabytes, exabytes as possible result but only maximum gigabytes.

Second quote hides a catch: the automatic switch to StrFormatByteSizeW() is not done in AHK but only in C code.
Calling "shlwapi\StrFormatByteSize64" with either a Unicode or ANSI buffer returns zero but doesn't set A_LastError.

Well, that's about it, we're kinda off-topic here. :)
Part of my AHK work can be found here.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WinAPI - GetXxxFormat Functions

01 Sep 2020, 04:45

I forgot to finish that. I hope I have everything ready now and have not missed anything.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Rohwedder and 79 guests