Skip to content
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

DayCounter to simplify looping dates. #26

Open
nanjizal opened this issue Mar 22, 2020 · 2 comments
Open

DayCounter to simplify looping dates. #26

nanjizal opened this issue Mar 22, 2020 · 2 comments

Comments

@nanjizal
Copy link

I found it useful to create a simple DayCounter for my Covid19 project. I convert dates from a string in the csv data using your library and use my DayCounter to easily control days I render.

Perhaps something like day counter would be useful directly in datetime if not then left here incase useful for others, I expect you could create a date and then increment it by one day but this seemed clearer to work with a cut down format, perhaps you have similar feature already that I missed.

import datetime.DateTime;
@:structInit
class InternalDayCounter {
    public var day: Int;
    public var month: Int;
    public var year: Int;
    function new( day:  Int, month: Int, year:  Int ){
        this.day  = day;
        this.month  = month;
        this.year = year;
    }
}
@:forward
abstract DayCounter( InternalDayCounter ) from InternalDayCounter to InternalDayCounter {
    public inline
    function new( v: InternalDayCounter ){ this = v; }
    public inline
    function hasNext(){
        return true;
    }
    public inline
    function next(){
        var isLeap = DateTime.isLeap( this.year );
        var dayTot = DateTime.daysInMonth( this.month, isLeap );
        this.day++;
        if( this.day > dayTot ) {
            this.day = 1;
            this.month++;
            if( this.month > 12 ) {
                this.month = 1;
                this.year++;
            }
        }
    }
    public inline
    function matchDate( date: DateTime ){
        return date.getDay() == this.day && date.getMonth() == this.month && date.getYear() == this.year;
    }
}

typical use

var dayCounter = new DayCounter({day:5,month:3,year:2020});
function renderDay(){ // every so many frames
   var renderData = new Array<MyData>();  // MyData would contain date and some data values.
   var j = 0;
   for( d in myData ) {
      if( dayCounter.matchDate( d.date ) ) renderData[ j++ ] = d;
  }
  plot( renderData ); // plot data to screen
  dayCounter.next();
}
@nanjizal
Copy link
Author

if you do decide it's useful I can create pull request.

@RealyUniqueName
Copy link
Owner

You don't need to count days/months/years manually.

var d:DateTime = '2019-12-31 00:00:00';
d += Day(1);
trace(d); // 2020-01-01 00:00:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants