Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
postgres-interval
Advanced tools
Package description
The postgres-interval npm package is designed to parse and handle PostgreSQL interval types in JavaScript. It allows for the parsing of interval output from PostgreSQL into a JavaScript object, manipulation of these interval objects, and formatting of interval objects back into strings that PostgreSQL can understand. This is particularly useful when working with time-based data that needs to be manipulated or displayed in a JavaScript application.
Parsing PostgreSQL interval output
This feature allows for the parsing of a PostgreSQL interval string into a JavaScript object, making it easier to work with in a JavaScript codebase. The object contains properties for years, months, days, hours, minutes, and seconds.
const parseInterval = require('postgres-interval');
const result = parseInterval('1 year 2 months 3 days 04:05:06');
console.log(result);
Manipulating interval objects
After parsing an interval string into an object, this feature allows for the manipulation of the interval's properties (e.g., adding an extra day). The manipulated interval can then be converted back into a PostgreSQL interval string.
const parseInterval = require('postgres-interval');
const interval = parseInterval('1 day');
interval.days += 1;
console.log(interval.toPostgres());
Formatting intervals for PostgreSQL
This feature is useful for converting a manipulated interval object back into a string format that PostgreSQL can understand, enabling the updated interval to be used in database queries or operations.
const parseInterval = require('postgres-interval');
const interval = parseInterval('10 hours');
const formattedInterval = interval.toPostgres();
console.log(formattedInterval);
Moment.js is a comprehensive date handling library that can parse, validate, manipulate, and display dates and times in JavaScript. While it does not specifically target PostgreSQL interval types, it offers broad functionality for handling time-based data. Compared to postgres-interval, Moment.js is more versatile for general date and time manipulation but lacks the direct focus on PostgreSQL intervals.
Luxon is a powerful library for working with dates and times in JavaScript, offering a range of features similar to Moment.js, including parsing, formatting, and manipulating dates and times. Like Moment.js, Luxon does not specifically address PostgreSQL interval types but provides a rich set of tools for time-based data manipulation. It is a modern alternative to Moment.js with a focus on immutability and internationalization.
date-fns is a modular date utility library for JavaScript, offering over 200 functions for date manipulation, including parsing, formatting, and arithmetic operations. While it does not provide specific support for PostgreSQL interval types, its modular nature allows for lightweight inclusion of only the necessary functions. Compared to postgres-interval, date-fns offers a broader scope of date manipulation features without the direct focus on PostgreSQL intervals.
Readme
Parse Postgres interval columns
npm install --save postgres-interval
var parse = require('postgres-interval')
var interval = parse('01:02:03')
//=> {hours: 1, minutes: 2, seconds: 3}
interval.toPostgres()
// 3 seconds 2 minutes 1 hours
interval.toISOString()
// P0Y0M0DT1H2M3S
This package parses the default Postgres interval style. If you have changed intervalstyle
, you will need to set it back to the default:
set intervalstyle to default;
parse(pgInterval)
-> interval
Required
Type: string
A Postgres interval string.
interval.toPostgres()
-> string
Returns an interval string. This allows the interval object to be passed into prepared statements.
interval.toISOString()
-> string
Returns an ISO 8601 compliant string, for example P0Y0M0DT0H9M0S
.
Also available as interval.toISO()
for backwards compatibility.
interval.toISOStringShort()
-> string
Returns an ISO 8601 compliant string shortened to minimum length, for example PT9M
.
MIT © Ben Drucker
FAQs
Parse Postgres interval columns
We found that postgres-interval demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).