How get data/time independent from localization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How get data/time independent from localization

#91 Post by aGerman » 22 Oct 2015 14:03

@Squashman
Either of these commands does work. But if I'm not totally blind there isn't any time or date output.

Are these performance counter translated

At least these object names are not translated for me.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#92 Post by Squashman » 22 Oct 2015 14:24

aGerman wrote:@Squashman
Either of these commands does work. But if I'm not totally blind there isn't any time or date output.

Are these performance counter translated

At least these object names are not translated for me.

Not from that specific LOGMAN option. I was just trying to find out if the GUID's are the same. Then we could use a different LOGMAN option using that GUID to get the date time output. Just wanted to know if the GUID's are the same for everyone regardless of language.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How get data/time independent from localization

#93 Post by npocmaka_ » 22 Oct 2015 15:37

Squashman wrote:
aGerman wrote:@Squashman
Either of these commands does work. But if I'm not totally blind there isn't any time or date output.

Are these performance counter translated

At least these object names are not translated for me.

Not from that specific LOGMAN option. I was just trying to find out if the GUID's are the same. Then we could use a different LOGMAN option using that GUID to get the date time output. Just wanted to know if the GUID's are the same for everyone regardless of language.



Code: Select all

>logman query providers {36C23E18-0E66-11D9-BBEB-505054503030}

Provider                                 GUID
-------------------------------------------------------------------------------
Microsoft-Windows-Diagnostics-Networking {36C23E18-0E66-11D9-BBEB-505054503030}


same here though very few thing are localized on bulgarian versions of windows.And how this can be used with typeperf :?:




I think network abbreviations are not translated even on japanese Windowses based on what searched on google.
UDPv4 prints the less information and on my machine delay is around 1 sec (with about half a second if put |Findstr in the for definition I think with IF condition it is faster)

The problem is that on windows XP there's only one UDP protocol called UDP (no UDP version 6) so I've tried to made the fastest possible check - through environment variable:

Code: Select all

@echo off
setlocal

::check if windows is XP
if defined USERDOMAIN_roamingprofile (set "v=v4") else (set "v=")

set "mon="
for /f "skip=2 delims=," %%# in ('typeperf "\UDP%v%\*" -si 0 -sc 1') do (
   if not defined mon (
      for /f "tokens=1-7 delims=.:/ " %%a in (%%#) do (
        set mon=%%a
        set date=%%b
        set year=%%c
        set hour=%%d
        set minute=%%e
        set sec=%%f
        set ms=%%g
      )
   )
)

echo %year%%mon%%date%
echo %hour%%minute%%sec%%ms%
endlocal

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#94 Post by Squashman » 23 Oct 2015 10:37

I searched through this thread and I don't see anyone asking about the NET TIME command. I would assume this is regional dependent as well.

Code: Select all

H:\>NET TIME \\%ComputerName%
Current time at \\Squashputer is 10/23/2015 11:37:26 AM

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: How get data/time independent from localization

#95 Post by mcnd » 23 Oct 2015 14:32

I think I have not seen this used in this topic.

Code: Select all

set "year="
for /f "tokens=1-6 delims=/: " %%a in ('robocopy "|" . /njh') do if not defined year (
    set "year=%%a" & set "month=%%b" & set "day=%%c"
    set "hour=%%d" & set "min=%%e"   & set "sec=%%f"
)

echo %year%/%month%/%day% %hour%:%min%:%sec%


The basic idea is to force an error in the robocopy command and parse the date and time of the error in the output.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#96 Post by Squashman » 23 Oct 2015 14:37

mcnd wrote:
The basic idea is to force an error in the robocopy command and parse the date and time of the error in the output.

Page3 viewtopic.php?p=29762#p29762
Robocopy is not native to XP. You also can just use ROBOCOPY. You don't need to force any error to it.

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: How get data/time independent from localization

#97 Post by mcnd » 23 Oct 2015 15:06

Squashman wrote:Page3 viewtopic.php?p=29762#p29762


Page 4 shows that the output is language dependant

Also, for me, with a limited user without rights over the root of the drive, the indicated command fails.

Squashman wrote:Robocopy is not native to XP


Yes, I know. Included here just for completion as I saw robocopy used in other posts.

Squashman wrote: You also can just use ROBOCOPY. You don't need to force any error to it.


Forcing the error we get date/time in yyyy/mm/dd hh:nn:ss format without even hitting the file system.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#98 Post by Squashman » 23 Oct 2015 16:09

Code: Select all

C:\>robocopy

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Fri Oct 23 17:08:43 2015

       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?


****  /MIR can DELETE files as well as copy them !

C:\>robocopy |find "Started :"
  Started : Fri Oct 23 17:08:55 2015

C:\>

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How get data/time independent from localization

#99 Post by npocmaka_ » 23 Oct 2015 16:39

mcnd wrote:I think I have not seen this used in this topic.

Code: Select all

set "year="
for /f "tokens=1-6 delims=/: " %%a in ('robocopy "|" . /njh') do if not defined year (
    set "year=%%a" & set "month=%%b" & set "day=%%c"
    set "hour=%%d" & set "min=%%e"   & set "sec=%%f"
)

echo %year%/%month%/%day% %hour%:%min%:%sec%


The basic idea is to force an error in the robocopy command and parse the date and time of the error in the output.



This is clever and fast , though the info header with day of the week is missing (one of good features of robocopy).
Output is always like:
2015/10/24 01:35:53 ERROR 123 (0x0000007B) Accessing Source Directory C:\Users\..
The filename, directory name, or volume label syntax is incorrect.


So the first part does not depend on the language.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How get data/time independent from localization

#100 Post by penpen » 24 Oct 2015 05:08

How about this (if needed Format could be modified):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

:: read format from registry
set "variables="iDate" "sDate" "sShortDate" "iTime" "iTimePrefix" "s1159" "s2359" "sTime" "sTimeFormat" "sDecimal""
for %%a in (%variables%) do set "%%~a="
for /F "tokens=1,3" %%a in ('reg query "HKCU\Control Panel\International" ^| findstr "%variables%"') do (
   set "%%~a=%%~b"
   set "variable.%%~a=true"
)
for %%a in (%variables%) do if defined variable.%%~a (
   set "variables=!variables:"%%~a"=!"
   set "variable.%%~a="
)
if not "!variables: =!" == "" for /F "tokens=1,3" %%a in ('reg query "HKU\.DEFAULT\Control Panel\International" ^| findstr "%variables%"') do (
   set "%%~a=%%~b"
   set "variable.%%~a="
)
if "!variables: =!" == "" ( set "variables="
) else echo(@todo: Error: %variables %


:: set date format
:: date[iDate]
set "date[0]=month%sDate%day%sDate%year"
set "date[1]=day%sDate%month%sDate%year"
set "date[2]=year%sDate%month%sDate%day"

for /F "tokens=1-6 delims=%sDate%" %%a in ("!date[%iDate%]!%sDate%%date%") do (
   set "%%~a=%%~d"
   set "%%~b=%%~e"
   set "%%~c=%%~f"
)
for /L %%a in (0, 1, 2) do set "date[%%~a]="


:: set date format
:: time[iTime][iTimePrefix]
set "time[0][0]=hour%sTime%minute%sTime%:second%sDecimal%centisecond timeMarker"
set "time[0][1]=timeMarker hour%sTime%minute%sTime%:second%sDecimal%centisecond"
set "time[1][0]=hour%sTime%minute%sTime%:second%sDecimal%centisecond timeMarker"
set "time[1][1]=hour%sTime%minute%sTime%:second%sDecimal%centisecond timeMarker"

for /F "tokens=1-10 delims=%sTime%%sDecimal% " %%a in ("!time[%iTime%][%iTimePrefix%]! !time!") do (
   set "%%~a=%%~f"
   set "%%~b=%%~g"
   set "%%~c=%%~h"
   set "%%~d=%%~i"
   set "%%~e=%%~j"
)
for /L %%a in (0, 1, 1) do for /L %%b in (0, 1, 1) do set "time[%%~a][%%~b]="


:: remove trailing zeroes
for %%a in ("year" "month" "day" "hour" "minute" "second" "centisecond") do (
   set "%%~a=0000!%%~a!"
   set /A "%%~a=1!%%~a:~-4!-10000"
)

:: convert to 24 if needed: actually not implemented
::    AM    PM     : h:m:s - hh:mm:ss   .
:: s1159 s2359 sTime sTimeFormat sDecimal
:: if "%s1159%" == "%timeMarker%" if %hour%" == "12" set hour...


:: display some info
if "%iTime%" == "0" echo(clock      :   12 hour
if "%iTime%" == "1" echo(clock      :   24 hour

if "%iTimePrefix%" == "0" echo(time marker:    suffix
if "%iTimePrefix%" == "1" echo(time marker:    prefix

echo(

set year
set month
set day
set hour
set minute
set second
set centisecond
if defined timeMarker set timeMarker

endlocal
goto :eof
Works on xp and 8.1.


penpen

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: How get data/time independent from localization

#101 Post by mcnd » 24 Oct 2015 06:13

npocmaka_ wrote: ..., though the info header with day of the week is missing (one of good features of robocopy).


For a locale independed we can use

Code: Select all

set "year="
for /f "tokens=1-6 delims=/: " %%a in ('robocopy "|" . /njh') do if not defined year (
    set "year=%%a" & set "month=%%b" & set "day=%%c"
    set "hour=%%d" & set "min=%%e"   & set "sec=%%f"
   
    rem Zeller's congruence : https://en.wikipedia.org/wiki/Zeller's_congruence
    if %%b lss 3 (set /a "y=%%a-1") else (set "y=%%a")
    set /a "dow=((1%%c %% 100)+(13*((((1%%b %% 100)+9) %% 12 + 3)+1)/5)+(y %% 100)+(y %% 100)/4+(y/100)/4-2*(y/100)) %% 7"

    rem Day correction from Zeller's output (0-Sat to 6-Fri) to ISO-8601 (1-Mon to 7-Sun)
    set /a "dow=(dow+5)%%7+1"
)

echo %year%/%month%/%day% %hour%:%min%:%sec% %dow%


edited: I forgot to include the correction for the initial zeros in day and month and the year correction. Now corrected

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#102 Post by Squashman » 13 Jan 2016 16:58

I am wondering if these are bullet proof.
http://www.commandline.co.uk/cmdfuncs/dandt/#getdate

The author said it stemmed from an old alt.msdos post here:
https://groups.google.com/forum/?hl=en# ... pEtjA07VAJ

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How get data/time independent from localization

#103 Post by dbenham » 13 Jan 2016 17:22

I only looked at the :GetDate function, and it is definitely NOT locale agnostic. It is highly dependent on the language specific abbreviations used for year, month and day.

Way back when I attempted to provide a platform for handling multiple languages based on that technique: viewtopic.php?f=3&t=1808


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How get data/time independent from localization

#104 Post by Squashman » 26 Oct 2016 20:56

Did we ever discuss the WHERE command with the /T option. Is it region independent?

Code: Select all

C:\Users\Squashman>where /T cmd.exe
    233984   10/30/2015    2:17:49 AM  C:\WINDOWS\System32\cmd.exe

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How get data/time independent from localization

#105 Post by dbenham » 26 Oct 2016 21:01

I'd be willing to bet a tidy sum that WHERE /T date/time format is locale dependent.

Dave Benham

Post Reply