Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

110
LINES

< > BotCompany Repo | #1028549 // dateStructureToTimestampRange

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3709L/23K).

static LongRange dateStructureToTimestampRange(DateStructures.SomeDate d, DateInterpretationConfig config default new DateInterpretationConfig) {
  ret new DateStructureToTimestampRange(config).getRange(d);
}

sclass DateStructureToTimestampRange {
  TimeZone tz;
  long now;
  bool assumeFuture;
  
  delegate SomeDate to DateStructures.
  delegate Day to DateStructures.
  delegate Month to DateStructures.
  delegate Week to DateStructures.
  delegate Year to DateStructures.
  delegate CurrentWeekPlus to DateStructures.
  delegate CurrentMonthPlus to DateStructures.
  delegate CurrentYearPlus to DateStructures.
  delegate Weekday to DateStructures.
  delegate TodayPlus to DateStructures.
  delegate Hour to DateStructures.
  delegate Between to DateStructures.
  
  DateStructureToTimestampRange(DateInterpretationConfig config) {
    tz = config.timeZone;
    now = config.now;
    assumeFuture = config.assumeFuture;
  }
  
  Year currentYear() {
    ret new Year(year(now, tz));
  }
  
  Month currentMonth() {
    ret new Month(month(now, tz), currentYear());
  }
  
  SomeDate concretizeDate(SomeDate d) {
    if (d cast CurrentMonthPlus)
      d = new Month(month(now, tz) + d.nMonths, currentYear());
    if (d cast TodayPlus)
      d = new Day(dayOfMonth(now, tz) + d.nDays, currentMonth());
    if (d cast CurrentYearPlus)
      d = new Year(year(now, tz) + d.nYears);
    if (d cast CurrentWeekPlus)
      d = new Week(weekInYear(now, tz) + d.nWeeks, currentYear());
    ret d;
  }
  
  LongRange getRange(SomeDate d) {
    d = concretizeDate(d);
    
    // year
    
    if (d cast Year)
      ret longRange(yearToTimestamp(d.year, tz), yearToTimestamp(d.year+1, tz));
      
    // month
    
    if (d cast Month) {
      if (d.year == null)
        d.year = (Year) concretizeDate(new CurrentYearPlus(d.month < currentMonth().month ? 1 : 0));
      int year = d.year.year;
      ret longRange(
        yearAndMonthToTimestamp(year, d.month, tz),
        yearAndMonthToTimestamp(year, d.month+1, tz));
    }
    
    // week
    
    if (d cast Week) {
      int year = d.year.year;
      ret longRange(
        yearAndWeekToTimestamp(year, d.week, tz),
        yearAndWeekToTimestamp(year, d.week+1, tz));
    }
    
    if (d cast Weekday) {
      if (d.week == null) d.week = (Week) concretizeDate(new CurrentWeekPlus(d.weekday < dayOfWeek_nr(now, tz) ? 1 : 0));
      long start = getRange(d.week).start+(d.weekday-1)*24*60*60*1000;
      ret LongRange(start, start+24*60*60*1000);
    }

    // day
  
    if (d cast Day) {
      int month = d.month.month;
      int year = d.month.year.year;
      ret longRange(
        ymdToTimestamp(year, month, d.day, tz),
        ymdToTimestamp(year, month, d.day+1, tz));
    }
    
    // hour
    
    if (d cast Hour) {
      if (d.isPM == null) fail("AM/PM unknown: " + d);
      long dayStart = getRange(assertNotNull("day of hour", d.day)).start;
      int actualHour = d.hour+(d.isPM ? 12 : 0);
      ret longRange(dayStart+hoursToMS(actualHour), dayStart+hoursToMS(actualHour+1));
    }
    
    // between
    
    if (d cast Between) {
      ret longRange(getRange(d.from).start, getRange(d.to).start);
    }
    
    fail("dateStructureToTimestampRange: unknown type " + d);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028549
Snippet name: dateStructureToTimestampRange
Eternal ID of this version: #1028549/45
Text MD5: ab3fb4fee88a03fd86804eba5493a471
Transpilation MD5: 493702b98648d5f3b4d2cb37a11a5de9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-28 18:41:58
Source code size: 3367 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 358 / 575
Version history: 44 change(s)
Referenced in: [show references]