use local dates to create day notes, closes #337

This commit is contained in:
azivner 2019-01-15 23:46:01 +01:00
parent cce8c1b674
commit bc4cec69a5
3 changed files with 31 additions and 13 deletions

View file

@ -81,7 +81,7 @@ app.on('ready', async () => {
const dateNoteService = require('./src/services/date_notes'); const dateNoteService = require('./src/services/date_notes');
const dateUtils = require('./src/services/date_utils'); const dateUtils = require('./src/services/date_utils');
const parentNote = await dateNoteService.getDateNote(dateUtils.nowDate()); const parentNote = await dateNoteService.getDateNote(dateUtils.nowLocalDate());
// window may be hidden / not in focus // window may be hidden / not in focus
mainWindow.focus(); mainWindow.focus();

View file

@ -47,8 +47,8 @@ async function getRootCalendarNote() {
return rootNote; return rootNote;
} }
async function getYearNote(dateTimeStr, rootNote) { async function getYearNote(dateStr, rootNote) {
const yearStr = dateTimeStr.substr(0, 4); const yearStr = dateStr.substr(0, 4);
let yearNote = await attributeService.getNoteWithLabel(YEAR_LABEL, yearStr); let yearNote = await attributeService.getNoteWithLabel(YEAR_LABEL, yearStr);
@ -66,19 +66,19 @@ async function getYearNote(dateTimeStr, rootNote) {
return yearNote; return yearNote;
} }
async function getMonthNote(dateTimeStr, rootNote) { async function getMonthNote(dateStr, rootNote) {
const monthStr = dateTimeStr.substr(0, 7); const monthStr = dateStr.substr(0, 7);
const monthNumber = dateTimeStr.substr(5, 2); const monthNumber = dateStr.substr(5, 2);
let monthNote = await attributeService.getNoteWithLabel(MONTH_LABEL, monthStr); let monthNote = await attributeService.getNoteWithLabel(MONTH_LABEL, monthStr);
if (!monthNote) { if (!monthNote) {
const yearNote = await getYearNote(dateTimeStr, rootNote); const yearNote = await getYearNote(dateStr, rootNote);
monthNote = await getNoteStartingWith(yearNote.noteId, monthNumber); monthNote = await getNoteStartingWith(yearNote.noteId, monthNumber);
if (!monthNote) { if (!monthNote) {
const dateObj = dateUtils.parseDate(dateTimeStr); const dateObj = dateUtils.parseLocalDate(dateStr);
const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()]; const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()];
@ -92,21 +92,20 @@ async function getMonthNote(dateTimeStr, rootNote) {
return monthNote; return monthNote;
} }
async function getDateNote(dateTimeStr) { async function getDateNote(dateStr) {
const rootNote = await getRootCalendarNote(); const rootNote = await getRootCalendarNote();
const dateStr = dateTimeStr.substr(0, 10); const dayNumber = dateStr.substr(8, 2);
const dayNumber = dateTimeStr.substr(8, 2);
let dateNote = await attributeService.getNoteWithLabel(DATE_LABEL, dateStr); let dateNote = await attributeService.getNoteWithLabel(DATE_LABEL, dateStr);
if (!dateNote) { if (!dateNote) {
const monthNote = await getMonthNote(dateTimeStr, rootNote); const monthNote = await getMonthNote(dateStr, rootNote);
dateNote = await getNoteStartingWith(monthNote.noteId, dayNumber); dateNote = await getNoteStartingWith(monthNote.noteId, dayNumber);
if (!dateNote) { if (!dateNote) {
const dateObj = dateUtils.parseDate(dateTimeStr); const dateObj = dateUtils.parseLocalDate(dateStr);
const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()]; const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()];

View file

@ -2,6 +2,16 @@ function nowDate() {
return dateStr(new Date()); return dateStr(new Date());
} }
function nowLocalDate() {
const date = new Date();
return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate());
}
function pad(num) {
return num <= 9 ? `0${num}` : `${num}`;
}
function dateStr(date) { function dateStr(date) {
return date.toISOString(); return date.toISOString();
} }
@ -25,14 +35,23 @@ function parseDate(str) {
return parseDateTime(datePart + "T12:00:00.000Z"); return parseDateTime(datePart + "T12:00:00.000Z");
} }
function parseLocalDate(str) {
const datePart = str.substr(0, 10);
// not specifying the timezone and specifying the time means Date.parse() will use the local timezone
return parseDateTime(datePart + " 12:00:00.000");
}
function getDateTimeForFile() { function getDateTimeForFile() {
return new Date().toISOString().substr(0, 19).replace(/:/g, ''); return new Date().toISOString().substr(0, 19).replace(/:/g, '');
} }
module.exports = { module.exports = {
nowDate, nowDate,
nowLocalDate,
dateStr, dateStr,
parseDate, parseDate,
parseDateTime, parseDateTime,
parseLocalDate,
getDateTimeForFile getDateTimeForFile
}; };