1 // Copyright 2017, The Gtk-rs Project Developers. 2 // See the COPYRIGHT file at the top-level directory of this distribution. 3 // Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT> 4 5 use glib_sys; 6 use gobject_sys; 7 use libc; 8 use std::cmp; 9 use std::fmt; 10 use std::hash; 11 use translate::*; 12 use DateDay; 13 use DateMonth; 14 use DateWeekday; 15 use DateYear; 16 use Time; 17 18 glib_wrapper! { 19 pub struct Date(Boxed<glib_sys::GDate>); 20 21 match fn { 22 copy => |ptr| gobject_sys::g_boxed_copy(glib_sys::g_date_get_type(), ptr as *const _) as *mut _, 23 free => |ptr| glib_sys::g_date_free(ptr), 24 init => |_ptr| (), 25 clear => |ptr| glib_sys::g_date_clear(ptr, 1), 26 get_type => || glib_sys::g_date_get_type(), 27 } 28 } 29 30 unsafe impl Send for Date {} 31 unsafe impl Sync for Date {} 32 33 impl Date { new() -> Date34 pub fn new() -> Date { 35 unsafe { from_glib_full(glib_sys::g_date_new()) } 36 } 37 new_dmy(day: DateDay, month: DateMonth, year: DateYear) -> Date38 pub fn new_dmy(day: DateDay, month: DateMonth, year: DateYear) -> Date { 39 unsafe { from_glib_full(glib_sys::g_date_new_dmy(day, month.to_glib(), year)) } 40 } 41 new_julian(julian_day: u32) -> Date42 pub fn new_julian(julian_day: u32) -> Date { 43 unsafe { from_glib_full(glib_sys::g_date_new_julian(julian_day)) } 44 } 45 add_days(&mut self, n_days: u32)46 pub fn add_days(&mut self, n_days: u32) { 47 unsafe { 48 glib_sys::g_date_add_days(self.to_glib_none_mut().0, n_days); 49 } 50 } 51 add_months(&mut self, n_months: u32)52 pub fn add_months(&mut self, n_months: u32) { 53 unsafe { 54 glib_sys::g_date_add_months(self.to_glib_none_mut().0, n_months); 55 } 56 } 57 add_years(&mut self, n_years: u32)58 pub fn add_years(&mut self, n_years: u32) { 59 unsafe { 60 glib_sys::g_date_add_years(self.to_glib_none_mut().0, n_years); 61 } 62 } 63 clamp(&mut self, min_date: &Date, max_date: &Date)64 pub fn clamp(&mut self, min_date: &Date, max_date: &Date) { 65 unsafe { 66 glib_sys::g_date_clamp( 67 self.to_glib_none_mut().0, 68 min_date.to_glib_none().0, 69 max_date.to_glib_none().0, 70 ); 71 } 72 } 73 clear(&mut self, n_dates: u32)74 pub fn clear(&mut self, n_dates: u32) { 75 unsafe { 76 glib_sys::g_date_clear(self.to_glib_none_mut().0, n_dates); 77 } 78 } 79 compare(&self, rhs: &Date) -> i3280 fn compare(&self, rhs: &Date) -> i32 { 81 unsafe { glib_sys::g_date_compare(self.to_glib_none().0, rhs.to_glib_none().0) } 82 } 83 days_between(&self, date2: &Date) -> i3284 pub fn days_between(&self, date2: &Date) -> i32 { 85 unsafe { glib_sys::g_date_days_between(self.to_glib_none().0, date2.to_glib_none().0) } 86 } 87 get_day(&self) -> DateDay88 pub fn get_day(&self) -> DateDay { 89 unsafe { glib_sys::g_date_get_day(self.to_glib_none().0) } 90 } 91 get_day_of_year(&self) -> u3292 pub fn get_day_of_year(&self) -> u32 { 93 unsafe { glib_sys::g_date_get_day_of_year(self.to_glib_none().0) } 94 } 95 get_iso8601_week_of_year(&self) -> u3296 pub fn get_iso8601_week_of_year(&self) -> u32 { 97 unsafe { glib_sys::g_date_get_iso8601_week_of_year(self.to_glib_none().0) } 98 } 99 get_julian(&self) -> u32100 pub fn get_julian(&self) -> u32 { 101 unsafe { glib_sys::g_date_get_julian(self.to_glib_none().0) } 102 } 103 get_monday_week_of_year(&self) -> u32104 pub fn get_monday_week_of_year(&self) -> u32 { 105 unsafe { glib_sys::g_date_get_monday_week_of_year(self.to_glib_none().0) } 106 } 107 get_month(&self) -> DateMonth108 pub fn get_month(&self) -> DateMonth { 109 unsafe { from_glib(glib_sys::g_date_get_month(self.to_glib_none().0)) } 110 } 111 get_sunday_week_of_year(&self) -> u32112 pub fn get_sunday_week_of_year(&self) -> u32 { 113 unsafe { glib_sys::g_date_get_sunday_week_of_year(self.to_glib_none().0) } 114 } 115 get_weekday(&self) -> DateWeekday116 pub fn get_weekday(&self) -> DateWeekday { 117 unsafe { from_glib(glib_sys::g_date_get_weekday(self.to_glib_none().0)) } 118 } 119 get_year(&self) -> DateYear120 pub fn get_year(&self) -> DateYear { 121 unsafe { glib_sys::g_date_get_year(self.to_glib_none().0) } 122 } 123 is_first_of_month(&self) -> bool124 pub fn is_first_of_month(&self) -> bool { 125 unsafe { from_glib(glib_sys::g_date_is_first_of_month(self.to_glib_none().0)) } 126 } 127 is_last_of_month(&self) -> bool128 pub fn is_last_of_month(&self) -> bool { 129 unsafe { from_glib(glib_sys::g_date_is_last_of_month(self.to_glib_none().0)) } 130 } 131 order(&mut self, date2: &mut Date)132 pub fn order(&mut self, date2: &mut Date) { 133 unsafe { 134 glib_sys::g_date_order(self.to_glib_none_mut().0, date2.to_glib_none_mut().0); 135 } 136 } 137 set_day(&mut self, day: DateDay)138 pub fn set_day(&mut self, day: DateDay) { 139 unsafe { 140 glib_sys::g_date_set_day(self.to_glib_none_mut().0, day); 141 } 142 } 143 set_dmy(&mut self, day: DateDay, month: DateMonth, y: DateYear)144 pub fn set_dmy(&mut self, day: DateDay, month: DateMonth, y: DateYear) { 145 unsafe { 146 glib_sys::g_date_set_dmy(self.to_glib_none_mut().0, day, month.to_glib(), y); 147 } 148 } 149 set_julian(&mut self, julian_date: u32)150 pub fn set_julian(&mut self, julian_date: u32) { 151 unsafe { 152 glib_sys::g_date_set_julian(self.to_glib_none_mut().0, julian_date); 153 } 154 } 155 set_month(&mut self, month: DateMonth)156 pub fn set_month(&mut self, month: DateMonth) { 157 unsafe { 158 glib_sys::g_date_set_month(self.to_glib_none_mut().0, month.to_glib()); 159 } 160 } 161 set_parse(&mut self, str: &str)162 pub fn set_parse(&mut self, str: &str) { 163 unsafe { 164 glib_sys::g_date_set_parse(self.to_glib_none_mut().0, str.to_glib_none().0); 165 } 166 } 167 set_time(&mut self, time_: Time)168 pub fn set_time(&mut self, time_: Time) { 169 unsafe { 170 glib_sys::g_date_set_time(self.to_glib_none_mut().0, time_); 171 } 172 } 173 set_time_t(&mut self, timet: libc::c_long)174 pub fn set_time_t(&mut self, timet: libc::c_long) { 175 unsafe { 176 glib_sys::g_date_set_time_t(self.to_glib_none_mut().0, timet); 177 } 178 } 179 180 //pub fn set_time_val(&mut self, timeval: /*Ignored*/&mut TimeVal) { 181 // unsafe { TODO: call glib_sys::g_date_set_time_val() } 182 //} 183 set_year(&mut self, year: DateYear)184 pub fn set_year(&mut self, year: DateYear) { 185 unsafe { 186 glib_sys::g_date_set_year(self.to_glib_none_mut().0, year); 187 } 188 } 189 subtract_days(&mut self, n_days: u32)190 pub fn subtract_days(&mut self, n_days: u32) { 191 unsafe { 192 glib_sys::g_date_subtract_days(self.to_glib_none_mut().0, n_days); 193 } 194 } 195 subtract_months(&mut self, n_months: u32)196 pub fn subtract_months(&mut self, n_months: u32) { 197 unsafe { 198 glib_sys::g_date_subtract_months(self.to_glib_none_mut().0, n_months); 199 } 200 } 201 subtract_years(&mut self, n_years: u32)202 pub fn subtract_years(&mut self, n_years: u32) { 203 unsafe { 204 glib_sys::g_date_subtract_years(self.to_glib_none_mut().0, n_years); 205 } 206 } 207 208 //pub fn to_struct_tm(&self, tm: /*Unimplemented*/Fundamental: Pointer) { 209 // unsafe { TODO: call glib_sys::g_date_to_struct_tm() } 210 //} 211 valid(&self) -> bool212 pub fn valid(&self) -> bool { 213 unsafe { from_glib(glib_sys::g_date_valid(self.to_glib_none().0)) } 214 } 215 get_days_in_month(month: DateMonth, year: DateYear) -> u8216 pub fn get_days_in_month(month: DateMonth, year: DateYear) -> u8 { 217 unsafe { glib_sys::g_date_get_days_in_month(month.to_glib(), year) } 218 } 219 get_monday_weeks_in_year(year: DateYear) -> u8220 pub fn get_monday_weeks_in_year(year: DateYear) -> u8 { 221 unsafe { glib_sys::g_date_get_monday_weeks_in_year(year) } 222 } 223 get_sunday_weeks_in_year(year: DateYear) -> u8224 pub fn get_sunday_weeks_in_year(year: DateYear) -> u8 { 225 unsafe { glib_sys::g_date_get_sunday_weeks_in_year(year) } 226 } 227 is_leap_year(year: DateYear) -> bool228 pub fn is_leap_year(year: DateYear) -> bool { 229 unsafe { from_glib(glib_sys::g_date_is_leap_year(year)) } 230 } 231 strftime(s: &str, format: &str, date: &Date) -> usize232 pub fn strftime(s: &str, format: &str, date: &Date) -> usize { 233 let slen = s.len() as usize; 234 unsafe { 235 glib_sys::g_date_strftime( 236 s.to_glib_none().0, 237 slen, 238 format.to_glib_none().0, 239 date.to_glib_none().0, 240 ) 241 } 242 } 243 valid_day(day: DateDay) -> bool244 pub fn valid_day(day: DateDay) -> bool { 245 unsafe { from_glib(glib_sys::g_date_valid_day(day)) } 246 } 247 valid_dmy(day: DateDay, month: DateMonth, year: DateYear) -> bool248 pub fn valid_dmy(day: DateDay, month: DateMonth, year: DateYear) -> bool { 249 unsafe { from_glib(glib_sys::g_date_valid_dmy(day, month.to_glib(), year)) } 250 } 251 valid_julian(julian_date: u32) -> bool252 pub fn valid_julian(julian_date: u32) -> bool { 253 unsafe { from_glib(glib_sys::g_date_valid_julian(julian_date)) } 254 } 255 valid_month(month: DateMonth) -> bool256 pub fn valid_month(month: DateMonth) -> bool { 257 unsafe { from_glib(glib_sys::g_date_valid_month(month.to_glib())) } 258 } 259 valid_weekday(weekday: DateWeekday) -> bool260 pub fn valid_weekday(weekday: DateWeekday) -> bool { 261 unsafe { from_glib(glib_sys::g_date_valid_weekday(weekday.to_glib())) } 262 } 263 valid_year(year: DateYear) -> bool264 pub fn valid_year(year: DateYear) -> bool { 265 unsafe { from_glib(glib_sys::g_date_valid_year(year)) } 266 } 267 } 268 269 impl Default for Date { default() -> Self270 fn default() -> Self { 271 Self::new() 272 } 273 } 274 275 impl PartialEq for Date { 276 #[inline] eq(&self, other: &Self) -> bool277 fn eq(&self, other: &Self) -> bool { 278 self.compare(other) == 0 279 } 280 } 281 282 impl Eq for Date {} 283 284 impl PartialOrd for Date { 285 #[inline] partial_cmp(&self, other: &Self) -> Option<cmp::Ordering>286 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 287 self.compare(other).partial_cmp(&0) 288 } 289 } 290 291 impl Ord for Date { 292 #[inline] cmp(&self, other: &Self) -> cmp::Ordering293 fn cmp(&self, other: &Self) -> cmp::Ordering { 294 self.compare(other).cmp(&0) 295 } 296 } 297 298 impl fmt::Debug for Date { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result299 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 300 f.debug_struct("Date") 301 .field("year", &self.get_year()) 302 .field("month", &self.get_month()) 303 .field("day", &self.get_day()) 304 .finish() 305 } 306 } 307 308 impl hash::Hash for Date { hash<H>(&self, state: &mut H) where H: hash::Hasher,309 fn hash<H>(&self, state: &mut H) 310 where 311 H: hash::Hasher, 312 { 313 self.get_year().hash(state); 314 self.get_month().hash(state); 315 self.get_day().hash(state); 316 } 317 } 318