PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label timer. Show all posts
Showing posts with label timer. Show all posts

Thursday, August 11, 2022

[FIXED] How can I add 0 before a numeric variable in batch

 August 11, 2022     batch-file, clock, decimal, digits, timer     No comments   

Issue

I can't seem to find the answer anywhere, even tho its such a "basic" thing I would assume someone else had already done it.

Anyways, here goes nothing.

I have made a simple batch script as a timer (counting up) and its in the dd:hh:mm:ss format but only displays what is necessary. For example if days and hours = 0 its only mm:ss etc.

Now I have ran into a slight issue when going from 0-9...

I want it to say 00-01-02-03-04-05... But right now its saying 0-1-2-3-4-5...

Lets say I put the time in a loop, with a 1 second delay always adding one second. I made a numeric /a variable since i'm working with numbers. But I can't seem to figure out how to add a 0 before the number if its less than 10...

So far I've tried the following:

echo %s:""=0%

Nevermind, I figured out the solution while typing this... See my answer


Solution

This is my final code, and it works because I just apply padding at all times if there is room for it.

Simple solution to a simple problem.

@echo off
title Stream counter
color a
cd /d "%~dp0"
set /a x=3
set nl=^&echo.
goto :top
:retry
cls
if %x% LEQ 0 (
    exit()
    ) else (
    title OOPS!
    echo Oops! You either didn't press anything or waited too long,
    echo if you're still there you can try again in a few seconds.
    ping 127.0.0.1 -n 10 >nul
    )
:top
title Stream counter
cls
echo Attemps left^: %x%
echo Start counter from zero? (y/n)
choice /n /c YNC /t 10 /d C>nul
if %errorlevel%==1 (
    goto :yes
    ) else if %errorlevel%==2 (
    goto :no
        ) else if %errorlevel%==3 (
        set /a x-=1
        goto :retry
        )
:yes
cls
set /a d=0
set /a h=0
set /a m=0
set /a s=0
goto :start
:no
cls
echo where do you want your timer to start?
echo Start by entering day^(s^)
set /p d=""
echo Now enter hour^(s^)
set /p h=""
echo Then enter minute^(s^)
set /p m=""
echo And finally second^(s^)
set /p s=""

goto :start
:start
rem delete this if you want to keep the result (it will get overwritten anyways...)
del final.txt
rem Use the choice to ask if you want to continue, and use that as a "Press any key to stop", kind of thing.
:startloop
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
goto :startloop
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
goto :startloop
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
goto :startloop
) else (
goto :mainloop
)
:mainloop
cls
set ps=00%s%
set pm=00%m%
set ph=00%h%
set ps=%ps:~-2%
set pm=%pm:~-2%
set ph=%ph:~-2%
if %d% geq 1 (
    if %h% geq 1 set "output=%d% day^(s^) %ph%^:%pm%^:%ps%"
    if %h% equ 0 set "output=%d% day^(s^) %pm%^:%ps%"
) else if %h% geq 1 (
    set "output=%ph%^:%pm%^:%ps%"
) else (
    set "output=%pm%^:%ps%"
)
echo %output% > counter.txt
echo Current time^: %output% &echo.
echo Do you wish to abort? (Y/N)
choice /n /c yn /t 1 /d n >nul
if %errorlevel%==1 goto :end
set /a s+=1
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
)
goto :mainloop
:end
:: END ::
rem COPY TO RESULT.txt and del counter.txt
copy /V counter.txt final.txt & del counter.txt
exit()


Answered By - tsgsOFFICIAL
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, May 17, 2022

[FIXED] How to make timeit.Timer() work with output of itertools.starmap()

 May 17, 2022     partial, python-3.x, starmap, timeit, timer     No comments   

Issue

I am having a hard time wrapping my head around why i can make timeit.Timer() work with output from functools.partial() but not with output from itertools.starmap().

What I basically need is starmap(func, tuples) to have the same 'attributes' as partial(func, one_arg_only) but be more generally in the sense that I can actually pass into func multiple arguments at the same time.

What's the easiest workaround here ? I tried timeit.Timer(starmap(func,tuples)) and obviously get the notorious error:

ValueError: stmt is neither a string nor callable

I presume this is coz starmap's output is not callable. But how do I work around this ?


Solution

The itertools.starmap() function returns an itertools.starmap iterable type whereas functools.partial() returns a functools.partial callable type. timeit.Timer() expects the first argument to be a callable (or a string it can exec()).

>>> type(itertools.starmap(lambda *args: list(args), [(1, 2)])
itertools.starmap
>>> type(functools.partial(lambda x: x+1, 1))
functools.partial

What you want to do is create a callable that will exhaust the iterable returned by itertools.starmap. One way to do this would be to call the list() function on the output of the starmap:

# This is the function to call
>>> example_func = lambda *args: len(args)

# This is the iterable that can call the example_func
>>> func_callers = itertools.starmap(example_func, [(1, 2)])

# This is the callable that actually processes func_callers
>>> execute_callers = lambda: list(func_callers)

# And finally using the timer
>>> timeit.Timer(execute_callers)


Answered By - damon
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, January 7, 2022

[FIXED] Javasacript Countdown timer in Days, Hours, Minute, Seconds

 January 07, 2022     countdowntimer, javascript, php, timer     No comments   

Issue

I'm trying to create a time-based count down clock. It is not based upon current_dates. The initial time that will be pulled will be from a separate php file. This will be for a browser based-game. When someone clicks the button to initiate this script. it will check if certain requirements are met and if so then this script will initiate. Based upon the level of the object it will pull the initial timer for that proceeding level. Hope that makes sense. Anyhow I based the timer script off of the first code I provide.

This script only accounts for minutes and seconds. I modified it to include days and hours as well. Somewhere in the process I have messed up and the script doesn't even work at all. I'm also not quite sure if this would be the best method to calculate this. So, if you have a cleaner method at doing this please share. Thank you in advance.

This script is based off of a minutes / seconds script I saw. Here's the original source:

<span id="countdown" class="timer"></span>
<script>
   var seconds = 60;
   function secondPassed() {
   var minutes = Math.round((seconds - 30)/60);
   var remainingSeconds = seconds % 60;
   if (remainingSeconds < 10) {
      remainingSeconds = "0" + remainingSeconds; 
   }
   document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
   if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "Buzz Buzz";
   } else {
    seconds--;
   }
   }
   var countdownTimer = setInterval('secondPassed()', 1000);
</script>

Here is the modified script that I am trying to include days, hours, minutes and seconds.

<span id="countdown"></span>
<script>
     var current_level = 93578;

     function timer() {

        var days = Math.round(current_level/86400);
        var remainingDays = Math.round(current_level - (days * 86400));

        if (days <= 0){
             days = current_level;
        }

        var hours = Math.round(remainingDays/3600);
        var remainingHours = Math.round(remainingDays - (hours * 3600));

        if (hours >= 24){
             hours = 23;
        }

        var minutes = Math.round(remainingHours/60);
        var remainingMinutes = Math.round(remainingHours - (minutes * 60));

        if (minutes >= 60) {
             minutes = 59;
        }

        var seconds = Math.round(remainingMinutes/60);

        document.getElementById('countdown').innerHTML = days + ":" + hours ":" + minutes + ":" + seconds;

        if (seconds == 0) {
             clearInterval(countdownTimer);
             document.getElementById('countdown').innerHTML = "Completed";
        }
     }
     var countdownTimer = setInterval('timer()', 1000);
</script>

Solution

I finally got back to looking at this and re-wrote the code and this works like a charm.

var upgradeTime = 172801;
var seconds = upgradeTime;
function timer() {
  var days        = Math.floor(seconds/24/60/60);
  var hoursLeft   = Math.floor((seconds) - (days*86400));
  var hours       = Math.floor(hoursLeft/3600);
  var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
  var minutes     = Math.floor(minutesLeft/60);
  var remainingSeconds = seconds % 60;
  function pad(n) {
    return (n < 10 ? "0" + n : n);
  }
  document.getElementById('countdown').innerHTML = pad(days) + ":" + pad(hours) + ":" + pad(minutes) + ":" + pad(remainingSeconds);
  if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "Completed";
  } else {
    seconds--;
  }
}
var countdownTimer = setInterval('timer()', 1000);
<span id="countdown" class="timer"></span>



Answered By - Phoenix Ryan
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing