-
Notifications
You must be signed in to change notification settings - Fork 344
Promote System.Time to corefx? #1740
Comments
/cc @DamianEdwards |
Also, when we originally discussed this proposal back in 2015 (see dotnet/corefx#2116), it seemed that a If there's no objection, I'll move this forward by pitching an API proposal on the corefx repo. In other words, I don't see why this should be any different than other proposals for date/time such as dotnet/corefx#24555 and dotnet/corefx#24449. |
We should also get consensus on #1870 before doing so, if possible. |
Some questions/remarks: System.Date
System.TimeOfDay
|
@drieseng - great questions. Thanks! There are no addition/subtraction operators between two It would be possible to have
Regarding the optional time zone designator in For A better alignment than XSD would be HTML5, whose Also, keep in mind that I am not trying to introduce an entirely new date/time approach in .NET. That already exists in the Noda Time library. Instead, I am trying to simply fill in some much-needed gaps. Date-only data is everywhere (birthdays, business days, holidays, etc.), and so is time-only data (business hours, school hours, recurring appointments, etc.). Having these types built-in solves a real-world need. With regard to WCF - I can't speak specifically to that. However, I'll say that there are many uses for these types throughout the .NET ecosystem. In some cases, teams have had to develop their own pseudo-types, such as |
@mj1856 agree with everything except
It would return a |
@mj1856 Thanks for taking time to respond to my questions!
Adding two dates is indeed meaningsless. public static DateTime operator +(Date d, TimeSpan t) { ... }
public static DateTime operator +(Date d, TimeOfDay t) { ... } However I agree that the individual Add methods are also useful, since they allow you to avoid going through DateTime to arrive at another Date instance. As @benaadams already mentioned, for subtraction one could indeed define the following operator: public static TimeSpan operator -(Date d1, Date d2) { ... }
Wouldn't it be a good idea to have the WCF and EF (and other) teams chime in on this. It would be a pity if these types finally arrived in .NET, only to ascertain that this had zero impact on the problems that these could solve. |
@benaadams - Subtracting two For example, this coming Sunday is In other words, don't conceptualize dates as some aggregate unit of time. Instead. think of dates as you would a square on a paper 12-month calendar. It is an abstract concept, that doesn't have time or time zone. |
@drieseng - Always glad to participate in a discussion about dates and times! 👍 Of the two operators you suggested, it would be possible to include the second one, though it is essentially a shortcut for the The first one you suggested would not be valid. A Regarding integration with other teams - the need is already established, but it will take having these types ubiquitous before it will be possible to facilitate those discussions beyond what I've already been able to do (both here on GitHub, and internally). The general issue is that while everyone agrees that .NET's date/time handling is less-than-ideal, it's not quite bad enough to resist continuing with the workarounds we've had since the beginning (such as using Thus, with these new bits I'm proposing - I'm not looking to solve the world for everyone, but rather to pick off the lowest hanging fruit. These are the things that have the widest impact for those who are not looking to convert everything over to Noda Time, but still want to model their data more cleanly, and interact better with external APIs. |
Fair enough, subtraction may be ambiguous and it does have the |
Yep. I chose |
@drieseng It's not a problem to add But what's about landing? I agree with @mj1856 that the |
@YohDeadfall I'm sure there's a lot more to it than that. For example, the SOAP importer and exporter should be updated to support exposing |
@drieseng, which could be done by data contracts, |
@YohDeadfall Is it feasible to update WCF server (.NET Framework) as well? |
@mj1856 If this is intended to be used as the data type for xs:time, you need to think about being able to round trip data. Although you have found little value in the offset, someone has and are using it in xml documents. If there is no offset in the .Net data type, it isn't a match for xs:time and shouldn't be suggested to be. Otherwise what do you do in the XmlSerializer when you come across an offset? Throw an exception as you can't parse it? Parse it without the offset and lose the ability to roundtrip data from xml? |
@YohDeadfall, adding support to DataContractSerializer isn't as simple as you think. We have the .Net native pre-gen serializer implementation which isn't open source which needs to be updated to match. XmlSerializer also needs support added. NetDataContractSerializer would also need support, although I don't think that's available on .Net core. Then you have the JsonDataContractSerializer would also need updating. We have our svcutil application for generating WCF proxies, WCF Connected Services too. And this would need to be done in a way so as not to break people who already do their own serialization of the xs:time xml type. Adding @shmao and @zhenlan who own the serializers. |
Don't we already get Regarding the optional offset allowed by XSD, I would expect it would drop an offset if encountered on deserialization. However, I can understand that might be seen as a bug. Then again, I also expect So, since it seems we're already in the game of using custom types, I suppose we could serialize these two as custom types as well. Using a restriction facet, which would be almost identical to this example in the XSD docs, would more precisely align the types with XSD: <simpleType name='bare-date'>
<restriction base='date'>
<pattern value='[^:Z]*'/>
</restriction>
</simpleType> Or would this confuse folks even more? I can implement this easily, as |
@mj1856 The problem is that the |
@mj1856, DataContractSerialier et al handle xml primitive types natively and don't use anything like IXmlSerializable. As xs:time is an xml primitive, the convention is that the serializers have innate knowledge how to handle them. IXmlSerializable is meant for custom serializable types. As for how xs:datetime is handled, just search stackoverflow or use your favorite search engine and look for issues people have with it and .Net. This has caused a lot of confusion and difficulty in the past. A restrictive facet depends on there being an xsd available. For WCF usage, we could potentially emit a restriction facet when creating a wsdl, but we aren't set up to consume very many xsd restrictions so it would be a lot of work to detect the restriction and then choose the new Time work as there's no framework that already exists to take that into account. |
I see. Well then, it seems that the best option would be to remove the XML serialization aspect from the structs entirely. Then other components can determine whether or not to support them, when, how, etc. separately. Or are there other ideas? |
I think it ultimately comes down to what is the justification/reasoning for adding these types? Is the primary reason to enable marshalling back and forth to the web? In which case what would be the right choice when designing a data type from scratch is irrelevant as you need to map to the existing data types that someone else has designed. Then any disagreement with the design choices made by others in other standards is incompatible with the purpose of the types. If this is for web interop, these types should primarily be considered convenience OM types which only need basic manipulation methods and the primary focus should be making sure they suit the function of losslessly serializing to and from web formats. This would mean they should have a timezone field, even if conceptually it doesn't make sense. If this is intended for HTML5 usage only, then what's the justification for excluding XML beyond "I don't like how they wrote their standard"? I this is to facilitate HTML5 only then personally I think we should reject the proposal to add the types to the framework.
|
/cc @jskeet |
Before any discussion about what values should be stored in a type, you need to define the purpose, the intended usage for that type. Can we define that first? Below is just a discussion of why this is important but this is the key point. A new type needs to be suitable for its intended purpose (which probably should include explicit non-goals) and unless that's defined, people will be talking at cross purposes. 1, That was my whole point. If this is meant for use with web standards including XML, then timezone is needed. It's needed to be able to roundtrip the data without information loss. Regardless if you think the information has any value or not, it's part of the XML spec and not having it in the .Net data type does mean there will be information loss when round tripping. Basically what I'm saying is the first question that needs to be answered is why? Why is Date and Time needed? Is the reason because of deficiencies in DateTime/DateTimeOffset? Is so, then why is Date needed but not Month? What about Hour? Or is the reason for interop with external sources more easily? If so, then what's the list of target external sources? SQL? What about Oracle, MySQL, PostgreSQL? What about web standards, HTML5, XML, JSON, E-Mail, HTTP date headers, SMTP date fields? ISO-8601 profiles, which profiles? If this is for use with external sources, it needs to be stated which external sources are goals to interop with and each evaluated to work out if the proposed solution will work with it. |
@mconnew - you bring up a good point, but the "why" has already been asked and answered. We're well past that point. This work is the result of long discussions in dotnet/corefx#700 and dotnet/corefx#2116. If you've read through those and feel it's still not clear why these types are needed, please let me know. Perhaps I can summarize more succinctly. |
@YohDeadfall respectfully, I really don't think In general, trying to cover all edge cases in the domain of date and time will eventually lead to a design like Noda Time, which is already available for those that need it. I've expanded nodatime/nodatime#864 to discuss adding |
@mj1856 I'm fully agree with you, Is it a good idea to publish the types with partial serialization support (without timezones)? For many devs it would be enough for now I think. |
I'm all for these new types, but specifically for XML/SOAP (de)serialization I would either like to have serialization support that is fully compatible with XML Schema data types (meaning with timezones), or no serialization support at all. I would prefer the former, but the latter may at least allow "us" to add full serialization support in a future iteration without introducing breaking changes. Having support for XML Schema data type (de)serialization that mostly works should be avoided. |
@mconnew and I met in person yesterday to discuss the tradeoffs and different approaches. The consensus we came to is that if I'd still like to leave this issue open for other discussion around promoting this to corefx. Thanks. |
Closing this, just because I already have issues open for each specific item remaining. When complete, I'll open API request issues in corefx. Thanks all. |
@mj1856 I've been coming here after reading your older vision about date/time handling in this comment. I find it hard to track this since it seems to be spread all over the place. Is there a place I can track this to find out when I can use the new types in entirety? |
Is it possible to reopen this issue? |
@Dreamescaper I am looking at some issues including this one for the planning. Could you advise which types you are interested in from this library and explain more about your usage and need for it? I am just gathering some data regarding such features and the requests. Thanks. |
@tarekgh I could use DateTime type, and consider Time component only, but it leads to issues with serialization - I often need separate Date type as well, but it's a bit less painfull to use DateTime type for Date only (comparing to case with Time only). |
@tarekgh We have a lot of products that is valid from date X to date Y. When we used |
I'm also interested into the type Date. In my experience it's a fairly common requirements to compare only dates, using a |
@OskarKlintrot @ilmax it is useful feedback. thank you. would you need formatting and parsing functionality for the Date and Time. I am seeing @Dreamescaper mention he is using Parsing but I am interested what kind of functionality you need with these classes? |
For our use case we are just deserializing from json or mapping from |
Ideally I would like the
so I would love to see this new |
@ilmax puts it better than I do, listen to him instead of me 😛 |
@ilmax, serialization is a difficult thing to achieve. For example, for XML, there is the xsd:date datatype, but it has an optional timezone. This issue was originally to support the date and time types used in HTML forms as defined by HTML5. The HTML5 date datetype doesn't include the timezone. So what do you do if you try to deserialize some XML with an xsd:date that has a timezone? Then we get to JSON, what's the right thing to do there? There is no official date format for JSON, it's just a string which an application can choose to interpret as a date. The JavaScript method Date.toJSON outputs a date and time with a timezone in ISO8601 format. But many people use a number which represents milliseconds since 1970 as that's what the JavaScript Date constructor accepts. Then you get to Protobuf which has no definition but Google has published a "Well Known Types" extension which has a Timestamp datatype. Reading the spec on that, it's similar to the general JSON approach, but is a number representing nanoseconds since some starting point. It doesn't have any concept of timezones, but it does represent an exact point in time. Which means a Protobuf Timestamp will represent different calendar dates for different timezones. So there is no way to simply represent an unambiguous date over gRPC without inventing some new over-the-wire representation. And I haven't even mentioned the issue of SQL. And this is the crux of the problem. If you define a date or time datatype with the purpose of being used in a particular context (in this case originally HTML5), that datatype will be unsuitable for other contexts. Within .NET the only thing you can really do is create a Date and a Time datatype for representing a simple Date or a simple Time with no intended purpose to use with any particular technology such a gRPC or WebAPI and then have each technology have it's own defined type representing the full fidelity of what that technology can represent. For example, gRPC can give you an exact point in time down to the nanosecond, but no data about timezone so then it's up to the application developer to decide how to handle the timezone issue if they just want a date. E.g. pick a timezone and use noon in that timezone and roundtrip dates with that. Or with XML, you have to decide what you want to do with the timezone, store it separately, ignore it, treat it's presence as an error? So if everyone is going to need to have their own representation for a date and a time based on their technology, what's the purpose of the Date and Time classes? They don't make any sense in isolation and are insufficient for every possible usage there is. Personally I'd prefer to see a suite of data types. An XmlDate, an XmlTime, an XmlDateTime, a protobuf TimeStamp, and for JSON, maybe a helper class which can consume and emit a range of popular formats used. But that would be down to each library to create and not a central type defined in the System root namespace. |
I don't mean to diminish or invalidate what you are saying but remember that NodaTime (and JodaTime, I presume) have had Date and Time for years now that can be serialized and works out-of-the-box for millions of developers. NodaTime have ~22 million downloads so I would say that the need for a Date and Time is very real for a lot of developers. Also, note that @ilmax is specifically mentioning serialization with
|
@mconnew thanks for the deep explanation, I wasn't aware of all these limitations. It's indeed a pity to not have industry wide standards on how to serialize a date and a time, but I still think there's some value in having them also to somehow achieve parity with other languages. |
Databases have several date/time data types. And each has their own set of types. Obviously, you can only use a .NET type that somehow matches the SQL type. This is the responsibility of the programmer. For exchange formats there is ISO 8601. It defines about every possible selection and combination of dates and times and time spans and ranges and what not. So I see no problem with transferring any date or time to another system. And if that other system requires the data in a specific/custom format, the programmer again has the task to provide/accept it accordingly. It's not like there would be a universally accepted format today, so no compatibility can be broken here. None of this limits the possibility of the existence of types like |
As discussed in aspnet/Mvc#6648, the types in the
System.Time
package would be useful in providing support in MVC tag helpers for<input type="date" />
and<input type="time" />
which are in the HTML5 spec.I see only one open issue, #300, which is a feature request. Other than this, what are the steps necessary to promote
System.Time
to corefx and make it available on nuget.org?The text was updated successfully, but these errors were encountered: