Skip to content

Commit

Permalink
Merge pull request #12 from confetti/story/dayjs
Browse files Browse the repository at this point in the history
Story/rewrites using dayjs
  • Loading branch information
borkanee authored Feb 9, 2024
2 parents a6b6f6b + b99d322 commit c59a674
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 166 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
95 changes: 68 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,84 @@
'use strict';

var moment = require('moment-timezone');
var dayjs = require('dayjs');
require('dayjs/locale/da');
require('dayjs/locale/de');
require('dayjs/locale/nb');
require('dayjs/locale/sv');
require('dayjs/locale/fr');
require('dayjs/locale/ja');

var utc = require('dayjs/plugin/utc');
var timezone = require('dayjs/plugin/timezone');
var advancedFormat = require('dayjs/plugin/advancedFormat');
var customParseFormat = require('dayjs/plugin/customParseFormat');
var isSameOrBefore = require('dayjs/plugin/isSameOrBefore');

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(advancedFormat);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);

var abbreviations = {
'Central European Standard Time': 'CET',
'Central European Summer Time': 'CEST',
'Eastern European Standard Time': 'EET',
'Eastern European Summer Time': 'EEST',
'British Summer Time': 'BST',
'Greenwich Mean Time': 'GMT',
'Eastern Daylight Time': 'EDT',
'Eastern Standard Time': 'EST',
'Central Africa Time': 'CAT',
'East Africa Time': 'EAT',
'Pacific Daylight Time': 'PDT',
'Pacific Standard Time': 'PST',
'Central Daylight Time': 'CDT',
'Central Standard Time': 'CST',
'Australian Eastern Daylight Time': 'AEDT',
'Australian Eastern Standard Time': 'AEST'
};

var customTimeZoneAbbreviation = function customTimeZoneAbbreviation(_ref) {
var timeZone = _ref.timeZone,
date = _ref.date;

if (timeZone === 'GMT') return timeZone;

var timeZoneLong = new Intl.DateTimeFormat('en-US', { timeZoneName: 'long', timeZone: timeZone }).formatToParts(date).find(function (part) {
return part.type === 'timeZoneName';
}).value;

return abbreviations[timeZoneLong];
};

var sameMonth = function sameMonth(start, end) {
return end.month() === start.month();
};
var sameDayOrNight = function sameDayOrNight(start, end) {
return end.isSame(start, 'day') || end.diff(start, 'hours') < 24 && end.hours() < 8;
return end.date() === start.date() || end.diff(start, 'hour') < 24 && end.hour() < 8;
};

module.exports = function (_ref, format) {
var endDate = _ref.endDate,
startDate = _ref.startDate,
timeZone = _ref.timeZone,
timeFormat = _ref.timeFormat,
locale = _ref.locale,
showTimeZone = _ref.showTimeZone;
module.exports = function (_ref2, format) {
var endDate = _ref2.endDate,
startDate = _ref2.startDate,
timeZone = _ref2.timeZone,
timeFormat = _ref2.timeFormat,
locale = _ref2.locale,
showTimeZone = _ref2.showTimeZone;

if (!timeZone) {
timeZone = 'Europe/Stockholm';
}
if (!timeFormat) {
timeFormat = '24';
}
if (!locale) {
locale = 'en';
}
if (!format) {
format = {
month: 'MMM'
};
}
timeZone = timeZone || 'Europe/Stockholm';
timeFormat = timeFormat || '24';
locale = locale || 'en';
format = format || { month: 'MMM' };

var formatResult = void 0;

var start = moment(startDate).locale(locale).tz(timeZone);
var isCurrentYear = start.year() === moment().year();
var start = dayjs(startDate).locale(locale).tz(timeZone);
var isCurrentYear = start.year() === dayjs().year();

if (endDate) {
var end = moment(endDate).locale(locale).tz(timeZone);
var end = dayjs(endDate).locale(locale).tz(timeZone);

if (sameDayOrNight(start, end)) {
if (timeFormat && timeFormat === '12') {
Expand All @@ -62,7 +102,8 @@ module.exports = function (_ref, format) {
}

if (showTimeZone) {
formatResult += ' ' + moment.tz(startDate, timeZone).format('z');
var shortAbbreviation = customTimeZoneAbbreviation({ timeZone: timeZone, date: start });
formatResult += ' ' + (shortAbbreviation || dayjs(startDate).tz(timeZone).format('z'));
}

return formatResult;
Expand Down
49 changes: 13 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "pretty-dates",
"description": "A simple lib for formatting a time span pretty with moment.js",
"version": "0.0.13",
"version": "0.0.15",
"main": "index.js",
"dependencies": {
"moment": "^2.29.4",
"moment-timezone": "^0.5.42"
"dayjs": "^1.11.10"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down
78 changes: 58 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
const moment = require('moment-timezone')
const dayjs = require('dayjs')
require('dayjs/locale/da')
require('dayjs/locale/de')
require('dayjs/locale/nb')
require('dayjs/locale/sv')
require('dayjs/locale/fr')
require('dayjs/locale/ja')

const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const advancedFormat = require('dayjs/plugin/advancedFormat')
const customParseFormat = require('dayjs/plugin/customParseFormat')
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore')

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(advancedFormat)
dayjs.extend(customParseFormat)
dayjs.extend(isSameOrBefore)

const abbreviations = {
'Central European Standard Time': 'CET',
'Central European Summer Time': 'CEST',
'Eastern European Standard Time': 'EET',
'Eastern European Summer Time': 'EEST',
'British Summer Time': 'BST',
'Greenwich Mean Time': 'GMT',
'Eastern Daylight Time': 'EDT',
'Eastern Standard Time': 'EST',
'Central Africa Time': 'CAT',
'East Africa Time': 'EAT',
'Pacific Daylight Time': 'PDT',
'Pacific Standard Time': 'PST',
'Central Daylight Time': 'CDT',
'Central Standard Time': 'CST',
'Australian Eastern Daylight Time': 'AEDT',
'Australian Eastern Standard Time': 'AEST',
}

const customTimeZoneAbbreviation = ({ timeZone, date }) => {
if (timeZone === 'GMT') return timeZone

const timeZoneLong = new Intl.DateTimeFormat('en-US', { timeZoneName: 'long', timeZone })
.formatToParts(date)
.find((part) => part.type === 'timeZoneName').value

return abbreviations[timeZoneLong]
}

const sameMonth = (start, end) => end.month() === start.month()
const sameDayOrNight = (start, end) => end.isSame(start, 'day') || (end.diff(start, 'hours') < 24 && end.hours() < 8)
const sameDayOrNight = (start, end) => end.date() === start.date() || (end.diff(start, 'hour') < 24 && end.hour() < 8)

module.exports = function ({ endDate, startDate, timeZone, timeFormat, locale, showTimeZone }, format) {
if (!timeZone) {
timeZone = 'Europe/Stockholm'
}
if (!timeFormat) {
timeFormat = '24'
}
if (!locale) {
locale = 'en'
}
if (!format) {
format = {
month: 'MMM',
}
}
timeZone = timeZone || 'Europe/Stockholm'
timeFormat = timeFormat || '24'
locale = locale || 'en'
format = format || { month: 'MMM' }

let formatResult

const start = moment(startDate).locale(locale).tz(timeZone)
const isCurrentYear = start.year() === moment().year()
const start = dayjs(startDate).locale(locale).tz(timeZone)
const isCurrentYear = start.year() === dayjs().year()

if (endDate) {
const end = moment(endDate).locale(locale).tz(timeZone)
const end = dayjs(endDate).locale(locale).tz(timeZone)

if (sameDayOrNight(start, end)) {
if (timeFormat && timeFormat === '12') {
Expand Down Expand Up @@ -62,7 +99,8 @@ module.exports = function ({ endDate, startDate, timeZone, timeFormat, locale, s
}

if (showTimeZone) {
formatResult += ' ' + moment.tz(startDate, timeZone).format('z')
const shortAbbreviation = customTimeZoneAbbreviation({ timeZone, date: start })
formatResult += ' ' + (shortAbbreviation || dayjs(startDate).tz(timeZone).format('z'))
}

return formatResult
Expand Down
Loading

0 comments on commit c59a674

Please sign in to comment.