Cross-midnight time measurements

Any time the behavior of your code depends on the Timer function you should take into account the (more or less) remote possibility that your code is executed just before midnight. Take for example the following code:

Sub Pause(seconds as Single)    Dim initTime as Single    initTime = Timer     Do: Loop Until Timer >= initTime + secondsEnd Sub

If this code is executed at 23:59:59 (or even earlier, depending on the value of the seconds argument), the Do-Loop will never end, and the user will have to kill the program from the Task Manager. The worst facet of this problem is that the bug can manifest months or even years after you’ve installed the program, which would leave you clueless about its causes.

Here’s the fix for the above routine:

Sub Pause(seconds as Single)    Dim initTime As Single, currTime As String    initTime = Timer     Do        currTime = Timer    Loop Until currTime >= initTime + seconds Or (currTime < initTime And _        currTime > initTime + seconds - 86400)End Sub

If you don’t need the pause to be really precise, and you’re satisfied with an integer number of seconds, you can also use this simpler approach:

Sub Pause(seconds as Single)    Dim initTime as Date    initTime = Now    Do: Loop Until DateDiff("s", initTime, Now) >= secondsEnd Sub

An even simpler way to insert a pause that work across midnite is by means of the Sleep API function. See the link below to learn how to use this function.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.