-
Notifications
You must be signed in to change notification settings - Fork 23
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
[RFC] Updating the fields of a DateTime is unsafe #75
Comments
Why should Option 2 break code that uses the DateTime constructor? |
Because the fields of |
Ah ok, you're right |
You will be surprised but I will be in the opposite camp here. I have experience working with fancy dates rolling calculations and must say ability to set date edit: negative months and month days are not a problem either 2018-0-(-2) is equal to 2017-11-29 |
Why not just allow the construction of |
@cooldome Hopefully, rolling dates should be a task handled by the library, not by manually setting non-existent days and normalizing them. This is why we can do By the way, this would be ambguous, because - say - rolling days then hours can be different from rolling hours then days. Setting both a day and an hour past the natural bounds would result in something which cannot be uniquely normalized |
@cooldome The problem with this is that we need to ensure that these strange dates keep being supported by the times library. "It works now" is not good enough, it needs to be documented then and tests ensure it keeps working. And I think some special cases will hurt us and break it. |
I am ok with it to add a check |
I am closing this issue here as rejected. I came to the conclusion that these changes would cause more problems than they would solve. |
Ok, I reopened it as I found out, you are the author of this module. But my point stays the same.
|
Please don't close RFC's this way. RFC's do take some time and effort to write, and seeing them closed because of problems that aren't even specified is disheartening.
What's the benefit of allowing invalid dates? A good API should prevent wrong usage.
It's not the same, but it's the only way to offer a safe API.
The object constructor for
Why? Not being able to design a safe API unless every state of every datatype is safe is a strange limitation. It should be noted that almost every programming language has a safe API for their I don't think that behavior would fit in with how the times module works currently. The times module uses range types heavily ( |
+1. And the same goes for easily dismissing PRs. |
I will tell you a real story that did happen. A public organisation (could be any company, millions of legal entities worldwide like this) was providing services to its customers and required passport details to apply. One day a new client comes with a valid passport with birth date 1938-02-30. It turned out that It is legally impossible to change your birth date, you can change your name or address but you can't change your date of birth. Government refused him to give a new passport with a different birth date. The client then sue the company in court claiming he has unlawfully refused in service and he won the case. I don't think nim stdlib will be usable in a company/public organisations that provides services to human beings if it will be too strict. It is library responsibility to help dealing with all sorts of problems. |
@cooldome But can't this be handled during parsing? Allowing |
Also, this would not just a problem in representing this date as a DateTime object. It would create problems when stored in the database, when computing age, when sending to external systems.... |
This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 7 days. |
See nim-lang/Nim#14197. |
The problem
It's current possible to mutate the fields of a
DateTime
object, but it's very unsafe since it's easy to end up with an invalid state. Examples of invalid states include:The API should be modified to ensure that
DateTime
can only be used in safe ways.Proposal
Both options below requires all fields of
DateTime
to be made private to avoid manual construction and direct write access. This is a breaking change of course.Option 1: add assignment operators for the fields that are allowed to be mutated. These procs can update the derived fields and prevent invalid states. This is how it's done in D. It's a breaking change for code that writes to the fields.
Option 2: embrace immutability and add procs for updating fields without mutation, e.g
dt = dt.withYear(2015)
. This is how it's done in Java. Deprecated overloads for field assignments can still be added to prevent breaking old code, which means that this option only breaks code that uses theDateTime(...)
constructor.Note that it only makes sense to update some fields. For example
utcOffset
andisDst
should never be updated directly, so they can be made private without any way to update them. The fieldsweekday
andyearday
should likely be removed and replaced with procs that calculate the values on the fly. The only fields that should be allowed to be updated are the date & time fields, e.g year/month/day and so on.The text was updated successfully, but these errors were encountered: