summaryrefslogtreecommitdiffstats
path: root/DDate.vala
blob: 7f9654dfd2bdb921ea80f27f6e976086ef5108e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using GLib;

public enum DDateMonth {
  CHAOS = 0, DISCORD = 1, CONFUSION = 2, BUREAUCRACY = 3, THE_AFTERMATH = 4, ST_TIBS_DAY;
  
  public string to_short_string() {
    switch(this) {
    case CHAOS:
      return "Chs";
    case DISCORD:
      return "Dsc";
    case CONFUSION:
      return "Cfn";
    case BUREAUCRACY:
      return "Bcy";
    case THE_AFTERMATH:
      return "Afm";
    default:
      return "St. Tib's Day";
    }
  }
}

public enum DDateWeekday {
  SWEETMORN = 0, BOOMTIME = 1, PUNGENDAY = 2, PRICKLE_PRICKLE = 3, SETTING_ORANGE = 4, ST_TIBS_DAY;
  
  public string to_short_string() {
    switch(this) {
    case SWEETMORN:
      return "SM";
    case BOOMTIME:
      return "BT";
    case PUNGENDAY:
      return "PD";
    case PRICKLE_PRICKLE:
      return "PP";
    case SETTING_ORANGE:
      return "SO";
    default:
      return "St. Tib's Day";
    }
  }
}

public struct DDate {
  private GLib.Date date;
  
  private uint get_day_of_year() {
    if (date.get_year().is_leap_year() && date.get_day_of_year() > 60)
      return date.get_day_of_year()-1;
    else
      return date.get_day_of_year();
  }
  
  public DDate(GLib.Date date0) {
    date = date0;
  }
  
  public DDate.with_time_t(time_t time) {
    date = Date();
    date.set_time_t(time);
  }
  
  public bool is_st_tibs_day() {
    return (date.get_month() == GLib.DateMonth.FEBRUARY && date.get_day() == 29);
  }
  
  public uchar get_day() {
    return (uchar)(((get_day_of_year()-1) % 73) + 1);
  }
  
  public DDateMonth get_month() {
    if(is_st_tibs_day())
      return DDateMonth.ST_TIBS_DAY;
    
    return (DDateMonth)((get_day_of_year()-1)/73);
  }
  
  public DDateWeekday get_weekday() {
    if(is_st_tibs_day())
      return DDateWeekday.ST_TIBS_DAY;
    
    return (DDateWeekday)((get_day_of_year()-1)%5);
  }
  
  public string to_string() {
    string ret = "";
    
    if(is_st_tibs_day()) {
      ret = "St. Tib's Day";
    }
    else {
      ret = get_weekday().to_short_string() + ", " + get_day().to_string() + ". " + get_month().to_short_string();
    }
    
    return ret;
  }
}