-
Notifications
You must be signed in to change notification settings - Fork 1
ADD : Period & ITimeBlock depuis Lucca #63
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System; | ||
|
|
||
| namespace NExtends.Primitives.DateTimes | ||
| { | ||
| public interface ITimeBlock | ||
| { | ||
| DateTime StartsAt { get; } | ||
| DateTime EndsAt { get; } | ||
| TimeSpan Duration { get; } | ||
|
|
||
| void ChangeStartsAt(DateTime startsAt); | ||
| void ChangeEndsAt(DateTime endsAt); | ||
| void ChangeDuration(TimeSpan duration); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System; | ||
| using System.Runtime.Serialization; | ||
|
|
||
| namespace NExtends.Primitives.DateTimes | ||
| { | ||
| [Serializable] | ||
| public class NegativeDurationException : ArgumentException | ||
| { | ||
| protected NegativeDurationException(SerializationInfo info, StreamingContext context) | ||
| : base(info, context) { } | ||
|
|
||
| public NegativeDurationException(string paramName) | ||
| : base("You cannot create or update a period to a negative duration", paramName) { } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,29 +46,39 @@ public static TimeSpan Sum<TSource>(this IEnumerable<TSource> source, Func<TSour | |
| public static bool IsNegativeOrZero(this TimeSpan t1) { return t1.Ticks <= 0; } | ||
| public static bool IsNegative(this TimeSpan t1) { return t1.Ticks < 0; } | ||
|
|
||
| public static string Humanize(this TimeSpan timeSpan, TimeUnit timeUnit, TimeInitials initials, bool showSign = false) | ||
| public static string Humanize(this TimeSpan timeSpan, TimeUnit timeUnit, bool showSign = false) | ||
| { | ||
| return Humanize(timeSpan, timeUnit, CultureInfo.CurrentCulture, showSign); | ||
| } | ||
| public static string Humanize(this TimeSpan timeSpan, TimeUnit timeUnit, CultureInfo culture, bool showSign = false) | ||
| { | ||
| switch (timeUnit) | ||
| { | ||
| case TimeUnit.Day: | ||
| return ToDays(timeSpan, initials, showSign); | ||
| return ToDays(timeSpan, culture, showSign); | ||
| case TimeUnit.Duration: | ||
| case TimeUnit.Time: | ||
| return ToHours(timeSpan, initials, showSign); | ||
| return ToHours(timeSpan, culture, showSign); | ||
| case TimeUnit.NotApplicable: | ||
| default: | ||
| throw new InvalidEnumArgumentException(nameof(timeUnit)); | ||
| } | ||
| } | ||
|
|
||
| public static string ToHours(this TimeSpan timeSpan, TimeInitials initials, bool showSign = false) | ||
| public static string ToHours(this TimeSpan timeSpan, bool showSign = false) | ||
| { | ||
| return ToHours(timeSpan, CultureInfo.CurrentCulture, showSign); | ||
| } | ||
| public static string ToHours(this TimeSpan timeSpan, CultureInfo culture, bool showSign = false) | ||
| { | ||
| if (timeSpan == TimeSpan.Zero) | ||
| { | ||
| return "-"; | ||
| } | ||
|
|
||
| var absSpan = new TimeSpan(Math.Abs(timeSpan.Ticks)); | ||
| var totalHours = Math.Floor(absSpan.TotalHours); | ||
| var initials = TimeInitials.FromCulture(culture); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on pourrait s'appuyer sur https://github.com/Humanizr/Humanizer pour ce genre de choses
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not, on peut faire ça dans une version ultérieure avec une Issue dédiée. |
||
|
|
||
| var sb = new StringBuilder(); | ||
| if (showSign && timeSpan > TimeSpan.Zero) | ||
|
|
@@ -91,17 +101,26 @@ public static string ToHours(this TimeSpan timeSpan, TimeInitials initials, bool | |
| { | ||
| sb.Append(initials.MinutesInitial); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| public static string ToDays(this TimeSpan span, TimeInitials initials, bool showSign = false) | ||
| public static string ToDays(this TimeSpan span, bool showSign = false) | ||
| { | ||
| return ToDays(span, CultureInfo.CurrentCulture, showSign); | ||
| } | ||
|
|
||
| public static string ToDays(this TimeSpan span, CultureInfo culture, bool showSign = false) | ||
| { | ||
| if (span == TimeSpan.Zero) | ||
| { | ||
| return "-"; | ||
| } | ||
|
|
||
| var absSpan = new TimeSpan(Math.Abs(span.Ticks)); | ||
| var sb = new StringBuilder(); | ||
| var initials = TimeInitials.FromCulture(culture); | ||
|
|
||
| if (showSign && span > TimeSpan.Zero) | ||
| { | ||
| sb.Append("+"); | ||
|
|
@@ -110,7 +129,9 @@ public static string ToDays(this TimeSpan span, TimeInitials initials, bool show | |
| { | ||
| sb.Append("-"); | ||
| } | ||
|
|
||
| sb.AppendFormat(CultureInfo.InvariantCulture, "{0} " + initials.DaysInitial, absSpan.TotalDays.RealRound(5)); | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Il est interdit d'implémenter
GetHashCode()sur une une instance mutable, car la plupart des primitives de liste gardent le hashcode calculé en cache. Hors si l'instance est mutable, alors une fois modifiée, le HashCode n'est plus valide, mais il ne sera pas pour autant calculé.C'est le genre d'effet de bord à se taper la tête contre les murs lors du débug.
D'autre part, pour des class, toujours implémenter un
IEqualityComparer<T>plutôt queIEquatable<T>.