Convert YmdHms format date to something human readable?

Started by snowsnowsnow, February 15, 2020, 02:41:11 AM

Previous topic - Next topic

snowsnowsnow

I suppose one might argue that it is already (sort of) human readable, but I'd like something better.  I'm partial to the output of the "ctime" function in Unix.

So, I'm asking if there is some built-in function to do this.  Searching the help file, I could not find one, but maybe I've overlooked something.  If so, please advise.

That said, I was able to work up a method, but, again, I'd prefer something already there.  Here is my method:

Code (winbatch) Select

msvcrt = DllLoad("msvcrt.dll")
buff = BinaryAlloc(4)

; Convert a TimeYmdHms string to text...
; The "ActiveTimeBias" is the number of minutes that we are behind GMT.
; We multiply it by 60 to get seconds,
; then add it to the calculated time value.
Now = TimeYmdHms()
tme = TimeDiffSecs(Now,"1970:01:01:0:0:0") + 60*RegQueryDword(@REGMACHINE, "System\CurrentControlSet\control\TimeZoneInformation[ActiveTimeBias]")
BinaryPoke4(buff,0,tme)
Pause("tme = %tme%",DllCallCdecl(msvcrt,lpstr:"ctime",lpbinary:buff))

stanl


td

Unless something has changed -which I doubt- Snow++'s version of WinBatch predates the addition of the TimeFormat function.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on February 15, 2020, 07:34:08 AM
Unless something has changed -which I doubt- Snow++'s version of WinBatch predates the addition of the TimeFormat function.


I know that. My reply was just to answer I'd prefer something already there

snowsnowsnow

Yes, I don't have that function.

Just out of curiosity, how does it work?

stanl


snowsnowsnow

Which I know nothing about.

I was hoping for more detail from you.  Not just a couple of words...

JTaylor

TimeFormat
Returns the date and/or time as a text string in the indicated format.

Syntax:
TimeFormat (ymdhms,format)

Parameters:
(s) ymdhms  Date/time in YYYY:MM:DD:HH:MM:SS WIL time format.

(s) format  Indicates the date and/or time format to be returned by the function.  see below.

Returns:
(s) formatted date and/or time.  The maximum total expanded size of the returned string is limited to 2048 characters.



The following table lists the standard Windows date and time format characters accepted by the function.  Non-format characters are accepted but should be enclosed in single (') quotes to avoid non-format characters being used for date/time formatting.  In order to include a single quote in the returned text, escape it with a second single quote.  For example, '''' will create one single quote in the returned text.

Format
Used For
Description

d
Days
Displays the day as a number from 1 to 31

dd
Days
Displays the day as a number with a leading 0 if the number is less than 10

ddd
Weekdays
Displays a weekday name with 3 letters as Mon, Tue, etc

dddd
Weekdays
Displays the complete name of a week day as Monday, etc

M
Months
Displays the numeric month from 1 to 12

MM
Months
Displays the numeric month with a leading 0 if the number is less than 10

MMM
Months
Displays the short name of the month as Jan, Feb, Mar, etc

MMMM
Months
Displays the complete name of the month as January, etc

yy
Years
Displays two digits for the year as 00 for 2000 or 03 for 2003

yyyy
Years
Displays the numeric year with 4 digits

h
Hours
Hours with no leading zero for single-digit hours; 12-hour clock

hh
Hours
Hours with leading zero for single-digit hours; 12-hour clock

H
Hours
Hours with no leading zero for single-digit hours; 24-hour clock

HH
Hours
Hours with leading zero for single-digit hours; 24-hour clock

m
Minutes
Minutes with no leading zero for single-digit minutes

mm
Minutes
Minutes with leading zero for single-digit minutes

s
Seconds
Seconds with no leading zero for single-digit seconds

ss
Seconds
Seconds with leading zero for single-digit seconds

t
Time Mark
Two-character abbreviation (for example, AM or PM)*


* This function does not support single character time marks.

mstarliper

I feel like I'm intruding - forgive me...

Curiosity only - Why add the ActiveTimeBias in? Doesn't TimeYmdHms already account for that?

Also, I know it isn't ctime, but TimeDate/DateTime states specifically "Provides the current data and time in a human-readable format".

snowsnowsnow

Quote from: mstarliper on February 17, 2020, 06:16:01 AM
I feel like I'm intruding - forgive me...
No problem.  Discussion is good.  Welcome to my thread!

QuoteCuriosity only - Why add the ActiveTimeBias in? Doesn't TimeYmdHms already account for that?
Let me explain. 
The Unix "epoch" is defined as midnight at the beginning of 1970 - in UTC.  So, whenever you pass 0 to a Unix function that expects a time value (time_t in C-speak), you are referring to midnight at the beginning of January 1st, 1970, UTC.  Equivalently, 0 also refers to 4 PM, December 31st, 1969, PST.  Therefore, the number 8*3600 (28800) refers to Midnight, January 1st, 1970, PST.

So, when I use TimeDiffSecs() to subtract 1970:01:01 from the Now, I get the number of seconds ago between now and that particular midnight *in the local time zone*, or, following the example above of using PST as an example, it gives me the number of seconds ago that was 8 AM UTC (on 1/1/70).  This is obviously short of the number I want by 28800, so I have to add back that 28800 to make it come out right.

QuoteAlso, I know it isn't ctime, but TimeDate/DateTime states specifically "Provides the current data and time in a human-readable format".

TimeDate() takes no arguments.  Therefore, it is only capable of generating a date/time string for the current time.  I need something that will work for any YmdHms value.

mstarliper

Thanks for explaining. It makes perfect sense - I wasn't thinking epoch, even though I saw it in the code.

snowsnowsnow

As a final note on this, you can also do it with strftime() (note that the new TimeFormat() function in WB seems to be a re-implementation of Unix strftime(), done MS-style), like this

Code (winbatch) Select

buff = BinaryAlloc(4)
; tme is as calculated previously
BinaryPoke4(buff,0,tme)
buff80 = BinaryAlloc(80)
BinaryEodSet(buff80,80)
tm_ptr = DllCallCdecl(msvcrt,long:"localtime",lpbinary:buff)
DllCallCdecl(msvcrt,long:"strftime",lpbinary:buff80,long:80,lpstr:"%%c",long:tm_ptr)
Message("Formateed string:",BinaryPeekStr(buff80,0,80))


Note that the above code assumes that longs and pointers are the same size, which is OK in 32-bit land.  Not sure how that will translate to 64-bit land.

td

The WIL TimeFormat function's format string is based on the format string used by Microsoft's DateTime Common Control.  That format string is similar to but not the same as the one used by an MSFT C runtime function.  The WIL function does not simply call an MSFT function either.  It does its own parsing and only relies on the system for getting the locale of the conversion to match the current process's locale.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow