Viewing version 2.1.6. Open the latest package page.
time
2.1.6Type-safe DateTime and Duration calculations, powered by extensions.
Readme
⏰ Time
With shiny extensions, if you have ever written something like this, then look no further:
final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);
🎖 Installation
dependencies:
time: "^2.1.6"
⚡ Import
import 'package:time/time.dart';
🎮 Usage
final Duration tenMinutes = 10.minutes;
final Duration oneHourThirtyMinutes = 1.5.hours;
final DateTime afterTenMinutes = DateTime.now() + 10.minutes;
final Duration tenMinutesAndSome = 10.minutes + 15.seconds;
final int tenMinutesInSeconds = 10.minutes.inSeconds;
final DateTime tenMinutesFromNow = 10.minutes.fromNow;
You can perform all basic arithmetic operations on Duration as you always have been:
final Duration interval = 10.minutes + 15.seconds - 3.minutes + 2.hours;
final Duration doubled = interval * 2;
You can also use these operations on DateTime:
final DateTime oneHourAfter = DateTime() + 1.hours;
Duration is easily convertible as it always has been:
final int twoMinutesInSeconds = 2.minutes.inSeconds;
You can also convert Duration to DateTime, if needed:
final DateTime timeInFuture = 5.minutes.fromNow;
final DateTime timeInPast = 5.minutes.ago;
Iterate through a DateTime range:
final DateTime start = DateTime(2019, 12, 2);
final DateTime end = start + 1.weeks;
final DateTime tuesday = start.to(end).firstWhere((date) => date.weekday == DateTime.tuesday);
Granular comparison between DateTime fields:
final DateTime specificDate = DateTime(2021, 01, 01);
final DateTime otherDate = DateTime(2021, 02, 01);
print(specificDate.isAtSameYearAs(otherDate)); // true
print(specificDate.isAtSameMonthAs(otherDate)); // false
print(specificDate.isAtSameDayAs(otherDate)); // false
You can also delay code execution:
void doSomething() async {
await 5.seconds.delay;
// Do the other things
}
You can also use the popular copyWith:
final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1);
final expected = initial.copyWith(
year: 2021,
month: 10,
day: 28,
hour: 12,
minute: 45,
second: 10,
millisecond: 0,
microsecond: 12,
);
Visit the API Reference to find out all that is available.
🐛 Bugs/Requests
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.
👏 Inspiration
⭐ License
MIT License
Changelog
2.1.6
- Include
millisecondsandmicrosecondsintimeOfDayextension toDateTime
2.1.5
- Introduce
endOfDayextension toDateTime
2.1.4
- Introduce
shiftextension toDateTime
2.1.3
- Introduce
isWeekendextension toDateTime - Introduce
isWorkdayextension toDateTime
2.1.2
- Fix
minandmaxassertion withclampextension
2.1.1
- Introduce
firstDayOfWeekextension toDateTime - Introduce
lastDayOfWeekextension toDateTime - Introduce
firstDayOfMonthextension toDateTime - Introduce
lastDayOfMonthextension toDateTime - Introduce
firstDayOfYearextension toDateTime - Introduce
lastDayOfYearextension toDateTime - Introduce
clampextension toDateTime - Introduce
clampextension toDuration
2.1.0
- Introduce
package:clockdependency for more predictable testing
2.0.1
- Implement utc support for
copyWithoperation
2.0.0
- Migrate to null-safety
1.5.0-nullsafety.1
- Introduce
isAtSameYearAsextension toDateTime - Introduce
isAtSameMonthAsextension toDateTime - Introduce
isAtSameDayAsextension toDateTime - Introduce
isAtSameHourAsextension toDateTime - Introduce
isAtSameMinuteAsextension toDateTime - Introduce
isAtSameMillisecondAsextension toDateTime - Introduce
isAtSameMicrosecondAsextension toDateTime - Introduce
isLeapYearextension toDateTime - Introduce
daysInMonthextension toDateTime
final DateTime specificDate = DateTime(2021, 01, 01);
final DateTime otherDate = DateTime(2021, 02, 01);
print(specificDate.isAtSameYearAs(otherDate)); // true
print(specificDate.isAtSameMonthAs(otherDate)); // false
print(specificDate.isAtSameDayAs(otherDate)); // false
1.4.1
- Introduce
isAtSameYearAsextension toDateTime - Introduce
isAtSameMonthAsextension toDateTime - Introduce
isAtSameDayAsextension toDateTime - Introduce
isAtSameHourAsextension toDateTime - Introduce
isAtSameMinuteAsextension toDateTime - Introduce
isAtSameMillisecondAsextension toDateTime - Introduce
isAtSameMicrosecondAsextension toDateTime - Introduce
isLeapYearextension toDateTime - Introduce
daysInMonthextension toDateTime
1.5.0-nullsafety.0
- Migrated to null-safe dart
1.4.0
- Introduce
isTodayextension toDuration - Introduce
isTomorrowextension toDateTime - Introduce
wasYesterdayextension toDateTime
1.3.0
- Introduce
delayextension toDuration - Introduce
copyWithextension toDateTime
1.2.0
- Iterate through a
DateTimerange:
final DateTime start = DateTime(2019, 12, 2);
final DateTime end = start + 1.weeks;
final DateTime tuesday = start.to(end).firstWhere((date) => date.weekday == DateTime.tuesday);
1.1.1
- Fix breaking change from v1.1.0 by introducing @deprecated flag
1.1.0
- Breaking Change: renamed
latertofromNowto align with other ecosystems - Introduced support for other variants of
numi.edouble
1.0.0
- Named extensions to allow discoverability
- Introduced quality control test cases
0.0.1+1
- Unify extensions for int, Duration & DateTime
0.0.1
- Initial version
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'time:2.1.6'pubspec.yaml
name: time
description: Type-safe DateTime and Duration calculations, powered by extensions.
version: 2.1.6
homepage: https://github.com/jogboms/time.dart
environment:
sdk: ">=2.12.0 <4.0.0"
dependencies:
clock: ">=1.1.1 <2.0.0"
funding:
- https://www.paypal.me/jogboms
- https://www.buymeacoffee.com/jogboms
dev_dependencies:
lints: ^6.0.0
test: ^1.28.0
coverage: ^1.15.0
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | On demand | Download | 37d78d5d…7510d6 |
| 0.0.1+1 | On demand | Download | bd2a9bab…a11e79 |
| 1.0.0 | On demand | Download | be965b59…07040f |
| 1.1.0 | On demand | Download | 768d0c18…70c5c0 |
| 1.1.1 | On demand | Download | f6e16bf3…ce11e1 |
| 1.2.0 | On demand | Download | 9e25d486…af249f |
| 1.3.0 | On demand | Download | d6efb2a4…d8b877 |
| 1.4.0 | On demand | Download | 6bbb0a77…5b1878 |
| 1.4.1 | On demand | Download | 3dca0fa6…be0284 |
| 1.5.0-nullsafety.0 | On demand | Download | e12cb283…165313 |
| 1.5.0-nullsafety.1 | On demand | Download | 943006c9…e6ade9 |
| 2.0.0 | On demand | Download | 21535c17…5ae0ce |
| 2.0.1 | On demand | Download | 56f524b3…34ed60 |
| 2.1.0 | On demand | Download | 44c6a63e…7ab611 |
| 2.1.1 | On demand | Download | 28e2673c…4fa9db |
| 2.1.2 | On demand | Download | 267028bb…a07844 |
| 2.1.3 | On demand | Download | 83427e11…140124 |
| 2.1.4 | On demand | Download | ad8e018a…48c221 |
| 2.1.5 | On demand | Download | 370572cf…e6f461 |
| 2.1.6 | Cached · Upstream SHA-256 matched | Download | 46187cf3…0c789f |
Dependencies
clock "\u003e=1.1.1 \u003c2.0.0"