Skip to content

DayCounter to simplify looping dates. #26

@nanjizal

Description

@nanjizal

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions