NASutils Knowledge BaseUnofficialPolski

BASIC · projectA big clock in yabasic

This program turns the SSH window into a large clock: hours, minutes and seconds in digits five lines tall, the date below them, a new picture every second. q ends it.

Programming on the NAS › BASIC in 30 lessons



         ╔═ NAS clock ════════════════════════════════════════════════╗
         ║                                                            ║
         ║                                                            ║
         ║   ██████  ██████      ██████  ██████      ██████  ██████   ║
         ║   ██  ██  ██  ██  ██      ██  ██  ██  ██  ██  ██  ██  ██   ║
         ║   ██  ██  ██████      ██████  ██  ██      ██  ██  ██  ██   ║
         ║   ██  ██      ██  ██      ██  ██  ██  ██  ██  ██  ██  ██   ║
         ║   ██████  ██████      ██████  ██████      ██████  ██████   ║
         ║                                                            ║
         ║                                                            ║
         ║                  Friday, 27 November 2026                  ║
         ║                                                            ║
         ║                                                            ║
         ╚════════════════════════════════════════════════════════════╝





           q quits
The clock at 09:30:00 on Friday, 27 November 2026.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/basic/clock.yab

clock.yab Download clock.yab

rem Big clock: the time in block digits and the date, new every second until you press q.
rem Run: yabasic clock.yab   With NOW="2026-11-27 09:30:00" it shows that moment and stays on it.
rem inkey$(1) waits for a key one second at most, so the loop also ticks once a second.

dim font$(5), wday$(7), mon$(12)
rem Each digit 0-9 is 3 dots wide and 5 dots tall; one dot is drawn as two block characters
font$(1) = "111010111111101111111111111111"
font$(2) = "101110001001101100100001101101"
font$(3) = "101010111111111111111001111111"
font$(4) = "101010100001001001101001101001"
font$(5) = "111111111111001111111001111111"
n = token("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", wday$(), ",")
n = token("January,February,March,April,May,June,July,August,September,October,November,December", mon$(), ",")
clear screen
send(e$("0") + chr$(27) + "[2J" + box$(10, 4, 62, 14, "NAS clock") + at$(12, 23) + e$("33") + "q quits" + e$("0"))
repeat
  now$ = peek$("env", "NOW")
  if now$ = "" then
    dim d$(6) : n = token(date$, d$(), "-") : t$ = time$
    now$ = d$(4) + "-" + right$("0" + d$(2), 2) + "-" + right$("0" + d$(3), 2) + " " + left$(t$, 2) + ":" + mid$(t$, 4, 2) + ":" + mid$(t$, 7, 2)
  endif
  show(mid$(now$, 12, 8))
  year = val(left$(now$, 4)) : month = val(mid$(now$, 6, 2)) : day = val(mid$(now$, 9, 2))
  when$ = wday$(weekday(year, month, day) + 1) + ", " + str$(day) + " " + mon$(month) + " " + str$(year)
  send(at$(12, 14) + rep$(" ", 58) + at$(41 - int(width(when$) / 2), 14) + e$("0") + when$)
  k$ = inkey$(1)
until k$ = "q"
send(e$("0") + chr$(27) + "[2J" + at$(1, 1))
exit

rem Draws "HH:MM:SS" in big digits: hours and minutes bright, seconds plain
sub show(t$)
  local s$, row, i, c$, d, bits$, j
  for row = 1 to 5
    s$ = s$ + at$(14, 6 + row)
    for i = 1 to 8
      c$ = mid$(t$, i, 1)
      if i < 6 then s$ = s$ + e$("1;33") else s$ = s$ + e$("0;33") : endif
      if c$ = ":" then
        if row = 2 or row = 4 then s$ = s$ + "██  " else s$ = s$ + "    " : endif
      else
        d = val(c$) : bits$ = mid$(font$(row), d * 3 + 1, 3)
        for j = 1 to 3
          if mid$(bits$, j, 1) = "1" then s$ = s$ + "██" else s$ = s$ + "  " : endif
        next j
        s$ = s$ + "  "
      endif
    next i
  next row
  send(s$ + e$("0"))
end sub

rem Day of the week, 0 = Sunday (Sakamoto's method)
sub weekday(y, m, d)
  local t$
  t$ = "032503514624"
  if m < 3 y = y - 1
  return mod(y + int(y / 4) - int(y / 100) + int(y / 400) + val(mid$(t$, m, 1)) + d, 7)
end sub

sub box$(x, y, w, h, title$)
  local s$, i
  s$ = e$("33") + at$(x, y) + "╔" + rep$("═", w - 2) + "╗"
  for i = 1 to h - 2 : s$ = s$ + at$(x, y + i) + "║" + rep$(" ", w - 2) + "║" : next i
  s$ = s$ + at$(x, y + h - 1) + "╚" + rep$("═", w - 2) + "╝"
  return s$ + at$(x + 2, y) + e$("1;33") + " " + title$ + " " + e$("0")
end sub

sub width(s$)
  local i, n, c
  for i = 1 to len(s$)
    c = asc(mid$(s$, i, 1))
    if c < 128 or c >= 192 n = n + 1
  next i
  return n
end sub

sub rep$(s$, n) : local r$, i : for i = 1 to n : r$ = r$ + s$ : next i : return r$ : end sub
sub e$(code$) : return chr$(27) + "[" + code$ + "m" : end sub
sub at$(x, y) : return chr$(27) + "[" + str$(y) + ";" + str$(x) + "H" : end sub
sub send(s$) : open #1, "/dev/tty", "w" : print #1 s$; : close #1 : end sub

What you learn

  • Five strings of 0 and 1 hold a whole font: each digit takes three characters in each string.
  • mid$(font$(row), d * 3 + 1, 3) cuts the dots of digit d out of one row of the font.
  • inkey$(1) waits one second at most for a key and returns "" without one, so the same line reads q and keeps the tick.
  • The program draws the frame once and then only the parts that change: the digits and the date line.

How it works

The frame and the hint "q quits" go out once before the loop. Each turn of the loop takes the time: NOW when it is set, otherwise date$ and time$ in the same form "YYYY-MM-DD HH:MM:SS". show draws the eight characters "HH:MM:SS".

For each of the five rows of the font, show goes through the eight characters. A colon gets a block in rows 2 and 4 and spaces in the others. A digit takes its three dots from the font string, and each dot becomes ██ or two spaces, because a character cell is about twice as tall as it is wide. Hours and minutes are bold amber, the seconds plain amber.

The date line is cleared with spaces and written again in the middle of the frame. width counts the letters, so a Polish date with ą or ś stands in the middle as well.

Notes to the lines

  • 6–11The font: in font$(1) the first three characters are the top row of 0, the next three the top row of 1, and so on up to 9.
  • 15The frame, its title and the hint are drawn once.
  • 17–21Without NOW the program builds the time from date$ and time$. With NOW the time stays the same, which the test needs for a fixed picture.
  • 2541 is the middle column of the frame; half the width of the text goes to the left of it.
  • 26One second without a key returns "", and the loop draws the next second.
  • 38The first five characters, "HH:MM", get bold amber, the rest plain amber.
  • 40The colon is one dot wide, with two spaces after it.
  • 42–45One digit: three dots of the row, each as ██ or two spaces, then a gap of two spaces.
  • 54–59Sakamoto's method gives the day of the week, 0 for Sunday.

Run it

yabasic clock.yab

The clock runs until you press q. With NOW="2026-11-27 09:30:00" yabasic clock.yab it shows that moment and does not move.

Try it yourself

Draw the dots with ▓▓ instead of ██ on lines 40 and 44 and compare the look. Add a line under the date with the uptime from /proc/uptime.

More projects in BASIC