using ISO8601 format instead of custom

This commit is contained in:
azivner 2017-12-10 15:31:43 -05:00
parent 021f02bd8c
commit cba457bd61
6 changed files with 24 additions and 23 deletions

View File

@ -9,7 +9,7 @@ CREATE TABLE `sync_mig` (
`sync_date` TEXT NOT NULL); `sync_date` TEXT NOT NULL);
INSERT INTO sync_mig (id, entity_name, entity_id, source_id, sync_date) INSERT INTO sync_mig (id, entity_name, entity_id, source_id, sync_date)
SELECT id, entity_name, entity_id, source_id, datetime(sync_date, 'unixepoch') || '.000' FROM sync; SELECT id, entity_name, entity_id, source_id, strftime('%Y-%m-%dT%H:%M:%S.000Z', sync_date, 'unixepoch') FROM sync;
DROP TABLE sync; DROP TABLE sync;
ALTER TABLE sync_mig RENAME TO sync; ALTER TABLE sync_mig RENAME TO sync;
@ -25,8 +25,8 @@ CREATE INDEX `IDX_sync_sync_date` ON `sync` (
-- Options -- Options
UPDATE options SET opt_value = datetime(opt_value, 'unixepoch') || '.000' WHERE opt_name IN ('last_backup_date'); UPDATE options SET opt_value = strftime('%Y-%m-%dT%H:%M:%S.000Z', opt_value, 'unixepoch') WHERE opt_name IN ('last_backup_date');
UPDATE options SET date_modified = datetime(date_modified, 'unixepoch') || '.000'; UPDATE options SET date_modified = strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch');
-- Event log -- Event log
@ -38,7 +38,7 @@ CREATE TABLE `event_log_mig` (
); );
INSERT INTO event_log_mig (id, note_id, comment, date_added) INSERT INTO event_log_mig (id, note_id, comment, date_added)
SELECT id, note_id, comment, datetime(date_added, 'unixepoch') || '.000' FROM event_log; SELECT id, note_id, comment, strftime('%Y-%m-%dT%H:%M:%S.000Z', date_added, 'unixepoch') FROM event_log;
DROP TABLE event_log; DROP TABLE event_log;
ALTER TABLE event_log_mig RENAME TO event_log; ALTER TABLE event_log_mig RENAME TO event_log;
@ -62,8 +62,8 @@ CREATE TABLE IF NOT EXISTS "notes_mig" (
INSERT INTO notes_mig (note_id, note_title, note_text, is_protected, is_deleted, date_created, date_modified) INSERT INTO notes_mig (note_id, note_title, note_text, is_protected, is_deleted, date_created, date_modified)
SELECT note_id, note_title, note_text, is_protected, is_deleted, SELECT note_id, note_title, note_text, is_protected, is_deleted,
datetime(date_created, 'unixepoch') || '.000', strftime('%Y-%m-%dT%H:%M:%S.000Z', date_created, 'unixepoch'),
datetime(date_modified, 'unixepoch') || '.000' strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch')
FROM notes; FROM notes;
DROP TABLE notes; DROP TABLE notes;
@ -87,8 +87,8 @@ CREATE TABLE IF NOT EXISTS "notes_history_mig" (
INSERT INTO notes_history_mig (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to) INSERT INTO notes_history_mig (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to)
SELECT note_history_id, note_id, note_title, note_text, is_protected, SELECT note_history_id, note_id, note_title, note_text, is_protected,
datetime(date_modified_from, 'unixepoch') || '.000', strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified_from, 'unixepoch'),
datetime(date_modified_to, 'unixepoch') || '.000' strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified_to, 'unixepoch')
FROM notes_history; FROM notes_history;
DROP TABLE notes_history; DROP TABLE notes_history;
@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS "notes_tree_mig" (
INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted, date_modified) INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted, date_modified)
SELECT note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted, SELECT note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted,
datetime(date_modified, 'unixepoch') || '.000' strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch')
FROM notes_tree; FROM notes_tree;
DROP TABLE notes_tree; DROP TABLE notes_tree;

View File

@ -18,7 +18,7 @@ const eventLog = (function() {
listEl.html(''); listEl.html('');
for (const event of result) { for (const event of result) {
const dateTime = formatDateTime(getDateFromTS(event.date_added)); const dateTime = formatDateTime(parseDate(event.date_added));
if (event.note_id) { if (event.note_id) {
const noteLink = link.createNoteLink(event.note_id).prop('outerHTML'); const noteLink = link.createNoteLink(event.note_id).prop('outerHTML');

View File

@ -27,7 +27,7 @@ const noteHistory = (function() {
historyItems = await server.get('notes-history/' + noteId); historyItems = await server.get('notes-history/' + noteId);
for (const item of historyItems) { for (const item of historyItems) {
const dateModified = getDateFromTS(item.date_modified_from); const dateModified = parseDate(item.date_modified_from);
$("#note-history-list").append($('<option>', { $("#note-history-list").append($('<option>', {
value: item.note_history_id, value: item.note_history_id,

View File

@ -24,7 +24,7 @@ const recentChanges = (function() {
const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl); const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);
for (const change of dayChanges) { for (const change of dayChanges) {
const formattedTime = formatTime(getDateFromTS(change.date_modified_to)); const formattedTime = formatTime(parseDate(change.date_modified_to));
const revLink = $("<a>", { const revLink = $("<a>", {
href: 'javascript:', href: 'javascript:',
@ -57,7 +57,7 @@ const recentChanges = (function() {
const dayCache = {}; const dayCache = {};
for (const row of result) { for (const row of result) {
let dateDay = getDateFromTS(row.date_modified_to); let dateDay = parseDate(row.date_modified_to);
dateDay.setHours(0); dateDay.setHours(0);
dateDay.setMinutes(0); dateDay.setMinutes(0);
dateDay.setSeconds(0); dateDay.setSeconds(0);

View File

@ -36,10 +36,13 @@ function throwError(message) {
throw new Error(message); throw new Error(message);
} }
function getDateFromTS(timestamp) { function parseDate(str) {
// Date accepts number of milliseconds since epoch so UTC timestamp works without any extra handling try {
// see https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date-with-javascript return new Date(Date.parse(str));
return new Date(timestamp * 1000); }
catch (e) {
throw new Error("Can't parse date from " + str + ": " + e.stack);
}
} }
function formatTime(date) { function formatTime(date) {

View File

@ -32,18 +32,16 @@ function nowDate() {
} }
function dateStr(date) { function dateStr(date) {
return date.toISOString().replace("T", " ").replace("Z", ""); return date.toISOString();
} }
/** /**
* @param str - needs to be in the "YYYY-MM-DD HH:MM:SS.sss" format as outputted by dateStr(). * @param str - needs to be in the ISO 8601 format "YYYY-MM-DDTHH:MM:SS.sssZ" format as outputted by dateStr().
* also is assumed to be GMT time, *not* local time * also is assumed to be GMT time (as indicated by the "Z" at the end), *not* local time
*/ */
function parseDate(str) { function parseDate(str) {
try { try {
const isoDate = str.replace(" ", "T") + "Z"; return new Date(Date.parse(str));
return new Date(Date.parse(isoDate));
} }
catch (e) { catch (e) {
throw new Error("Can't parse date from " + str + ": " + e.stack); throw new Error("Can't parse date from " + str + ": " + e.stack);