Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:iamkun/dayjs into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
BePo65 committed Sep 20, 2023
2 parents 2e886c2 + 2254635 commit ef1e96e
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 41 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/R

<p align="center"><a href="https://day.js.org/" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
alt="Day.js" /></a></p>
<p align="center">Fast <b>2kB</b> alternative to Moment.js with the same modern API</p>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
Expand Down Expand Up @@ -41,7 +41,7 @@ dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:m

### Documentation

You can find for more details, API, and other docs on [day.js.org](https://day.js.org/) website.
You can find more details, API, and other docs on [day.js.org](https://day.js.org/) website.

### Installation

Expand Down Expand Up @@ -82,6 +82,7 @@ dayjs.locale('es') // use Spanish locale globally

dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in a specific instance
```

📚[Internationalization](https://day.js.org/docs/en/i18n/i18n)

### Plugin
Expand All @@ -100,7 +101,7 @@ dayjs().format('Q Do k kk X x') // more available formats

## Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.

[[Become a sponsor via Github](https://github.com/sponsors/iamkun/)] [[Become a sponsor via OpenCollective](https://opencollective.com/dayjs#sponsor)]

Expand Down
6 changes: 3 additions & 3 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
日本語 | [English](../../README.md) | [简体中文](../zh-cn/README.zh-CN.md) | [Português Brasileiro](../pt-br/README-pt-br.md) | [한국어](../ko/README-ko.md) | [Español (España)](../es-es/README-es-es.md) | [Русский](../ru/README-ru.md)| [עברית](./docs/he/README-he.md)
日本語 | [English](../../README.md) | [简体中文](../zh-cn/README.zh-CN.md) | [Português Brasileiro](../pt-br/README-pt-br.md) | [한국어](../ko/README-ko.md) | [Español (España)](../es-es/README-es-es.md) | [Русский](../ru/README-ru.md)| [עברית](../he/README-he.md)

<p align="center"><a href="https://day.js.org/" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
Expand Down Expand Up @@ -44,9 +44,9 @@ dayjs()

## はじめに

### Documentation
### ドキュメント

You can find for more details, API, and other docs on [day.js.org](https://day.js.org/) website.
詳細、API、その他のドキュメントについては、[day.js.org](https://day.js.org/) のウェブサイトを参照してください。

### インストール

Expand Down
12 changes: 6 additions & 6 deletions docs/ko/README-ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</a>
</p>

> Day.js는 Moment.js와 호환되는 대부분의 API를 사용하며, 최신 브라우저에서 날짜와 시간에 대한 구문 분석, 유효성 검사, 조작, 출력하는 경량 JavaScript 라이브러리입니다. Moment.js를 사용하고 있다면, Day.js는 껌입니다.
> Day.js는 대부분의 API가 Moment.js와 호환되며 최신 브라우저에서 날짜와 시간에 대한 구문 분석, 유효성 검사, 조작, 출력을 간편하게 처리하는 경량 JavaScript 라이브러리 입니다. Moment.js를 사용해본 경험이 있다면, Day.js도 쉽게 사용하실 수 있습니다.
```js
dayjs()
Expand All @@ -46,7 +46,7 @@ dayjs()

### 문서

더 많은 세부 사항과 API, 그리고 다른 문서를 [day.js.org](https://day.js.org/) 웹사이트에서 수 있습니다.
더 많은 세부 사항과 API 다른 문서는 [day.js.org](https://day.js.org/) 웹사이트에서 확인하실 수 있습니다.

### 설치

Expand All @@ -58,7 +58,7 @@ npm install dayjs --save

### API

Day.js API를 사용해서 날짜와 시간에 대한 구문 분석, 유효성 검사, 조작, 출력을 쉽게 할 수 있습니다.
Day.js API를 사용하여 날짜와 시간의 구문 분석, 검증, 조작, 출력을 간편하게 처리할 수 있습니다.

```javascript
dayjs('2018-08-08') // parse
Expand All @@ -78,9 +78,9 @@ dayjs().isBefore(dayjs()) // query

### I18n

Day.js는 국제화에 대해 많은 지원을 합니다.
Day.js는 국제화에 대한 많은 지원을 제공하고 있습니다.

그러나 그것을 사용하지 않는다면, 그 누구도 당신의 빌드에 포함되지 않습니다.
하지만 이 기능을 사용하지 않는 경우, 빌드에 포함되지 않습니다.

```javascript
import 'dayjs/locale/es' // load on demand
Expand All @@ -96,7 +96,7 @@ dayjs('2018-05-05')

### Plugin

플러그인은 기능을 확장하거나 새로운 기능을 추가하기 위해 Day.js에 추가할 수 있는 독립적인 모듈입니다.
플러그인은 Day.js의 기능을 확장하거나 새로운 기능을 도입하기 위한 독립적인 모듈입니다.

```javascript
import advancedFormat from 'dayjs/plugin/advancedFormat' // load on demand
Expand Down
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ let L = 'en' // global locale
const Ls = {} // global loaded locale
Ls[L] = en

const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define
const IS_DAYJS = '$isDayjsObject'

// eslint-disable-next-line no-use-before-define
const isDayjs = d => d instanceof Dayjs || !!(d && d[IS_DAYJS])

const parseLocale = (preset, object, isLocal) => {
let l
Expand Down Expand Up @@ -72,7 +75,7 @@ const parseDate = (cfg) => {
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
}
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
}
}

Expand All @@ -83,11 +86,12 @@ class Dayjs {
constructor(cfg) {
this.$L = parseLocale(cfg.locale, null, true)
this.parse(cfg) // for plugin
this.$x = this.$x || cfg.x || {}
this[IS_DAYJS] = true
}

parse(cfg) {
this.$d = parseDate(cfg)
this.$x = cfg.x || {}
this.init()
}

Expand Down
2 changes: 2 additions & 0 deletions src/locale/ar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dayjs from 'dayjs'

const months = 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_')

const symbolMap = {
1: '١',
2: '٢',
Expand Down Expand Up @@ -36,6 +37,7 @@ const locale = {
months,
monthsShort: months,
weekStart: 6,
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
relativeTime: {
future: 'بعد %s',
past: 'منذ %s',
Expand Down
8 changes: 4 additions & 4 deletions src/locale/fa.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import dayjs from 'dayjs'
const locale = {
name: 'fa',
weekdays: 'یک‌شنبه_دوشنبه_سه‌شنبه_چهارشنبه_پنج‌شنبه_جمعه_شنبه'.split('_'),
weekdaysShort: 'یک‌_دو_سه‌_چه_پن_جم_شن'.split('_'),
weekdaysShort: 'یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_جمعه_شنبه'.split('_'),
weekdaysMin: 'ی_د_س_چ_پ_ج_ش'.split('_'),
weekStart: 6,
months: 'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_'),
monthsShort: 'فرو_ارد_خرد_تیر_مرد_شهر_مهر_آبا_آذر_دی_بهم_اسف'.split('_'),
months: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'),
monthsShort: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'),
ordinal: n => n,
formats: {
LT: 'HH:mm',
Expand All @@ -20,7 +20,7 @@ const locale = {
},
relativeTime: {
future: 'در %s',
past: '%s قبل',
past: '%s پیش',
s: 'چند ثانیه',
m: 'یک دقیقه',
mm: '%d دقیقه',
Expand Down
2 changes: 1 addition & 1 deletion src/locale/ko.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const locale = {
weekdaysMin: '일_월_화_수_목_금_토'.split('_'),
months: '1월_2월_3월_4월_5월_6월_7월_8월_9월_10월_11월_12월'.split('_'),
monthsShort: '1월_2월_3월_4월_5월_6월_7월_8월_9월_10월_11월_12월'.split('_'),
ordinal: n => n,
ordinal: n => `${n}일`,
formats: {
LT: 'A h:mm',
LTS: 'A h:mm:ss',
Expand Down
91 changes: 78 additions & 13 deletions src/locale/sl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
// Slovenian [sl]
import dayjs from 'dayjs'

function dual(n) {
return (n % 100) == 2 // eslint-disable-line
}

function threeFour(n) {
return (n % 100) == 3 || (n % 100) == 4 // eslint-disable-line
}

/* eslint-disable */
function translate(number, withoutSuffix, key, isFuture) {
const result = `${number} `
switch (key) {
case 's': // a few seconds / in a few seconds / a few seconds ago
return (withoutSuffix || isFuture) ? 'nekaj sekund' : 'nekaj sekundami'
case 'm': // a minute / in a minute / a minute ago
return withoutSuffix ? 'ena minuta' : 'eno minuto'
case 'mm': // 9 minutes / in 9 minutes / 9 minutes ago
if (dual(number)) {
return result + ((withoutSuffix || isFuture) ? 'minuti' : 'minutama')
}
if (threeFour(number)) {
return result + ((withoutSuffix || isFuture) ? 'minute' : 'minutami')
}
return result + ((withoutSuffix || isFuture) ? 'minut' : 'minutami')
case 'h': // an hour / in an hour / an hour ago
return withoutSuffix ? 'ena ura' : (isFuture ? 'eno uro' : 'eno uro')
case 'hh': // 9 hours / in 9 hours / 9 hours ago
if (dual(number)) {
return result + ((withoutSuffix || isFuture) ? 'uri' : 'urama')
}
if (threeFour(number)) {
return result + ((withoutSuffix || isFuture) ? 'ure' : 'urami')
}
return result + ((withoutSuffix || isFuture) ? 'ur' : 'urami')
case 'd': // a day / in a day / a day ago
return (withoutSuffix || isFuture) ? 'en dan' : 'enim dnem'
case 'dd': // 9 days / in 9 days / 9 days ago
if (dual(number)) {
return result + ((withoutSuffix || isFuture) ? 'dneva' : 'dnevoma')
}
return result + ((withoutSuffix || isFuture) ? 'dni' : 'dnevi')
case 'M': // a month / in a month / a month ago
return (withoutSuffix || isFuture) ? 'en mesec' : 'enim mesecem'
case 'MM': // 9 months / in 9 months / 9 months ago
if (dual(number)) { // 2 minutes / in 2 minutes
return result + ((withoutSuffix || isFuture) ? 'meseca' : 'mesecema')
}
if (threeFour(number)) {
return result + ((withoutSuffix || isFuture) ? 'mesece' : 'meseci')
}
return result + ((withoutSuffix || isFuture) ? 'mesecev' : 'meseci')
case 'y': // a year / in a year / a year ago
return (withoutSuffix || isFuture) ? 'eno leto' : 'enim letom'
case 'yy': // 9 years / in 9 years / 9 years ago
if (dual(number)) { // 2 minutes / in 2 minutes
return result + ((withoutSuffix || isFuture) ? 'leti' : 'letoma')
}
if (threeFour(number)) {
return result + ((withoutSuffix || isFuture) ? 'leta' : 'leti')
}
return result + ((withoutSuffix || isFuture) ? 'let' : 'leti')
}
}

/* eslint-enable */
const locale = {
name: 'sl',
weekdays: 'nedelja_ponedeljek_torek_sreda_četrtek_petek_sobota'.split('_'),
Expand All @@ -16,26 +81,26 @@ const locale = {
L: 'DD.MM.YYYY',
LL: 'D. MMMM YYYY',
LLL: 'D. MMMM YYYY H:mm',
LLLL: 'dddd, D. MMMM YYYY H:mm'
LLLL: 'dddd, D. MMMM YYYY H:mm',
l: 'D. M. YYYY'
},
relativeTime: {
future: 'čez %s',
past: 'pred %s',
s: 'nekaj sekund',
m: 'minuta',
mm: '%d minut',
h: 'ura',
hh: '%d ur',
d: 'dan',
dd: '%d dni',
M: 'mesec',
MM: '%d mesecev',
y: 'leto',
yy: '%d let'
s: translate,
m: translate,
mm: translate,
h: translate,
hh: translate,
d: translate,
dd: translate,
M: translate,
MM: translate,
y: translate,
yy: translate
}
}

dayjs.locale(locale, null, true)

export default locale

2 changes: 1 addition & 1 deletion src/locale/uz-latn.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const locale = {
},
relativeTime: {
future: 'Yaqin %s ichida',
past: 'Bir necha %s oldin',
past: '%s oldin',
s: 'soniya',
m: 'bir daqiqa',
mm: '%d daqiqa',
Expand Down
2 changes: 1 addition & 1 deletion src/locale/uz.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const locale = {
},
relativeTime: {
future: 'Якин %s ичида',
past: 'Бир неча %s олдин',
past: '%s олдин',
s: 'фурсат',
m: 'бир дакика',
mm: '%d дакика',
Expand Down
5 changes: 3 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12

const durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/

Expand Down Expand Up @@ -140,6 +140,7 @@ class Duration {
let seconds = this.$d.seconds || 0
if (this.$d.milliseconds) {
seconds += this.$d.milliseconds / 1000
seconds = Math.round(seconds * 1000) / 1000
}

const S = getNumberUnitFormat(seconds, 'S')
Expand Down Expand Up @@ -198,7 +199,7 @@ class Duration {
} else {
base = this.$d[pUnit]
}
return base === 0 ? 0 : base // a === 0 will be true on both 0 and -0
return base || 0 // a === 0 will be true on both 0 and -0
}

add(input, unit, isSubtract) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default (o, c, d) => {
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
let ins = d(target).$set(MS, this.$ms)
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
if (keepLocalTime) {
const newOffset = ins.utcOffset()
Expand All @@ -120,7 +120,7 @@ export default (o, c, d) => {
return oldStartOf.call(this, units, startOf)
}

const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'))
const withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), { locale: this.$L })
const startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf)
return startOfWithoutTz.tz(this.$x.$timezone, true)
}
Expand Down
8 changes: 8 additions & 0 deletions test/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ it('supports instanceof dayjs', () => {
expect(dayjs() instanceof dayjs).toBeTruthy()
})

it('$isDayjsObject', () => {
const mockOtherVersionDayjsObj = {
$isDayjsObject: true
}
expect(dayjs.isDayjs(mockOtherVersionDayjsObj)).toBeTruthy()
})

it('does not break isDayjs', () => {
expect(dayjs.isDayjs(dayjs())).toBeTruthy()
expect(dayjs.isDayjs(new Date())).toBeFalsy()
})
10 changes: 10 additions & 0 deletions test/locale/ar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ it('RelativeTime: Time from X gets formatted', () => {
.toBe(t[2])
})
})

it('Format meridiem with locale function', () => {
for (let i = 0; i <= 23; i += 1) {
const hour = dayjs()
.startOf('day')
.add(i, 'hour')
const meridiem = i > 12 ? 'م' : 'ص'
expect(hour.locale('ar').format('A')).toBe(`${meridiem}`)
}
})
Loading

0 comments on commit ef1e96e

Please sign in to comment.