You are not logged in.

#1 01 Apr 2007 08:00

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Getting current time

Test it and let me know what you think. I'm trying to get this to work for all regions.

Thanks!

Updated: (Should work for all regions!!)

@ECHO OFF

set _foundNum=
set _hour=
set _min=
set _sec=
set _tic=

set /A _foundNum=0

for /F "tokens=* delims= " %%g in ('echo.^|time') do for %%h in (%%g) do call :GET_TIME %%h

echo.%_hour%:%_min%:%_sec%.%_tic%

goto END

:GET_TIME

if NOT "%1:~0,1%" GTR "9" set /A _foundNum+=1
if %_foundNum%==0 goto :eof

for /F "delims=:. tokens=1-4" %%g in ("%1") do (
          set _hour=%%g&set _min=%%h&set _sec=%%i&set _tic=%%j)

set /A _foundNum=0

goto :eof

:END
set _foundNum=
set _hour=
set _min=
set _sec=
set _tic=
@ECHO OFF
set _hour=
set _min=
set _sec=
set _tic=

for /F "skip=1 tokens=1-5 delims=:." %%g in ('echo.^|time^|sort') do (
set /A _hour="%%h"&&set /A _min="%%i"&&set /A _sec="%%j"&&set /A _tic="%%k")

echo.%_hour%:%_min%:%_sec%.%_tic%

set _hour=
set _min=
set _sec=
set _tic=

Last edited by //[T.0.P]// (02 Apr 2007 11:40)

Offline

#2 01 Apr 2007 11:04

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Getting current time

I can see what your getting at with that, but I don't know why you're making life difficult for yourself by parsing the output of "time" instead of using "time /t" or, better yet, "%time%".

That aside, I should point out that it's flawed.

You're using "set /a" which will mean any time you try to set a value of "08", or "09" then an error will be thrown, such as this:

C:\>timesplit.cmd
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
10:23:.

This is because "set /a" will treat any number with a leading 0 as octal, which it will attempt to store as decimal.  Obviously "08" and "09" are not value numbers in octal, hence the above error.

Do you follow me?  Splendid.

Anyhow, I'd have done it more like this:

set _hour=%time:~0,2%&set _min=%time:~3,2%&set _sec=%time:~6,2%&set _tic=%time:~9,2%

Or to use the "for" method for a bit more clarity:

for /f "delims=.: tokens=1,2,3,4" %%a in ("%time%") do (
    set _hour=%%a
    set _min=%%b
    set _sec=%%c
    set _tic=%%d
)

cmd | *sh | ruby | chef

Offline

#3 01 Apr 2007 12:44

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Getting current time

bluesxman wrote:

or, better yet, "%time%".

Oh. Never heard of the environment variable %time% before. So will %time% work with NT/2K/XP and 2K3? Just curious. Because I just tried %date% and I get the same thing 'date /t' would give you, and there's a whole script on getting the dates into their own variables that will work for all regional settings and will work under NT/2K/XP: GetDate.cmd

bluesxman wrote:

Do you follow me?

Yeah I would get some errors like that trying to write it differently. I didn't understand it. When I was trying it out I didn't see any errors writting it this way. Now I just ran the script though a loop count of a 100 and finally saw those invalid number errors.

I tried to write it as short as possible. The reason why I used 'set /A' is because I was trying to set each value as a number. If you tried to just use regular 'set'. You would get some spaces in the values of some variables, more likely the hour variable. So you would have to echo it out like %_hour: =%. Trying to elimate the spaces within the same FOR loop would require to call another sub routine because with the english text, there's 8 arguments to be passed.

But here's a better version of what I was trying to do. Which WOULD work for all OS's and I hope all regional settings as well:

@ECHO OFF

set _foundNum=
set _hour=
set _min=
set _sec=
set _tic=

set /A _foundNum=0

for /F "skip=1 tokens=* delims= " %%g in ('echo.^|time^|sort') do for %%h in (%%g) do call :GET_TIME %%h

echo.%_hour%:%_min%:%_sec%.%_tic%

goto END

:GET_TIME

if NOT "%1:~0,1%" GTR "9" set /A _foundNum+=1
if %_foundNum%==0 goto :eof

for /F "delims=:. tokens=1-4" %%g in ("%1") do (set _hour=%%g&set _min=%%h&set _sec=%%i&set _tic=%%j)

goto :eof

:END
set _foundNum=
set _hour=
set _min=
set _sec=
set _tic=

This way, it wouldn't matter how lengthy the actual text would be and it would still retrieve the number data in the string. The only thing short of this script is if the different language packs say different things in the console. So maybe in Spanish the console reads "Current time to enter:" or whatever.

Offline

#4 01 Apr 2007 13:18

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Getting current time

I just added 'set /A _foundNum=0' at the end of the GET_TIME sub routine and changed the FOR /F loop, so it should work regardless of regional settings:

for /F "tokens=* delims= " %%g in ('echo.^|time') do for %%h in (%%g) do call :GET_TIME %%h

Offline

#5 01 Apr 2007 16:56

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Getting current time

One other regional setting to consider is Daylight Savings Time.

If you are syncing jobs between machines and theres a chance they are in different regions then this vbs script will show the DST settings
http://ss64.com/vb/syntax-getdatetime.html

wikipedia have some good references
http://en.wikipedia.org/wiki/Daylight_s … #Computing

Offline

#6 02 Apr 2007 11:39

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Getting current time

Yeah I've been thinking about how to catch Daylight Savings Time all day. I just came under the impression that it was impossible since it's on a different day every year and some regions don't have it. With that said I don't know why there was a patch for Windows to get this year's DST, oh well maybe it was a fixed number of days to select every year.. who knows. I never payed attention to that until now.

I found out where the DST settings are saved in the registry. But that didn't help me either. I was looking inside the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones and HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation. I saw if I disabled DST the TimeZoneInformation folder would change add/change values. But that didn't get me too far either.

I'll read up on those links you've sent me and see what I can come up with. Thanks for the info!

Offline

#7 02 Apr 2007 12:44

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Getting current time

I don't know why you insist on over complicating everything smile

Try this:

set time=%time: =0%
set _hour=%time:~0,2%
set _min=%time:~3,2%
set _sec=%time:~6,2%
set _tic=%time:~9,2%
set time=

%time% had been available since WinNT4 and is present in each successive NT based OS, AFAIK.

Last edited by bluesxman (02 Apr 2007 12:48)


cmd | *sh | ruby | chef

Offline

#8 02 Apr 2007 14:12

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Getting current time

Or you could do this:

for /F "tokens=1-4 delims=:. " %%g in ("%time%") do (
          set _hour=%%g&set _min=%%h&set _sec=%%i&set _tic=%%j)

Offline

#9 03 Apr 2007 10:58

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Getting current time

True, but not much good if some bright spark has decided to use a different time separator in their regional settings.  Of course we'd both start getting into serious trouble if the same bright spark chose more than 1 character as the time separator, or started messing with the ordering and layout.  That sort of action would send us delving into the registry to find out what the hell's going on.  But I guess that all depends on how robust you want your script to be.


cmd | *sh | ruby | chef

Offline

#10 04 Apr 2007 03:05

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Getting current time

Now you're getting over complicated.

The only format that is usually changed in all the regions is the date, not the time. But even if you change the time format in the registry, the command line still prints the time in HH MM SS TT.

Here's one of the MANY solutions to the problem if the user chooses to have a different time separator. Since REG is different on every version of NT, this should also work for any version of the application:

for /F "tokens=1-3*" %%g in ('reg query "HKEY_CURRENT_USER\Control Panel\International"^| find "sTime"') do (
    if /i "%%g"=="sTime" set _sTime=%%i)

if NOT DEFINED _sTime set _sTime=:

for /F "tokens=* delims=%_sTime%" %%g in ("%time%") do for %%h in (%%g) do (
          call set _timeStamp=%%_timeStamp%%:%%h)

for /F "tokens=1-3 delims=:" %%g in ("%_timeStamp%") do (
          set _hour=%%g&set _min=%%h&set _sec=%%i)

if NOT "%_sec:~3,1%"=="" set _tic=%_sec:~3,2%&set _sec=%_sec:~0,2%

Last edited by //[T.0.P]// (04 Apr 2007 10:43)

Offline

Board footer

Powered by