Skip to content

Instantly share code, notes, and snippets.

@noboru-i
Last active September 8, 2022 03:32
Show Gist options
  • Select an option

  • Save noboru-i/db9fc6c6ea20ad1282574a24d3dc4e6d to your computer and use it in GitHub Desktop.

Select an option

Save noboru-i/db9fc6c6ea20ad1282574a24d3dc4e6d to your computer and use it in GitHub Desktop.
Check term
void main() {
_test1();
_test2();
_test3();
_test4();
// check `assert` works
assert(false);
}
void _test1() {
print('_test1');
final from = DateTime.parse('2022-08-01 00:00:00Z');
final to = DateTime.parse('2022-08-31 00:00:00Z');
assert(true == isInTerm(DateTime.parse('2022-08-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-07-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-09-11 00:00:00Z'), from, to));
}
void _test2() {
print('_test2');
final from = DateTime.parse('2022-08-01 00:00:00Z');
final to = null;
assert(true == isInTerm(DateTime.parse('2022-08-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-07-11 00:00:00Z'), from, to));
assert(true == isInTerm(DateTime.parse('2022-09-11 00:00:00Z'), from, to));
}
void _test3() {
print('_test3');
final from = null;
final to = DateTime.parse('2022-08-31 00:00:00Z');
assert(true == isInTerm(DateTime.parse('2022-08-11 00:00:00Z'), from, to));
assert(true == isInTerm(DateTime.parse('2022-07-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-09-11 00:00:00Z'), from, to));
}
void _test4() {
print('_test4');
final from = null;
final to = null;
assert(false == isInTerm(DateTime.parse('2022-08-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-07-11 00:00:00Z'), from, to));
assert(false == isInTerm(DateTime.parse('2022-09-11 00:00:00Z'), from, to));
}
bool isInTerm(DateTime target, DateTime? from, DateTime? to) {
if (from == null && to == null) {
return false;
}
if (from == null) {
return to!.isAfter(target);
}
if (to == null) {
return from.isBefore(target);
}
return from.isBefore(target) && target.isBefore(to);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment