Giter Site home page Giter Site logo

amazon-date-parser's Introduction

Amazon Date parser

Software License Build Status

Given an AMAZON.DATE returns an object with the relative start and end date.

Installation

npm install amazon-date-parser

Basic usage

var AmazonDateParser = require('amazon-date-parser');

var date = new AmazonDateParser('2017-01-31');

/*
returns:
{
    startDate: Tue Jan 31 2017 00:00:00 GMT+0000 (GMT),
    endDate: Tue Jan 31 2017 23:59:59 GMT+0000 (GMT)
}
*/

It throws an error when the date is not valid or not supported.

var AmazonDateParser = require('amazon-date-parser');

try {
    var date = new AmazonDateParser('dummy');    
}catch(e) {
    console.log(e.message);
}

// Error: Invalid constructor parameter or parameter not supported.

Input values:

  • right now: PRESENT_REF
  • single day: 2017-11-24
  • week: 2017-W48
  • weekend: 2017-W48-WE
  • month: 2017-11 and 2019-11-XX
  • quarter: 2018-Q2
  • seasons: 2017-SP, 2017-WI, 2017-FA, 2017-SU (see below)
  • year: 2018 and 2020-XX
  • decade: 201X

Seasons

The constructor receives the following optional parameters only valid for season's calculations.

{
    hemisphere: 'N',    //`N` for North, `S` for the South hemisphere (default is `N`)
    seasons: { // see Custom seasons for more info about this parameter
        // ...
        'SU': { // summer
            startDate: [5, 1],  // 1st of June
            endDate: [8, 0]     // 31st of August
        }
        // ...
    }
}

The seasons parameters overrides all the others. For instance, if you provide seasons and hemisphere the latter will be ignored.

Default seasons

The north hemisphere seasons are by default defined as follow:

  • Spring, from the 1st of March to the 31st of May
  • Summer, from the 1st of June to the 31st of August
  • Fall/Autumn, from the 1st of September to the 30th of November
  • Winter, from the 1st of December to the end of February (28th or 29th depending on leap year)

For the south hemisphere you should construct the Amazon date parser as follow:
var date = new AmazonDateParser({hemisphere: 'S'});
and you can get:

  • Spring, from the 1st of September to the 30th of November
  • Summer, from the 1st of December to the end of February (28th or 29th depending on leap year)
  • Fall/Autumn, from the 1st of March to the 31st of May
  • Winter, from the 1st of June to the 31st of August

Custom seasons

You can pass your own seasons representation using the following configurations:

var options = {
    seasons: {
        'SP': {
            startDate: [2, 1],  // 1st of March
            endDate: [3, 0]     // end of March
        },
        'SU': {
            startDate: [5, 1],  // 1st of June
            endDate: [8, 0]     // end of August
        },
        'FA': {
            startDate: [8, 1],  // 1st of September
            endDate: [11, 0]    // end of November
        },
        'WI': {
            startDate: [11, 1], // 1st of December
            endDate: [2, 0]     // end of February (28th or 29th)
        }
}
var AmazonDateParser = require('amazon-date-parser');

var date = new AmazonDateParser('2018-SP', options);
/*
returns something like:
{
    startDate: Thu Mar 01 2018 00:00:00 GMT+0000 (GMT),
    endDate: Sat Mar 31 2018 23:59:59 GMT+0000 (GMT)
}
*/

Credits

License

The MIT License (MIT). Please see License File for more information.

amazon-date-parser's People

Contributors

dependabot[bot] avatar niklongstone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

matijagrcic

amazon-date-parser's Issues

Can't handle quarters

First off, great project!

Currently, it doesn't seem to handle quarters.. If I ask alexa about last quarter, it produces the value "2017-Q4". Unfortunately this crashes the parser:

Error: Invalid constructor parameter or parameter not supported.
    at new AmazonDateParser (/built_app/programs/server/npm/node_modules/amazon-date-parser/index.js:80:23)

Is this something you are considering adding in the future?

Issue with seasons

Hi i'm having some issues with seasons...passing 2019-su or 2019-wi into AmazonDateParser gives me....

2019-04-12T12:59:22.094Z 8723fdad-4204-4fde-8f3b-21eeb0064c06 Unexpected exception 'Error: Invalid constructor parameter or parameter not supported.': Error: Invalid constructor parameter or parameter not supported. at new AmazonDateParser (/var/task/node_modules/amazon-date-parser/index.js:88:23) at searchDatabase (/var/task/index.js:629:19) at Object.findExperienceIntentHandler (/var/task/index.js:370:23) at Object.FindExperienceIntent (/var/task/index.js:63:31) at emitNone (events.js:106:13) at AlexaRequestEmitter.emit (events.js:208:7) at AlexaRequestEmitter.EmitEvent (/var/task/node_modules/alexa-sdk/lib/alexa.js:216:10) at AlexaRequestEmitter.ValidateRequest (/var/task/node_modules/alexa-sdk/lib/alexa.js:181:23) at AlexaRequestEmitter.HandleLambdaEvent (/var/task/node_modules/alexa-sdk/lib/alexa.js:126:25) at AlexaRequestEmitter.value (/var/task/node_modules/alexa-sdk/lib/alexa.js:100:31)

UPDATE, i also have the same issue with 2019-w16

2018-XX-XX format not supported

https://developer.amazon.com/docs/custom-skills/slot-type-reference.html#date

Utterances that map to a year (such as "next year") convert to a date containing just the year. Note that the date format differs between English and other languages:
...
All other languages (French, German, Italian, Japanese, Portuguese, and Spanish) use the format YYYY-XX-XX. For example: 2018-XX-XX.

2018-XX-XX format is not supported by the library. This is the case if other language then English is used in the Alexa model.

Month parsing is returning dates from previous month

@niklongstone AMAZON.DATE slot gives this month as "2017-11". If I parse it using amazon-date-parser
I'm getting the object with dates from previous month.

{
endDate: Tue Oct 31 2017 19:59:59 GMT-0400 (EDT),
startDate: Sat Sep 30 2017 20:00:00 GMT-0400 (EDT)
}

I think the object should rather return the startDate as first day of the current month and endDate as either

  1. Date-time of now or
  2. Last day of current month

For now, I'm adding 1 month to both startDate & endDate when a month is parsed.

Please check and let me know if I'm missing anything.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.