Skip to content

Commit

Permalink
Merge pull request #736 from atjn/clock-locale
Browse files Browse the repository at this point in the history
Use `locale` in clock tutorial
  • Loading branch information
gfwilliams authored Sep 11, 2024
2 parents 2dcf480 + ac8df6a commit 1934e9d
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions tutorials/Bangle.js Clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Copy the following code to the right of the IDE and click Upload ![](data:image/
function draw() {
// work out how to display the current time
var d = new Date();
var h = d.getHours(), m = d.getMinutes();
var time = h + ":" + m.toString().padStart(2,0);
var clock = require("locale").time(d);
var meridian = require("locale").meridian(d);
var time = clock + " " + meridian;

// Reset the state of the graphics library
g.reset();
Expand All @@ -44,7 +45,7 @@ var secondInterval = setInterval(draw, 1000);

You'll now have some tiny text in the middle of the screen which displays the time.

The `m.toString().padStart(2,0)` code zero-pads the minutes for us (so 1 minute past 12 gets written as `"12:01"` rather than `"12:1"`).
The `locale` module will print the clock in different formats depending on each user's language and area.

**Why is the code formatted like this?** Check out the [Code Style](/Code+Style)
page for some tips and the reasoning behind it.
Expand All @@ -69,17 +70,21 @@ const X = g.getWidth()/2 + 45,
function draw() {
// work out how to display the current time
var d = new Date();
var h = d.getHours(), m = d.getMinutes();
var time = (" "+h).substr(-2) + ":" + m.toString().padStart(2,0);
var clock = require("locale").time(d, 1 /*omit seconds*/);
var seconds = d.getSeconds().toString().padStart(2,0);
var meridian = require("locale").meridian(d);
// Reset the state of the graphics library
g.reset();
// draw the current time (4x size 7 segment)
g.setFont("7x11Numeric7Seg",4);
g.setFontAlign(1,1); // align bottom right
g.drawString(time, X, Y, true /*clear background*/);
// draw the seconds (2x size 7 segment)
g.setFont("7x11Numeric7Seg",2);
g.drawString(("0"+d.getSeconds()).substr(-2), X+30, Y, true /*clear background*/);
g.setFont("7x11Numeric7Seg:4");
g.drawString(clock, X, Y, true /*clear background*/);
// draw the meridian(am/pm) and seconds (2x size 7 segment)
g.setFontAlign(-1,1); // align bottom left
g.setFont("6x8:2");
g.drawString(meridian, X+4, Y-26, true /*clear background*/);
g.setFont("7x11Numeric7Seg:2");
g.drawString(seconds, X+2, Y, true /*clear background*/);
}

// Clear the screen once, at startup
Expand All @@ -90,16 +95,15 @@ draw();
var secondInterval = setInterval(draw, 1000);
```

![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSAAAJJklEQVR4Xu3cUY8bVRBEYUfiIf//1+YBKUiEFYpDKHdmxumyP95Q371TPtVn7d0EPt1ut683/yCAQCWBTwSu7E1oBP4mQGCLgEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0BgO4BAMQECF5cnOgIEtgMIFBMgcHF5oiNAYDuAQDEBAheXJzoCBLYDCBQTIHBxeaIjQGA7gEAxAQIXlyc6AgS2AwgUEyBwcXmiI0B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AQSAQInQuYILCZA4MXliIZAIkDgRMgcgcUECLy4HNEQSAQInAiZI7CYAIEXlyMaAokAgRMhcwQWEyDw4nJEQyARIHAiZI7AYgIEXlyOaAgkAgROhMwRWEyAwIvLEQ2BROAvKAzkLi21dV8AAAAASUVORK5CYII=)
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALAAAACwAQAAAACHzNnzAAAAfklEQVR42u3UsQqAIBDG8WvpXtOpZ3C7WuoVelSnTJSbDOUgg+D7Dwq/QRFRQggh9N8kroOYk/k8eZ9GG8txdpmDsluWYSy7hd3c5zQpExmYGqzXUEaOgWws29ViPso2urGb1neZwxcsGRtsXTs1gKsnZWM9TsX47RBCCD10A8Mgq4tOgt5iAAAAAElFTkSuQmCC)

The `.toString().padStart(2,0)` code zero-pads the seconds for us (so 1 second gets written as `"01"` rather than `"1"`).

**Note:** To avoid clearing the whole area here we're using the 4th argument to `drawString`,
which clears the background (by default only the text itself is drawn). This only works because we know
our text will always be the same length!

Finally, let's add the date. For this, we can use the `locale` library
which means that the data will be in the correct language for each user.

Just add the following at the bottom of the `draw` function:
Finally, let's add the date. Just add the following at the bottom of the `draw` function:

```JS
// draw the date, in a normal font
Expand Down Expand Up @@ -195,17 +199,21 @@ const X = g.getWidth()/2 + 45,
function draw() {
// work out how to display the current time
var d = new Date();
var h = d.getHours(), m = d.getMinutes();
var time = (" "+h).substr(-2) + ":" + m.toString().padStart(2,0);
var clock = require("locale").time(d, 1 /*omit seconds*/);
var seconds = d.getSeconds().toString().padStart(2,0);
var meridian = require("locale").meridian(d);
// Reset the state of the graphics library
g.reset();
// draw the current time (4x size 7 segment)
g.setFont("7x11Numeric7Seg",4);
g.setFontAlign(1,1); // align bottom right
g.drawString(time, X, Y, true /*clear background*/);
// draw the seconds (2x size 7 segment)
g.setFont("7x11Numeric7Seg",2);
g.drawString(("0"+d.getSeconds()).substr(-2), X+30, Y, true /*clear background*/);
g.setFont("7x11Numeric7Seg:4");
g.drawString(clock, X, Y, true /*clear background*/);
// draw the meridian(am/pm) and seconds (2x size 7 segment)
g.setFontAlign(-1,1); // align bottom left
g.setFont("6x8:2");
g.drawString(meridian, X+4, Y-26, true /*clear background*/);
g.setFont("7x11Numeric7Seg:2");
g.drawString(seconds, X+2, Y, true /*clear background*/);
// draw the date, in a normal font
g.setFont("6x8");
g.setFontAlign(0,1); // align center bottom
Expand Down

0 comments on commit 1934e9d

Please sign in to comment.