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).

1  
static LongRange dateStructureToTimestampRange(DateStructures.SomeDate d, DateInterpretationConfig config default new DateInterpretationConfig) {
2  
  ret new DateStructureToTimestampRange(config).getRange(d);
3  
}
4  
5  
sclass DateStructureToTimestampRange {
6  
  TimeZone tz;
7  
  long now;
8  
  bool assumeFuture;
9  
  
10  
  delegate SomeDate to DateStructures.
11  
  delegate Day to DateStructures.
12  
  delegate Month to DateStructures.
13  
  delegate Week to DateStructures.
14  
  delegate Year to DateStructures.
15  
  delegate CurrentWeekPlus to DateStructures.
16  
  delegate CurrentMonthPlus to DateStructures.
17  
  delegate CurrentYearPlus to DateStructures.
18  
  delegate Weekday to DateStructures.
19  
  delegate TodayPlus to DateStructures.
20  
  delegate Hour to DateStructures.
21  
  delegate Between to DateStructures.
22  
  
23  
  DateStructureToTimestampRange(DateInterpretationConfig config) {
24  
    tz = config.timeZone;
25  
    now = config.now;
26  
    assumeFuture = config.assumeFuture;
27  
  }
28  
  
29  
  Year currentYear() {
30  
    ret new Year(year(now, tz));
31  
  }
32  
  
33  
  Month currentMonth() {
34  
    ret new Month(month(now, tz), currentYear());
35  
  }
36  
  
37  
  SomeDate concretizeDate(SomeDate d) {
38  
    if (d cast CurrentMonthPlus)
39  
      d = new Month(month(now, tz) + d.nMonths, currentYear());
40  
    if (d cast TodayPlus)
41  
      d = new Day(dayOfMonth(now, tz) + d.nDays, currentMonth());
42  
    if (d cast CurrentYearPlus)
43  
      d = new Year(year(now, tz) + d.nYears);
44  
    if (d cast CurrentWeekPlus)
45  
      d = new Week(weekInYear(now, tz) + d.nWeeks, currentYear());
46  
    ret d;
47  
  }
48  
  
49  
  LongRange getRange(SomeDate d) {
50  
    d = concretizeDate(d);
51  
    
52  
    // year
53  
    
54  
    if (d cast Year)
55  
      ret longRange(yearToTimestamp(d.year, tz), yearToTimestamp(d.year+1, tz));
56  
      
57  
    // month
58  
    
59  
    if (d cast Month) {
60  
      if (d.year == null)
61  
        d.year = (Year) concretizeDate(new CurrentYearPlus(d.month < currentMonth().month ? 1 : 0));
62  
      int year = d.year.year;
63  
      ret longRange(
64  
        yearAndMonthToTimestamp(year, d.month, tz),
65  
        yearAndMonthToTimestamp(year, d.month+1, tz));
66  
    }
67  
    
68  
    // week
69  
    
70  
    if (d cast Week) {
71  
      int year = d.year.year;
72  
      ret longRange(
73  
        yearAndWeekToTimestamp(year, d.week, tz),
74  
        yearAndWeekToTimestamp(year, d.week+1, tz));
75  
    }
76  
    
77  
    if (d cast Weekday) {
78  
      if (d.week == null) d.week = (Week) concretizeDate(new CurrentWeekPlus(d.weekday < dayOfWeek_nr(now, tz) ? 1 : 0));
79  
      long start = getRange(d.week).start+(d.weekday-1)*24*60*60*1000;
80  
      ret LongRange(start, start+24*60*60*1000);
81  
    }
82  
83  
    // day
84  
  
85  
    if (d cast Day) {
86  
      int month = d.month.month;
87  
      int year = d.month.year.year;
88  
      ret longRange(
89  
        ymdToTimestamp(year, month, d.day, tz),
90  
        ymdToTimestamp(year, month, d.day+1, tz));
91  
    }
92  
    
93  
    // hour
94  
    
95  
    if (d cast Hour) {
96  
      if (d.isPM == null) fail("AM/PM unknown: " + d);
97  
      long dayStart = getRange(assertNotNull("day of hour", d.day)).start;
98  
      int actualHour = d.hour+(d.isPM ? 12 : 0);
99  
      ret longRange(dayStart+hoursToMS(actualHour), dayStart+hoursToMS(actualHour+1));
100  
    }
101  
    
102  
    // between
103  
    
104  
    if (d cast Between) {
105  
      ret longRange(getRange(d.from).start, getRange(d.to).start);
106  
    }
107  
    
108  
    fail("dateStructureToTimestampRange: unknown type " + d);
109  
  }
110  
}

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: 359 / 576
Version history: 44 change(s)
Referenced in: [show references]