Bar clock 0.02: Apply locale, 12-hour setting

Plus minor bar drawing improvement
master
Richard de Boer 2020-03-31 00:05:00 +02:00
parent 277132e2a4
commit 54346977c4
3 changed files with 46 additions and 17 deletions

View File

@ -879,8 +879,8 @@
{ "id": "barclock", { "id": "barclock",
"name": "Bar Clock", "name": "Bar Clock",
"icon": "clock-bar.png", "icon": "clock-bar.png",
"version":"0.01", "version":"0.02",
"description": "A simple 24h digital clock showing seconds as a bar", "description": "A simple digital clock showing seconds as a bar",
"tags": "clock", "tags": "clock",
"type":"clock", "type":"clock",
"allow_emulator":true, "allow_emulator":true,

View File

@ -1 +1,2 @@
0.01: Created Bar Clock 0.01: Created Bar Clock
0.02: Apply locale, 12-hour setting

View File

@ -1,10 +1,23 @@
/* jshint esversion: 6 */ /* jshint esversion: 6 */
/** /**
* A simple 24h digital clock showing seconds as a bar * A simple digital clock showing seconds as a bar
**/ **/
{ {
// Check settings for what type our clock should be
const is12Hour = (require('Storage').readJSON('setting.json', 1) || {})['12hour']
const locale = require('locale')
{ // add some more info to locale
let date = new Date()
date.setFullYear(1111)
date.setMonth(1, 3) // februari: months are zero-indexed
const localized = locale.date(date, true)
locale.dayFirst = /3.*2/.test(localized)
locale.hasMeridian = (locale.meridian(date) !== '')
}
const timeFont = '6x8' const timeFont = '6x8'
const timeFontSize = 8 // 'hh:mm' fits exactly const timeFontSize = (is12Hour && locale.hasMeridian) ? 6 : 8
const ampmFontSize = 2
const dateFont = 'Vector' const dateFont = 'Vector'
const dateFontSize = 20 const dateFontSize = 20
@ -18,35 +31,50 @@
const SECONDS_PER_MINUTE = 60 const SECONDS_PER_MINUTE = 60
function timeText(date) { function timeText(date) {
const d = date.toString().split(' ') if (!is12Hour) {
const time = d[4].substr(0, 5) return {time: locale.time(date, true), ampm: ''}
const t = time.split(':') }
const hours = t[0], const meridian = locale.meridian(date)
minutes = t[1] const hours = date.getHours()
return `${hours}:${minutes}` if (hours === 0) {
date.setHours(12)
} else if (hours > 12) {
date.setHours(hours - 12)
}
return {time: locale.time(date, true), ampm: meridian}
} }
function dateText(date) { function dateText(date) {
const d = date.toString().split(' ') const dayName = locale.dow(date, true),
const dayName = d[0], month = locale.month(date, true),
month = d[1], day = date.getDate()
day = d[2] return `${dayName} ` + (locale.dayFirst ? `${day} ${month}` : `${month} ${day}`)
return `${dayName} ${day} ${month}`
} }
function drawDateTime(date) { function drawDateTime(date) {
const timeTexts = timeText(date)
g.setFontAlign(0, 0) // centered g.setFontAlign(0, 0) // centered
g.setFont(timeFont, timeFontSize) g.setFont(timeFont, timeFontSize)
g.drawString(timeText(date), screenCenter, timeY, true) g.drawString(timeTexts.time, screenCenter, timeY, true)
if (timeTexts.ampm !== '') {
g.setFontAlign(1, -1)
g.setFont(timeFont, ampmFontSize)
g.drawString(timeTexts.ampm,
// at right edge of screen , aligned with time bottom
(screenSize - ampmFontSize * 2), (timeY + timeFontSize - ampmFontSize),
true)
}
g.setFontAlign(0, 0) // centered
g.setFont(dateFont, dateFontSize) g.setFont(dateFont, dateFontSize)
g.drawString(dateText(date), screenCenter, dateY, true) g.drawString(dateText(date), screenCenter, dateY, true)
} }
function drawBar(date) { function drawBar(date) {
const seconds = date.getSeconds() const seconds = date.getSeconds()
if (seconds === 0) return; // zero-size rect stills draws one line of pixels
const fraction = seconds / SECONDS_PER_MINUTE const fraction = seconds / SECONDS_PER_MINUTE
g.fillRect(0, barY, fraction * screenSize, barY + barThickness) g.fillRect(0, barY, fraction * screenSize, barY + barThickness)
} }