Skip to content

Commit 5c6318d

Browse files
committed
fix #19, add min/max
1 parent 0e6aa1b commit 5c6318d

12 files changed

+41
-13
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.2.0
2+
3+
- Fixed `.endOf(DurationUnit.week)` was incorrectly returning 7 days ahead [#19](https://github.com/sadespresso/moment_dart/issues/19)
4+
- Renamed `DateTime.to` to `.rangeTo` for clarity. Old method will be removed on the next released.
5+
- Added `Moment.maxValue` and `Moment.minValue`. For example, now you can use `CustomTimeRange(now, Moment.maxValue)` or `epoch.to(Moment.maxValue)`
6+
17
## 2.1.0
28

39
- Fixed `LocalWeekTimeRange` and `IsoWeekTimeRange`'s `to` being 1 microsecond ahead

lib/src/extensions/benefits.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,25 @@ extension MomentBenefits on DateTime {
221221
/// Returns [CustomTimeRange] from [this] to [other]
222222
///
223223
/// If [this] is after [other], it will be swapped.
224-
CustomTimeRange to(DateTime other) {
225-
if (this <= other) return CustomTimeRange(this, other);
224+
CustomTimeRange rangeTo(DateTime other) {
225+
if (this < other) return CustomTimeRange(this, other);
226226

227227
return CustomTimeRange(other, this);
228228
}
229+
230+
/// Returns [CustomTimeRange] from [this] to [other]
231+
///
232+
/// If [this] is after [other], it will be swapped.
233+
CustomTimeRange rangeToMax() => CustomTimeRange(this, Moment.maxValue);
234+
235+
/// Returns [CustomTimeRange] from [this] to [other]
236+
///
237+
/// If [this] is after [other], it will be swapped.
238+
@Deprecated(
239+
"This will be deprecated in the next major"
240+
"release. Please use [rangeTo] instead.",
241+
)
242+
CustomTimeRange to(DateTime other) => rangeTo(other);
229243
}
230244

231245
extension MomentBenefitsPlus on Moment {

lib/src/extensions/end_of.dart

+1-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ extension EndOfUnit on DateTime {
150150
DateTime endOfLocalWeek([int? weekStart]) {
151151
weekStart ??= Moment.defaultLocalization.weekStart;
152152

153-
int delta = (weekStart + 6) - weekday;
154-
if (delta > 7) {
155-
delta -= 7;
156-
}
153+
final int delta = (weekStart + 6 - weekday) % 7;
157154

158155
return add(Duration(days: delta)).endOfDay();
159156
}

lib/src/extensions/start_of.dart

+1-5
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ extension StartOfUnit on DateTime {
106106
DateTime startOfLocalWeek([int? weekStart]) {
107107
weekStart ??= Moment.defaultLocalization.weekStart;
108108

109-
int delta = weekday - weekStart;
110-
111-
if (delta < 0) {
112-
delta += 7;
113-
}
109+
final int delta = (weekday - weekStart + 7) % 7;
114110

115111
return subtract(Duration(days: delta)).startOfDay();
116112
}

lib/src/localization.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ abstract class MomentLocalization {
315315
/// Language name in English
316316
String get languageNameInEnglish;
317317

318-
/// Start of week. For example, Monday for most countries, Sunday for weird ones
318+
/// Start of week. For example, Monday for most countries, Sunday for the weird ones
319319
int get weekStart => DateTime.monday;
320320
}
321321

lib/src/moment.dart

+5
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,9 @@ class Moment extends DateTime {
576576
/// epoch in UTC
577577
static DateTime epochUtc =
578578
DateTime.fromMicrosecondsSinceEpoch(0, isUtc: true);
579+
580+
static DateTime maxValue =
581+
DateTime.fromMicrosecondsSinceEpoch(8640000000000000000);
582+
static DateTime minValue =
583+
DateTime.fromMicrosecondsSinceEpoch(-8640000000000000000);
579584
}

lib/src/time_range/day.dart

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class DayTimeRange extends TimeRange with PageableRange<DayTimeRange> {
99
final int month;
1010
final int day;
1111

12+
/// Please note that [hour], [minute], [second], [millisecond], [microsecond]
13+
/// will be ignored.
1214
const DayTimeRange(
1315
this.year,
1416
this.month,

lib/src/time_range/hour.dart

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class HourTimeRange extends TimeRange with PageableRange<HourTimeRange> {
1010
final int day;
1111
final int hour;
1212

13+
/// Please note that [minute], [second], [millisecond], [microsecond]
14+
/// will be ignored.
1315
const HourTimeRange(
1416
this.year,
1517
this.month,

lib/src/time_range/month.dart

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class MonthTimeRange extends TimeRange with PageableRange<MonthTimeRange> {
88
final int year;
99
final int month;
1010

11+
/// Please note that [day], [hour], [minute], [second], [millisecond], [microsecond]
12+
/// will be ignored.
1113
const MonthTimeRange(
1214
this.year,
1315
this.month, {

lib/src/time_range/week.dart

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class LocalWeekTimeRange extends CustomTimeRange
66
implements PageableRange<LocalWeekTimeRange> {
77
final int? weekStart;
88

9+
/// from `.startOfLocalWeek` to `.endOfLocalWeek`, inclusive
910
LocalWeekTimeRange(DateTime dateTime, [this.weekStart])
1011
: super(
1112
dateTime.startOfLocalWeek(weekStart),

lib/src/time_range/year.dart

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class YearTimeRange extends TimeRange with PageableRange<YearTimeRange> {
1313
});
1414

1515
/// Will preserve the timezone of [dateTime]
16+
///
17+
/// Please note that [month], [day], [hour], [minute], [second], [millisecond], [microsecond]
18+
/// will be ignored.
1619
factory YearTimeRange.fromDateTime(DateTime dateTime) =>
1720
YearTimeRange(dateTime.year, isUtc: dateTime.isUtc);
1821

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: moment_dart
22
description: Multi-purpose immutable DateTime subclass. Supports multiple localizations to easily convert DateTime and Duration into human-readable format
3-
version: 2.1.0
3+
version: 2.2.0
44
homepage: https://github.com/sadespresso/moment_dart
55
issue_tracker: https://github.com/sadespresso/moment_dart/issues
66
funding:

0 commit comments

Comments
 (0)