Skip to content

Commit

Permalink
Add p+ tokens support to parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
ericreis committed Jun 6, 2019
1 parent 62779b6 commit ee7f62c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import toDate from '../toDate/index.js'
import subMilliseconds from '../subMilliseconds/index.js'
import defaultLocale from '../locale/en-US/index.js'
import parsers from './_lib/parsers/index.js'
import longFormatters from '../_lib/format/longFormatters/index.js'
import {
isProtectedWeekYearToken,
isProtectedDayOfYearToken,
Expand All @@ -26,6 +27,10 @@ var TIMEZONE_UNIT_PRIORITY = 10
// - . matches any single character unmatched by previous parts of the RegExps
var formattingTokensRegExp = /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g

// This RegExp catches symbols escaped by quotes, and also
// sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
var longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g

var escapedStringRegExp = /^'(.*?)'?$/
var doubleQuoteRegExp = /''/g

Expand Down Expand Up @@ -186,6 +191,16 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/
* | | | xxx | -08:00, +05:30, +00:00 | 2 |
* | | | xxxx | -0800, +0530, +0000, +123456 | |
* | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
* | Long localized date | ? | P | 05/29/1453 | 7 |
* | | | PP | May 29, 1453 | 7 |
* | | | PPP | May 29th, 1453 | 7 |
* | | | PPPP | Sunday, May 29th, 1453 | 2,5 |
* | Long localized time | ? | p | 12:00 AM | 7 |
* | | | pp | 12:00:00 AM | 7 |
* | Combination of date and time | ? | Pp | 05/29/1453, 12:00 AM | 7 |
* | | | PPpp | May 29, 1453, 12:00:00 AM | 7 |
* | | | PPPpp | May 29th, 1453 at ... | 7 |
* | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5 |
* Notes:
* 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
* are the same as "stand-alone" units, but are different in some languages.
Expand Down Expand Up @@ -240,6 +255,8 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/
* - `I`: ISO week of year
* - `R`: ISO week-numbering year
* - `o`: ordinal number modifier
* - `P`: long localized date
* - `p`: long localized time
*
* 6. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
* You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://git.io/fxCyr
Expand Down Expand Up @@ -397,7 +414,19 @@ export default function parse(

var i

var tokens = formatString.match(formattingTokensRegExp)
var tokens = formatString
.match(longFormattingTokensRegExp)
.map(function(substring) {
var firstCharacter = substring[0]
if (firstCharacter === 'p' || firstCharacter === 'P') {
var longFormatter = longFormatters[firstCharacter]
const a = longFormatter(substring, locale.formatLong, subFnOptions)
return a
}
return substring
})
.join('')
.match(formattingTokensRegExp)

for (i = 0; i < tokens.length; i++) {
var token = tokens[i]
Expand Down

0 comments on commit ee7f62c

Please sign in to comment.