Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faker.date.future should include a min parameter #2397

Open
tecsobs opened this issue Sep 16, 2023 · 10 comments
Open

faker.date.future should include a min parameter #2397

tecsobs opened this issue Sep 16, 2023 · 10 comments
Labels
c: feature Request for new feature m: date Something is referring to the date module p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Milestone

Comments

@tecsobs
Copy link

tecsobs commented Sep 16, 2023

Clear and concise description of the problem

Right now there is no way to fake a date that is at least 5 years in the future for example.

The param years works as a range between 1 and the value specified.

Suggested solution

Create a new min param, optional, that allow to create a new date in the future with at least "min" years

Alternative

No response

Additional context

No response

@tecsobs tecsobs added c: feature Request for new feature s: pending triage Pending Triage s: waiting for user interest Waiting for more users interested in this feature labels Sep 16, 2023
@github-actions
Copy link
Contributor

Thank you for your feature proposal.

We marked it as "waiting for user interest" for now to gather some feedback from our community:

  • If you would like to see this feature be implemented, please react to the description with an up-vote (:+1:).
  • If you have a suggestion or want to point out some special cases that need to be considered, please leave a comment, so we are aware about them.

We would also like to hear about other community members' use cases for the feature to give us a better understanding of their potential implicit or explicit requirements.

We will start the implementation based on:

  • the number of votes (:+1:) and comments
  • the relevance for the ecosystem
  • availability of alternatives and workarounds
  • and the complexity of the requested feature

We do this because:

  • There are plenty of languages/countries out there and we would like to ensure that every method can cover all or almost all of them.
  • Every feature we add to faker has "costs" associated to it:
    • initial costs: design, implementation, reviews, documentation
    • running costs: awareness of the feature itself, more complex module structure, increased bundle size, more work during refactors

View more issues which are waiting for user interest

@Shinigami92
Copy link
Member

You should pass the refDate as now + 5 years. How that refDate is calculated is up to you and you can use e.g. packages like luxon for that.
But it is not part of Faker to handle the complexity of Date-math.

@matthewmayer
Copy link
Contributor

What about passing a refDate 5 years in the future?

@tecsobs
Copy link
Author

tecsobs commented Sep 16, 2023

What's the point of faker then?

Faker actually do some math for the future method, like:

this.between({ from: new Date(date.getTime() - 1000 * 60 * 60 * 24 * 365), to: new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365), })

` const fromMs = toDate(from, this.faker.defaultRefDate).getTime();
const toMs = toDate(to, this.faker.defaultRefDate).getTime();
const dateOffset = this.faker.number.int(toMs - fromMs);

return new Date(fromMs + dateOffset);`

@Shinigami92
Copy link
Member

What's the point of faker then?

The deterministic generation of data

@tecsobs
Copy link
Author

tecsobs commented Sep 16, 2023

as referenced before on my edit, faker have some calculations.

`const range = {
min: 1000,
max: years * 365 * 24 * 3600 * 1000,
};

let future = date.getTime();
future += this.faker.number.int(range); // some time from now to N years later, in milliseconds
date.setTime(future);`

The min value is set as constant, in milliseconds. My suggestion is offer a optional parameter for this, if not present use the constant

Add the min parameter only will not change the goal of faker, and it is not that a big deal either in my opinion

@ST-DDT
Copy link
Member

ST-DDT commented Sep 16, 2023

@tecsobs Could you please describe/draft the API you would expect from/would like to have for the future method?

@matthewmayer
Copy link
Contributor

Logically if this was added there would need to be equivalent paramters for recent(), past() , soon(), and future() as they are symmetrical.

@ST-DDT ST-DDT added this to the vFuture milestone Nov 19, 2023
@ST-DDT ST-DDT added p: 1-normal Nothing urgent m: date Something is referring to the date module and removed s: pending triage Pending Triage labels Nov 19, 2023
@xpol
Copy link

xpol commented Sep 12, 2024

The current date API is easy to get wrong.

E.g. faker.date.future with years options, we might think the result is a larger date than today.
But actually, it can be a result of today's time.

How about:

type DateOffsetOptions = {
    years?: number
    months?: number
    days?: number
    weeks?: number
    hours?: number
    ...
}

faker.date.after(options?: DateOffsetOptions): Date
faker.date.before(options?: DateOffsetOptions): Date

@ST-DDT
Copy link
Member

ST-DDT commented Sep 23, 2024

@xpol How about a relativeBetween method?

relativeBetween({ refDate?: Date, startOffset: { years?, months?, days?, hours?, minutes?, seconds?, millis? }, endOffset: {...})

where the offset values can be both positive or negative e.g.

relativeBetween({startOffset: { days: 2 }, endOffset: { years: 1, days: -1}) // Returns a Date that is 2-364 days into the future
relativeBetween({startOffset: { days: -2 }, endOffset: { days: 2 }) // Returns a Date that is up to 2 days in the past or future

(or maybe just relative or as an alternative signature for between?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature m: date Something is referring to the date module p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Projects
None yet
Development

No branches or pull requests

5 participants