Last active
December 20, 2019 15:40
-
-
Save OhkuboSGMS/329fa2d0447dbdb25cb9135ff4327d81 to your computer and use it in GitHub Desktop.
月ごとのイテレーター
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:collection'; | |
| main(){ | |
| // var base =DateTime(2019,2); | |
| // var cnt =0; | |
| // for (var d in MonthRange(DateTime(base.year,base.month-6),base)) { | |
| // print(d); | |
| // cnt++; | |
| // } | |
| // print("Count :$cnt"); | |
| // | |
| // print(subtractMonth(base, 14)); | |
| // print(subtractMonth(base, 2)); | |
| // print(subtractMonth(base, 1)); | |
| // print(subtractMonth(base, 36)); | |
| } | |
| class _MonthRangeIterator implements Iterator<DateTime>{ | |
| final DateTime start; | |
| final DateTime goal; | |
| DateTime _currentMonth ; | |
| bool _nowSameDate=false; | |
| _MonthRangeIterator(this.start,this.goal){ | |
| if(this.goal.year==this.start.year && this.goal.month == this.start.month){ | |
| _currentMonth =null; | |
| }else{ | |
| _currentMonth =DateTime(this.goal.year,this.goal.month+1); | |
| } | |
| } | |
| bool get _sameYearMonth => this.current.year==this.start.year && this.current.month == this.start.month; | |
| bool _backOneMonth(){ | |
| final int month =_currentMonth.month; | |
| if(month == DateTime.january){ | |
| var year = _currentMonth.year -1; | |
| _currentMonth = DateTime(year,DateTime.december); | |
| }else{ | |
| _currentMonth = DateTime(_currentMonth.year,month-1); | |
| } | |
| return _sameYearMonth; | |
| } | |
| @override | |
| bool moveNext() { | |
| print("moveNExt"); | |
| if(_currentMonth==null)return false; | |
| if(_nowSameDate)return false; | |
| _nowSameDate =_backOneMonth(); | |
| return true; | |
| } | |
| @override | |
| DateTime get current => _currentMonth; | |
| } | |
| class MonthRange extends IterableBase<DateTime>{ | |
| final DateTime start; | |
| final DateTime goal; | |
| MonthRange(this.start, this.goal); | |
| @override | |
| Iterator<DateTime> get iterator => _MonthRangeIterator(start, goal); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment