[Batch] Clock tiny

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

[Batch] Clock tiny

Post by MigrationUser »

10 Nov 2013 09:43
carlos

Hello. This is a clock in batch for be used like a widget.

Image

clock_tiny.cmd

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion

::batch clock tiny
::code author: carlos
::last updated: 2013, 18 november: reduced cpu usage fixed
::created: 2013, october
::
::the clock automatically updates the display
::note: only open 1 instance of this script else it may fail
::for use 24 hour clock set /a "format=24"
::for use 12 hour clock set /a "format=12"
set /a "format=12"
::
mode con cols=23 lines=10
color 0a

Set "diga=ÛÛÛ Û ÛÛÛÛÛÛÛ ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"
Set "digb=Û Û Û   Û  ÛÛ ÛÛ  Û    ÛÛ ÛÛ Û"
Set "digc=Û Û Û ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ  ÛÛÛÛÛÛÛ"
Set "digd=Û Û Û Û    Û  Û  ÛÛ Û  ÛÛ Û  Û"
Set "dige=ÛÛÛ Û ÛÛÛÛÛÛ  ÛÛÛÛÛÛÛ  ÛÛÛÛÛÛÛ"
set "m=JanFebMarAprMayJunJulAugSepOctNovDec"
set "w=MonTueWedThuFriSatSun"

call :getdate.init
call :getdate

:clock
set "th1=%day%%month%%year%%hour%%minute%"
set "da=."
set "db=."
if 12 equ !format! (set "db=M"
set "da=A"
if 12 leq %hour% (set /a "hour-=12"
set "da=P"
)
if 0 equ !hour! set "hour=12"
)

set /a "h1=hour/10,h2=hour-10*h1"
set /a "m1=minute/10,m2=minute-10*m1"

for %%_ in (h1 h2 m1 m2) do (set /a "index=3*%%_"
for %%# in (!index!) do (
set "%%_a=!diga:~%%#,3!"
set "%%_b=!digb:~%%#,3!"
set "%%_c=!digc:~%%#,3!"
set "%%_d=!digd:~%%#,3!"
set "%%_e=!dige:~%%#,3!"
))

set /a "index=3*(month-1)"
Set "mfp=!m:~%index%,3!"
set /a "index=3*(weekday-1)"
Set "wdp=!w:~%index%,3!"
set "dfp=%day%"
if %dfp% lss 10 set "dfp= %dfp%"

cls
if 12 equ !format! (title %h1%%h2%:%m1%%m2% %da%%db%
) else title %h1%%h2%:%m1%%m2%
echo(
echo    %h1a% %h2a%   %m1a% %m2a%
echo    %h1b% %h2b%   %m1b% %m2b%
echo    %h1c% %h2c% %da% %m1c% %m2c%
echo    %h1d% %h2d% %db% %m1d% %m2d%
echo    %h1e% %h2e%   %m1e% %m2e%
echo(
echo    ÄÄÄÄÄÄÄÂÄÄÂÄÄÄÄÄÄ
echo      %wdp%  ³%dfp%³ %mfp%

:upd
call :getdate
set "th2=%day%%month%%year%%hour%%minute%"
if not "%th1%"=="%th2%" goto :clock
ping -l 0 -n 1 -w 500 1.0.0.0 >nul
goto :upd

:getdate.init
set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
set /a "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
(echo .set infheader=""
echo .set infsectionorder=""
echo .set inffooter="%%2"
for /l %%# in (1,1,4) do echo .set inffooter%%#=""
echo .set cabinet="off"
echo .set compress="off"
echo .set donotcopyfiles="on"
echo .set rptfilename="nul"
) >"%temp%\~foo.ddf"
goto :eof

:getdate
set "tf=%temp%\~%random%"
makecab /d inffilename="%tf%" /f "%temp%\~foo.ddf" >nul
for /f "usebackq tokens=1-7 delims=: " %%a in ("%tf%") do (
set /a "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
set /a "hour=1%%d-100,minute=1%%e-100,second=1%%f-100")
del "%tf%" >nul 2>&1
goto :eof
Last edited by carlos (18 Nov 2013 20:57)

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

#2 10 Nov 2013 15:04
foxidrive

It looks nice carlos.

My only quibble is that on my 8 core 3 GHz CPU it takes 5% CPU when running.

----------------------------
#3 12 Nov 2013 07:20
carlos
foxidrive wrote:

It looks nice carlos.

My only quibble is that on my 8 core 3 GHz CPU it takes 5% CPU when running.
I will think how reduce the cpu usage.

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

#4 12 Nov 2013 23:28
Aacini
foxidrive wrote:

It looks nice carlos.

My only quibble is that on my 8 core 3 GHz CPU it takes 5% CPU when running.
Did you seen my version?

My clock have a one-second ping delay that is used to blink the "dots" and go backwards the timer seconds, so it should use very little CPU...

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

#5 13 Nov 2013 00:24
foxidrive

Yes, Aacini - yours uses less than 0.5 % and mostly less than 0.2 % according to Process Explorer.

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

#6 18 Nov 2013 19:30

carlos


@Aacini: thanks for mentions the tip.
@Foxidrive: I updated the code for use less cpu.

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

#7 18 Nov 2013 20:05
carlos

Fixed a bug after post the update. The last updated have this stamp:

Code: Select all

::last updated: 2013, 18 november: reduced cpu usage fixed
note the end word: fixed.

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

#8 19 Nov 2013 06:26
foxidrive

That made a huge difference to CPU use. Now it's less than 0.2 %

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

#9 05 Feb 2014 23:09
SmartGenius

Muy buen codigo carlitos wink

No sabia que aun continuabas en estos lados aportando big_smile

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

#10 06 Feb 2014 01:21
carlos
SmartGenius wrote:

Muy buen codigo carlitos wink

No sabia que aun continuabas en estos lados aportando big_smile
Buena Smart, tanto tiempo.
Si aquí tratando de explotar al máximo batch.
Lo último que aporté hace poco fue a la creación de una nueva técnica para crear archivos binarios usando solo batch (no debug).
Acá: http://www.dostips.com/forum/viewtopic.php?f=3&t=5330

Last edited by carlos (06 Feb 2014 06:50)

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

#11 06 Feb 2014 05:00
SmartGenius


Si por ahi estuve leyendo el post completo (tanto aqui como en DosTips) viendo los avances que hacian conforme cada uno aportaba, y yo mismo fui pensando en la idea de generar archivos usando eso, pero veo que ya lo has aplicado, muy bueno brother.

Saludos.
Post Reply