Skip to content

Commit

Permalink
var
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-blackson committed Aug 25, 2023
1 parent 557fdc6 commit 59cf77c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ The goal of the adapter is to provide you a possibility to run countdowns for fu

## Changelog

### 2.1.0 (2023-07-XX)
### 2.2.0 (2023-08-25)
* (jack-blackson) Added ability to maintain, adjust and delete countdowns in adapter settings
* (jack-blackson) Bugfix with incorrect spaces as first character in "in words long" and "in words short"
* (jack-blackson) Bugfix incorrect calculation totalYears
* (bagsik) added "totalJSON" object in each countdown

### 2.1.0 (2023-07-22)
* (jack-blackson) Ability to use the countdown "backwards" - e.g. for calculating age of a baby
* (jack-blackson) Adjustments for "in words" -> fixed year/years and adjusted which detail level is shown at which point of time

Expand Down
78 changes: 51 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,50 +459,65 @@ async function createCountdownData(CountName, CountDate){

//months
if (months != 0 || years != 0){

if (CountDowninWordsLong != ''){
CountDowninWordsLong += ' '
CountDowninWordsShort += ' '
}

if (months > 1){
CountDowninWordsLong += ' ' + months+ ' ' + translateObject.textMonths;
CountDowninWordsShort += ' ' + months+translateObject.textMonthsShort;
CountDowninWordsLong += months+ ' ' + translateObject.textMonths;
CountDowninWordsShort += months+translateObject.textMonthsShort;
}
else if (months == 1) {
CountDowninWordsLong += ' ' + months+ ' ' + translateObject.textMonth;
CountDowninWordsShort += ' ' + months+translateObject.textMonthsShort;
CountDowninWordsLong += months+ ' ' + translateObject.textMonth;
CountDowninWordsShort += months+translateObject.textMonthsShort;
}
}

//days
if (days != 0 || months != 0 || years != 0){

if (CountDowninWordsLong != ''){
CountDowninWordsLong += ' '
CountDowninWordsShort += ' '
}
if (days > 1){
CountDowninWordsLong += ' ' + days+ ' ' + translateObject.textDays;
CountDowninWordsShort += ' ' + days+translateObject.textDaysShort;
CountDowninWordsLong += days+ ' ' + translateObject.textDays;
CountDowninWordsShort += days+translateObject.textDaysShort;
}
else if (days == 1) {
CountDowninWordsLong += ' ' + days+ ' ' + translateObject.textDay;
CountDowninWordsShort += ' ' + days+translateObject.textDaysShort;
CountDowninWordsLong += days+ ' ' + translateObject.textDay;
CountDowninWordsShort += days+translateObject.textDaysShort;
}
}

//hours
if (hours != 0 && years == 0 && months == 0 && days == 0){
if (CountDowninWordsLong != ''){
CountDowninWordsLong += ' '
CountDowninWordsShort += ' '
}
if (hours > 1){
CountDowninWordsLong += ' ' + hours+ ' ' + translateObject.textHours;
CountDowninWordsShort += ' ' + hours+translateObject.textHoursShort;
CountDowninWordsLong += hours+ ' ' + translateObject.textHours;
CountDowninWordsShort += hours+translateObject.textHoursShort;
}
else if (hours == 1){
CountDowninWordsLong += ' ' + hours+' ' + translateObject.textHour;
CountDowninWordsShort += ' ' + hours+translateObject.textHoursShort;
CountDowninWordsLong += hours+' ' + translateObject.textHour;
CountDowninWordsShort += hours+translateObject.textHoursShort;
}
}

//minutes
if (minutes != 0 && years == 0 && months == 0 && days == 0){
CountDowninWordsShort += ' ' + minutes+translateObject.textMinutesShort;
if (CountDowninWordsLong != ''){
CountDowninWordsLong += ' '
CountDowninWordsShort += ' '
}
CountDowninWordsShort += minutes+translateObject.textMinutesShort;
if (minutes > 1){
CountDowninWordsLong += ' ' + minutes+ ' ' + translateObject.textMinutes;
CountDowninWordsLong += minutes+ ' ' + translateObject.textMinutes;
}
else {
CountDowninWordsLong += ' ' + minutes+' ' + translateObject.textMinute;
CountDowninWordsLong += minutes+' ' + translateObject.textMinute;
}
}

Expand All @@ -522,7 +537,9 @@ async function createCountdownData(CountName, CountDate){
totalWeeks = mydiff(Date(),newdate,"weeks");
totalMonths = mydiff(Date(),newdate,"months");
totalYears = mydiff(Date(),newdate,"years");
adapter.log.debug('TOTAL YEARS: ' + totalYears)
}

else{
// CountUp
totalDays = mydiff(newdate,Date(),"days");
Expand All @@ -544,14 +561,20 @@ async function createCountdownData(CountName, CountDate){
}

async function updateObjects(years,months,days,hours,minutes,CountDowninWordsShort,CountDowninWordsLong,totalDays,totalHours,totalWeeks,totalMonths, totalYears, repeatCycle,countUp){
let totalJson = {
'days': totalDays,
'hours': totalHours,
'weeks': totalWeeks,
'months': totalMonths,
'years': totalYears
}

var totalJsonData = [];
var tempTable = {}

tempTable[translateObject.textYears] = totalYears
tempTable[translateObject.textMonths] = totalMonths
tempTable[translateObject.textWeeks] = totalWeeks
tempTable[translateObject.textDays] = totalDays
tempTable[translateObject.textHours] = totalHours

totalJsonData.push(tempTable)

var totalJson = JSON.stringify(totalJsonData)
adapter.log.debug('TOTAL YEARS1: ' + totalYears)

const promises = await Promise.all([
adapter.setState({device: 'countdowns' , channel: storagename, state: 'years'}, {val: years, ack: true}),
adapter.setState({device: 'countdowns' , channel: storagename, state: 'months'}, {val: months, ack: true}),
Expand Down Expand Up @@ -1172,7 +1195,7 @@ async function createObjects(CountName){
adapter.createStateAsync('countdowns', CountName, 'totalJson', {
read: true,
write: true,
name: "Total as json",
name: "Totals as JSON",
type: "string",
def: 0,
role: 'json'
Expand Down Expand Up @@ -1213,8 +1236,9 @@ function mydiff(date1,date2,interval) {
var date2Moment = moment(new Date(date2))
var timediff = date2 - date1;
if (isNaN(timediff)) return NaN;

switch (interval) {
case "years": return date2.getFullYear() - date1.getFullYear();
case "years": return date2Moment.diff(date1Moment, 'years', false);
case "months": return date2Moment.diff(date1Moment, 'months', false);
case "weeks" : return Math.floor(timediff / week);
case "days" : return Math.floor(timediff / day);
Expand Down
3 changes: 3 additions & 0 deletions translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ module.exports = class A {
translateObject.textHours = textHours
translateObject.textHoursShort = textHoursShort

translateObject.textWeeks = textWeeks


translateObject.textMinute = textMinute
translateObject.textMinutes = textMinutes
translateObject.textMinutesShort = textMinutesShort
Expand Down

0 comments on commit 59cf77c

Please sign in to comment.