Skip to content

Instantly share code, notes, and snippets.

@fvdm
Last active April 30, 2024 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvdm/e5897fd4271e03183d30f5f14805fdcd to your computer and use it in GitHub Desktop.
Save fvdm/e5897fd4271e03183d30f5f14805fdcd to your computer and use it in GitHub Desktop.
HomeyScript for simple date calculation within a flow. The date class is using the timezone configured in the Homey settings. The result is provided as card tags which can be used in cards that come after. Make sure to add delays to the cards that use these tags, because HomeyScripts are executed async.
/**
* Get current date and time as card tags
* Optional date offset calculation using Homey's timezone
* suffix: [y]ears, [m]onths, [d]ays, [h]ours, m[i]nutes, [s]econds, [m]illi[s]econds
*
* @example
* // two months ago
* '-2m'
*
* @example
* // 123 days ahead
* '+123d'
*
* @see {@link https://gist.github.com/fvdm/e5897fd4271e03183d30f5f14805fdcd}
*/
args[0] = args[0] || 0;
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
const str = `${args[0]}`.match( /^(?<offset>[+-]?\d+) ?(?<unit>[ymdhis]|ms)?$/ );
const offset = parseInt( str.groups.offset, 10 );
const unit = str.groups.unit || 'd';
const info = await Homey.system.getInfo();
const local = new Date().toLocaleString( 'en', { timeZone: info.timezone } );
const d = new Date( local );
if ( typeof offset === 'number' ) {
switch( unit ) {
case 'y': d.setFullYear( d.getFullYear() + offset ); break;
case 'm': d.setMonth( d.getMonth() + offset ); break;
case 'd': d.setDate( d.getDate() + offset ); break;
case 'h': d.setHours( d.getHours() + offset ); break;
case 'i': d.setMinutes( d.getMinutes() + offset ); break;
case 's': d.setSeconds( d.getSeconds() + offset ); break;
case 'ms': d.setMilliseconds( d.getMilliseconds() + offset ); break;
}
}
const firstSun = new Date( d.getFullYear(), 0, 0 );
const firstMon = new Date( d.getFullYear(), 0, 1 );
const firstThu = new Date( d.getFullYear(), 0, 4 );
const res = {
year: d.getFullYear(),
month: d.getMonth() + 1,
month3: months[d.getMonth()],
day: d.getDate(),
weekday: days[d.getDay()],
weekday2: days[d.getDay()].substring( 0, 2 ),
weekSun: Math.floor( ( ( ( d - firstSun ) / 86400000 ) + d.getDay() + 1 ) / 7 ),
weekMon: Math.floor( ( ( ( d - firstMon ) / 86400000 ) + d.getDay() + 1 ) / 7 ),
weekThu: Math.floor( ( ( ( d - firstThu ) / 86400000 ) + d.getDay() + 1 ) / 7 ),
hours: d.getHours(),
minutes: d.getMinutes(),
seconds: d.getSeconds(),
ms: d.getMilliseconds(),
ts: d.getTime() / 1000,
};
res.month2 = `${res.month}`.padStart( 2, '0' );
res.day2 = `${res.day}`.padStart( 2, '0' );
res.hours2 = `${res.hours}`.padStart( 2, '0' );
res.minutes2 = `${res.minutes}`.padStart( 2, '0' );
res.seconds2 = `${res.seconds}`.padStart( 2, '0' );
res.date = `${res.year}-${res.month2}-${res.day2}`;
res.time = `${res.hours2}:${res.minutes2}`;
// log( res );
let arr = [];
for ( key in res ) {
arr.push( tag( `datum_${key}`, res[key] ) );
}
await Promise.allSettled( arr );
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment