Skip to content

Instantly share code, notes, and snippets.

@colrichie
Last active May 7, 2025 08:05
Show Gist options
  • Save colrichie/019fa2b75b9842d2c06cbfc51f4115e6 to your computer and use it in GitHub Desktop.
Save colrichie/019fa2b75b9842d2c06cbfc51f4115e6 to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////
//
// A Sample Program of waitill() for JavaScript
//
// You can make a more accurate interval loop by using the
// "Punctual.prototype.waitill()" method than by using the setTimeout()
// function simply.
//
// The following JavaScript program can make the 0.5 ms interval printing
// more accurate than typical sample programs.
//
// THIS SAMPLE IS DEDICATED TO PUNCTUAL PROGRAMMERS IN THE WORLD!
//
// Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2025-05-07
//
//////////////////////////////////////////////////////////////////////
class Punctual {
#wt_epoch;
constructor() {this.#wt_epoch = new Date(); }
now() {return new Date(); }
get_epoch() {return new Date(this.#wt_epoch.getTime());}
redef_epoch() {this.#wt_epoch = new Date();
return new Date(this.#wt_epoch.getTime());}
async waitill(sec_since_epoch) {
if (typeof(sec_since_epoch)!=='number' || isNaN(sec_since_epoch)) {
throw new Error('"sec_since_epoch" argument must be a valid number');
}
const dt = new Date() - this.#wt_epoch;
const msec = sec_since_epoch*1000 - dt;
if (msec <= 0 ) {return;}
await new Promise(res => setTimeout(res,msec));
return true;
}
}
(async () => {
const punc = new Punctual();
console.log('set the epoch');
for (let t=0.5; t<=5.0; t+=0.5) {
await punc.waitill(t);
console.log(t+' second(s) have past.');
}
})();
#!/bin/sh
exec php -q "$0" "$@"
<?php
######################################################################
#
# A Sample Program of waitill() for PHP
#
# You can make a more accurate interval loop by using the "Punctual::waitill()"
# function than by using the usleep() function simply.
#
# The following PHP script can make the 0.5 ms interval printing
# more accurate than typical sample programs.
#
# THIS SAMPLE IS DEDICATED TO PUNCTUAL PROGRAMMERS IN THE WORLD!
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2025-05-07
#
######################################################################
class Punctual
{
private float $wt_epoch;
public function __construct()
{
$this->wt_epoch = microtime(true);
}
public function now(): float
{
return microtime(true);
}
public function get_epoch(): float
{
return $this->wt_epoch;
}
public function redef_epoch(): float
{
$this->wt_epoch = microtime(true);
return $this->wt_epoch;
}
public function waitill(float $sec_to_ret): bool
{
$time_to_ret = $this->wt_epoch + $sec_to_ret;
$old_err_lv = error_reporting(E_ERROR | E_PARSE);
time_sleep_until($time_to_ret);
error_reporting($old_err_lv);
return true;
}
}
$punc = new Punctual();
echo "Set the epoch\n";
for ($t=0.5; $t<=5.0; $t+=0.5) {
$punc->waitill($t);
printf("%.1f second(s) have past.\n", $t);
}
?>
#!/usr/bin/env python3
# ####################################################################
#
# A Sample Program of waitill() for Python3
#
# You can make a more accurate interval loop by using the "Punctual.waitill()"
# function than by using the time.sleep() function simply.
#
# The following Python script can make the 0.5 ms interval printing
# more accurate than typical sample programs.
#
# THIS SAMPLE IS DEDICATED TO PUNCTUAL PROGRAMMERS IN THE WORLD!
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2025-05-07
#
# ####################################################################
import datetime
import time
class Punctual:
_wt_epoch = 0
def __init__(self):
self._wt_epoch = datetime.datetime.now()
return
def now(self):
return datetime.datetime.now()
def get_epoch(self):
return self._wt_epoch.replace(minute=0)
def redef_epoch(self):
self._wt_epoch = datetime.datetime.now()
return self._wt_epoch.replace(minute=0)
def waitill(self, sec_since_epoch):
try:
if not isinstance(sec_since_epoch, (int, float)):
raise TypeError(f'"sec_since_epoch" must be int or float')
dt = datetime.datetime.now() - self._wt_epoch
sec = sec_since_epoch - dt.total_seconds()
if sec > 0:
time.sleep(sec)
return True
except (ValueError, TypeError):
return False
punc = Punctual()
print("set the epoch")
for i in range(10):
t = (i+1) * 0.5
punc.waitill(t)
print("{:03.1f} second(s) have past.".format(t))
#!/usr/bin/env ruby
######################################################################
#
# A Sample Program of waitill() for Ruby
#
# You can make a more accurate interval loop by using the "Punctual.waitill()"
# function than by using the time.sleep() function simply.
#
# The following ruby script can make the 0.5 ms interval printing
# more accurate than typical sample programs.
#
# THIS SAMPLE IS DEDICATED TO PUNCTUAL PROGRAMMERS IN THE WORLD!
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2025-05-07
#
######################################################################
class Punctual
def initialize
@wt_epoch = Time.now
end
def now
Time.now
end
def get_epoch
@wt_epoch.dup
end
def redef_epoch
@wt_epoch = Time.now
@wt_epoch.dup
end
def waitill(sec_to_ret)
dt = Time.now - @wt_epoch
sec = sec_to_ret - dt
sleep(sec) if sec > 0
true
end
end
punc = Punctual.new
puts "Set the epoch"
0.5.step(5.0, 0.5) do |t|
punc.waitill(t)
printf("%.1f second(s) have past.\n",t)
end
//////////////////////////////////////////////////////////////////////
//
// A Sample Program of waitill() for Arduino
//
// You can make a more accurate loop by using the "waitill()" function
// than by using the delay() function simply.
//
// The following Arduino sketch can make the 0.5 ms LED blinking more
// accurate than typical sample programs.
//
// THIS SAMPLE IS DEDICATED TO PUNCTUAL PROGRAMMERS IN THE WORLD!
//
// Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2025-05-07
//
//////////////////////////////////////////////////////////////////////
bool waitill(unsigned long millisec_to_ret);
unsigned long WT_EPOCH; // To set the reference time (epoch time)
unsigned long t; // To set the milliseconds after the WT_EPOCH
// (It is for the argment for waitill())
void setup() {
pinMode(13,OUTPUT); // LED pin
WT_EPOCH = millis();
t = 0;
}
void loop() {
digitalWrite(13,HIGH);
t += 500; // Set the next end time for the waitill(),
waitill(t); // then call it.
digitalWrite(13,LOW );
t += 500; // Set the next end time for the waitill(),
waitill(t); // then call it.
}
bool waitill(unsigned long millisec_to_ret) {
unsigned long dt;
dt = millis() - WT_EPOCH;
if (millisec_to_ret > dt) {delay(millisec_to_ret-dt);}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment