Skip to content

Commit

Permalink
update esm/datetime.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ais-one committed Nov 13, 2024
1 parent 34388b3 commit 0409853
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 67 deletions.
6 changes: 6 additions & 0 deletions libs/esm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
### 0.0.28

- `t4t-fe.js` fixed major bug preventing listing of records

### 0.0.29
- `t4t-fe.js` update autocomplete path

### 0.0.30
- fix `datetime.js` funtion name and add function to get timezone in format that can be used by JS Date object
36 changes: 24 additions & 12 deletions libs/esm/datetime.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@

// returns an array or YYYY, MM, DD - not so useful...
export function dateStrAddDay(dateStr, days = 0) {
const d = new Date(Date.parse(dateStr) - new Date().getTimezoneOffset())
d.setDate(d.getDate() + days) // add the days
return [d.getFullYear().toString(), (d.getMonth() + 1).toString().padStart(2, 0), d.getDate().toString().padStart(2, 0)]
const d = new Date(Date.parse(dateStr) - new Date().getTimezoneOffset());
d.setDate(d.getDate() + days); // add the days
return [d.getFullYear().toString(), (d.getMonth() + 1).toString().padStart(2, 0), d.getDate().toString().padStart(2, 0)];
}

export const dateStrAddDayISO = (isoString, days = 0) => {
const d = new Date(isoString)
d.setDate(d.getDate() + days)
return d.toISOString()
const d = new Date(isoString);
d.setDate(d.getDate() + days);
return d.toISOString();
}

// Input: Date object or ISO datetime string
// Return Format: 2023-10-24 11:40:15 GMT+8
export const getLocaleDateTimeTzISO = (dt) => {
if (!(dt instanceof Date)) dt = new Date(dt)
if (!(dt instanceof Date)) dt = new Date(dt);
return dt.toLocaleString('sv', {
timeZoneName: 'short',
hour12: false,
Expand All @@ -25,12 +25,24 @@ export const getLocaleDateTimeTzISO = (dt) => {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
});
}

export const getLocaleDateISO = (isoString) => getLocaleDateTimeTzISO(isoString).substring(0, 10)
export const getLocaleTimeISO = (isoString) => getLocaleDateTimeTzISO(isoString).substring(11, 19)
export const getLocaleDateISO = (isoString) => getLocaleDateTimeTzISO(isoString).substring(0, 10);
export const getLocaleTimeISO = (isoString) => getLocaleDateTimeTzISO(isoString).substring(11, 19);

export const dateISO = (date) => new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().substring(0, 10)
export const timeeISO = (date) => new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().substring(11, 19)
export const dateISO = (date) => new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().substring(0, 10);
export const timeISO = (date) => new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().substring(11, 19);


/**
* Get timezone offset in ISO format (+hh:mm or -hh:mm)
* @param {Date|undefined} date - if undefined, will create a date object and use that timezone
* @returns {string}
*/
export const getTzOffsetISO = (date) => {
const pad = n => `${Math.floor(Math.abs(n))}`.padStart(2, '0'); // Pad a number to 2 digits
if (!date) date = new Date();
const tzOffset = -date.getTimezoneOffset();
return (tzOffset >= 0 ? '+' : '-') + pad(tzOffset / 60) + ':' + pad(tzOffset % 60);
};
2 changes: 1 addition & 1 deletion libs/esm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@es-labs/esm",
"version": "0.0.29",
"version": "0.0.30",
"description": "Reusable esm code",
"main": "index.js",
"author": "Aaron Gong",
Expand Down
6 changes: 6 additions & 0 deletions tools/dbdeploy/READMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

https://knexjs.org/guide/migrations.html#migration-cli

For migration:up command, each command will execute a single file starting with first.

So if you have 3 files (.js) in the `migrations` folder, you need to run it 3 times

For seed:run command, it will run all seed files (.js) in the `seeds` folder

## Why a seperate node package

when DB could be shared between packages
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
await knex.schema.createTable('audit_logs', (table) => {
table.increments('id').primary()
table.string('user')
table.datetime('timestamp')
table.string('db_name')
table.string('table_name')
table.string('op').comment('READ, UPDATE, DELETE, INSERT')
table.string('where_cols')
table.string('where_vals')
table.string('cols_changed')
table.text('prev_values')
table.text('new_values')
table.index(['timestamp', 'db_name', 'op'])
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('audit_logs')
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,6 @@ exports.up = async function(knex) {
table.string('telegramUsername')
// table.timestamps() // createdAt, updatedAt
})
await knex.schema.createTable('categories', (table) => {
table.increments('id').primary()
table.string('name').unique()
table.timestamps(true, true)
})
await knex.schema.createTable('books', (table) => {
table.increments('id').primary()
table.string('name').unique()
table.integer('rating')
table.string('yearPublished')
table.integer('categoryId').references('categories.id')
table.timestamps(true, true)
})
await knex.schema.createTable('pages', (table) => { // one book, many pages
table.increments('id').primary()
table.string('content')
table.integer('bookId').references('books.id')
table.timestamps(true, true)
})
await knex.schema.createTable('authors', (table) => {
table.increments('id').primary()
table.string('name')
table.string('avatar').defaultsTo('')
table.timestamps(true, true)
})
await knex.schema.createTable('books_authors', (table) => { // many books, many authors
table.integer('bookId').unsigned().references('books.id')
table.integer('authorId').unsigned().references('authors.id')
table.unique(['bookId', 'authorId']) // remove this and you will have duplicates
})
await knex.schema.createTable('country', (table) => {
table.increments('id').primary()
table.string('name')
Expand Down Expand Up @@ -120,37 +90,17 @@ exports.up = async function(knex) {
// { column: "time", order: "DESC" },
// ]); // TBD indexing
})

await knex.schema.createTable('audit_logs', (table) => {
table.increments('id').primary()
table.string('user')
table.datetime('timestamp')
table.string('db_name')
table.string('table_name')
table.string('op').comment('READ, UPDATE, DELETE, INSERT')
table.string('where_cols')
table.string('where_vals')
table.string('cols_changed')
table.text('prev_values')
table.text('new_values')
table.index(['timestamp', 'db_name', 'op'])
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('book_authors')
await knex.schema.dropTableIfExists('pages')
await knex.schema.dropTableIfExists('books')
await knex.schema.dropTableIfExists('authors')
await knex.schema.dropTableIfExists('categories')
await knex.schema.dropTableIfExists('country')
await knex.schema.dropTableIfExists('state')
await knex.schema.dropTableIfExists('student')
await knex.schema.dropTableIfExists('student_subject')
await knex.schema.dropTableIfExists('audit_logs')
await knex.schema.dropTableIfExists('student')
await knex.schema.dropTableIfExists('state')
await knex.schema.dropTableIfExists('country')
await knex.schema.dropTableIfExists('subject')
await knex.schema.dropTableIfExists('users')
}

0 comments on commit 0409853

Please sign in to comment.