1 use webcore::value::Reference; 2 use webcore::try_from::TryInto; 3 4 /// [(JavaScript docs)](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date) 5 /// https://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor 6 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)] 7 #[reference(instance_of = "Date")] 8 pub struct Date( Reference ); 9 10 impl Date { 11 /// Creates a JavaScript Date instance that represents a single moment in time. 12 /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. 13 /// 14 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) 15 // https://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor-date new() -> Self16 pub fn new() -> Self { 17 js!( 18 return new Date(); 19 ).try_into().unwrap() 20 } 21 22 23 /// Creates a JavaScript Date instance that represents a single moment in time. 24 /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. 25 /// 26 /// year is an integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. 27 /// month is an integer value representing the month, beginning with 0 for January to 11 for December 28 /// day is an integer value representing the day of the month (normally from 1 to 31) 29 /// hours an integer value representing the minute segment of a time 30 /// seconds an integer value representing the second segment of a time 31 /// milliseconds an integer value representing the millisecond segment of a time 32 /// 33 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) 34 // https://www.ecma-international.org/ecma-262/6.0/#sec-date-year-month-date-hours-minutes-seconds-ms from_datetime(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> Self35 pub fn from_datetime(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> Self { 36 js!( 37 return new Date(@{year}, @{month}, @{day}, @{hours}, @{minutes}, @{seconds}, @{milliseconds}); 38 ).try_into().unwrap() 39 } 40 41 /// Creates a JavaScript Date instance that represents a single moment in time. 42 /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. 43 /// 44 /// String value representing a date. The string should be in a format recognized by 45 /// the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). 46 /// 47 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) 48 // https://www.ecma-international.org/ecma-262/6.0/#sec-date-value from_iso8601(date_string: &str) -> Self49 pub fn from_iso8601(date_string: &str) -> Self { 50 js!( 51 return new Date(@{date_string}); 52 ).try_into().unwrap() 53 } 54 55 /// Creates a JavaScript Date instance that represents a single moment in time. 56 /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. 57 /// 58 /// Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, 59 /// with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds). 60 /// 61 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) 62 // https://www.ecma-international.org/ecma-262/6.0/#sec-date-value from_time(now: f64) -> Self63 pub fn from_time(now: f64) -> Self { 64 js!( 65 return new Date(@{now}); 66 ).try_into().unwrap() 67 } 68 69 /// The Date.UTC() method accepts the same parameters as the longest form of the constructor, and 70 /// returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. 71 /// 72 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) 73 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.utc utc(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> f6474 pub fn utc(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> f64 { 75 js!( 76 return Date.UTC(@{year}, @{month}, @{day}, @{hours}, @{minutes}, @{seconds}, @{milliseconds}); 77 ).try_into().unwrap() 78 } 79 80 /// The Date.parse() method parses a string representation of a date, and returns the number of 81 /// milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in 82 /// some cases, contains illegal date values (e.g. 2015-02-31). 83 /// 84 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) 85 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.parse parse(date_string: &str) -> f6486 pub fn parse(date_string: &str) -> f64 { 87 js!( 88 return Date.parse(@{date_string}); 89 ).try_into().unwrap() 90 } 91 92 /// The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. 93 /// 94 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) 95 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.now now() -> f6496 pub fn now() -> f64 { 97 js!( 98 return Date.now(); 99 ).try_into().unwrap() 100 } 101 102 /// The getDate() method returns the day of the month for the specified date according to local time. 103 /// 104 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate) 105 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getdate get_date(&self) -> i32106 pub fn get_date(&self) -> i32 { 107 js!( 108 return @{self}.getDate(); 109 ).try_into().unwrap() 110 } 111 112 /// The getDay() method returns the day of the week for the specified date according to local time, 113 /// where 0 represents Sunday. For the day of the month see getDate(). 114 /// 115 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) 116 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getday get_day(&self) -> i32117 pub fn get_day(&self) -> i32 { 118 js!( 119 return @{self}.getDay(); 120 ).try_into().unwrap() 121 } 122 123 /// The getFullYear() method returns the year of the specified date according to local time. 124 /// 125 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) 126 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getfullyear get_full_year(&self) -> i32127 pub fn get_full_year(&self) -> i32 { 128 js!( 129 return @{self}.getFullYear(); 130 ).try_into().unwrap() 131 } 132 133 /// The getHours() method returns the hour for the specified date, according to local time. 134 /// 135 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours) 136 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gethours get_hours(&self) -> i32137 pub fn get_hours(&self) -> i32 { 138 js!( 139 return @{self}.getHours(); 140 ).try_into().unwrap() 141 } 142 143 /// The getMilliseconds() method returns the milliseconds in the specified date according to local time. 144 /// 145 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds) 146 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getmilliseconds get_milliseconds(&self) -> i32147 pub fn get_milliseconds(&self) -> i32 { 148 js!( 149 return @{self}.getMilliseconds(); 150 ).try_into().unwrap() 151 } 152 153 /// The getMinutes() method returns the minutes in the specified date according to local time. 154 /// 155 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes) 156 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getminutes get_minutes(&self) -> i32157 pub fn get_minutes(&self) -> i32 { 158 js!( 159 return @{self}.getMinutes(); 160 ).try_into().unwrap() 161 } 162 163 /// The getMonth() method returns the month in the specified date according to local time, as a 164 /// zero-based value (where zero indicates the first month of the year). 165 /// 166 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth) 167 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getmonth get_month(&self) -> i32168 pub fn get_month(&self) -> i32 { 169 js!( 170 return @{self}.getMonth(); 171 ).try_into().unwrap() 172 } 173 174 /// The getSeconds() method returns the seconds in the specified date according to local time. 175 /// 176 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getSeconds) 177 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getseconds get_seconds(&self) -> i32178 pub fn get_seconds(&self) -> i32 { 179 js!( 180 return @{self}.getSeconds(); 181 ).try_into().unwrap() 182 } 183 184 /// The getTime() method returns the numeric value corresponding to the time for the specified 185 /// date according to universal time. 186 /// 187 /// getTime() always uses UTC for time representation. For example, a client browser in one timezone, 188 /// getTime() will be the same as a client browser in any other timezone. 189 /// 190 /// You can use this method to help assign a date and time to another Date object. This method is 191 /// functionally equivalent to the valueOf() method. 192 /// 193 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) 194 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gettime get_time(&self) -> f64195 pub fn get_time(&self) -> f64 { 196 js!( 197 return @{self}.getTime(); 198 ).try_into().unwrap() 199 } 200 201 /// The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC. 202 /// 203 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset) 204 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gettimezoneoffset get_timezone_offset(&self) -> i32205 pub fn get_timezone_offset(&self) -> i32 { 206 js!( 207 return @{self}.getTimezoneOffset(); 208 ).try_into().unwrap() 209 } 210 211 /// The getUTCDate() method returns the day (date) of the month in the specified date according to 212 /// universal time. 213 /// 214 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate) 215 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcdate get_utc_date(&self) -> i32216 pub fn get_utc_date(&self) -> i32 { 217 js!( 218 return @{self}.getUTCDate(); 219 ).try_into().unwrap() 220 } 221 222 /// The getUTCDay() method returns the day of the week in the specified date according to universal 223 /// time, where 0 represents Sunday. 224 /// 225 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDay) 226 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcday get_utc_day(&self) -> i32227 pub fn get_utc_day(&self) -> i32 { 228 js!( 229 return @{self}.getUTCDay(); 230 ).try_into().unwrap() 231 } 232 233 /// The getUTCFullYear() method returns the year in the specified date according to universal time. 234 /// 235 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear) 236 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcfullyear get_utc_full_year(&self) -> i32237 pub fn get_utc_full_year(&self) -> i32 { 238 js!( 239 return @{self}.getUTCFullYear(); 240 ).try_into().unwrap() 241 } 242 243 /// The getUTCHours() method returns the hours in the specified date according to universal time. 244 /// 245 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours) 246 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutchours get_utc_hours(&self) -> i32247 pub fn get_utc_hours(&self) -> i32 { 248 js!( 249 return @{self}.getUTCHours(); 250 ).try_into().unwrap() 251 } 252 253 /// The getUTCMilliseconds() method returns the milliseconds in the specified date according to 254 /// universal time. 255 /// 256 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMilliseconds) 257 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcmilliseconds get_utc_milliseconds(&self) -> i32258 pub fn get_utc_milliseconds(&self) -> i32 { 259 js!( 260 return @{self}.getUTCMilliseconds(); 261 ).try_into().unwrap() 262 } 263 264 /// The getUTCMinutes() method returns the minutes in the specified date according to universal time. 265 /// 266 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMinutes) 267 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcminutes get_utc_minutes(&self) -> i32268 pub fn get_utc_minutes(&self) -> i32 { 269 js!( 270 return @{self}.getUTCMinutes(); 271 ).try_into().unwrap() 272 } 273 274 /// The getUTCMonth() returns the month of the specified date according to universal time, as a 275 /// zero-based value (where zero indicates the first month of the year). 276 /// 277 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth) 278 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcmonth get_utc_month(&self) -> i32279 pub fn get_utc_month(&self) -> i32 { 280 js!( 281 return @{self}.getUTCMonth(); 282 ).try_into().unwrap() 283 } 284 285 /// The getUTCSeconds() method returns the seconds in the specified date according to universal time. 286 /// 287 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCSeconds) 288 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcseconds get_utc_seconds(&self) -> i32289 pub fn get_utc_seconds(&self) -> i32 { 290 js!( 291 return @{self}.getUTCSeconds(); 292 ).try_into().unwrap() 293 } 294 295 /// The setDate() method sets the day of the Date object relative to the beginning of the currently set month. 296 /// 297 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate) 298 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setdate set_date(&self, date: i32)299 pub fn set_date(&self, date: i32) { 300 js!{ @(no_return) 301 @{self}.setDate(@{date}); 302 } 303 } 304 305 /// The setFullYear() method sets the full year for a specified date according to local time. Returns new timestamp. 306 /// 307 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear) 308 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setfullyear set_full_year(&self, full_year: i32)309 pub fn set_full_year(&self, full_year: i32) { 310 js!{ @(no_return) 311 @{self}.setFullYear(@{full_year}); 312 } 313 } 314 315 /// The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds 316 /// since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance. 317 /// 318 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours) 319 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.sethours set_hours(&self, hours: i32)320 pub fn set_hours(&self, hours: i32) { 321 js!{ @(no_return) 322 @{self}.setHours(@{hours}); 323 } 324 } 325 326 /// The setMilliseconds() method sets the milliseconds for a specified date according to local time. 327 /// 328 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMilliseconds) 329 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setmilliseconds set_milliseconds(&self, milliseconds: i32)330 pub fn set_milliseconds(&self, milliseconds: i32) { 331 js!{ @(no_return) 332 @{self}.setMilliseconds(@{milliseconds}); 333 } 334 } 335 336 /// The setMinutes() method sets the minutes for a specified date according to local time. 337 /// 338 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMinutes) 339 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setminutes set_minutes(&self, minutes: i32)340 pub fn set_minutes(&self, minutes: i32) { 341 js!{ @(no_return) 342 @{self}.setMinutes(@{minutes}); 343 } 344 } 345 346 /// The setMonth() method sets the month for a specified date according to the currently set year. 347 /// 348 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth) 349 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setmonth set_month(&self, month: i32)350 pub fn set_month(&self, month: i32) { 351 js!{ @(no_return) 352 @{self}.setMonth(@{month}); 353 } 354 } 355 356 /// The setSeconds() method sets the seconds for a specified date according to local time. 357 /// 358 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setSeconds) 359 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setseconds set_seconds(&self, seconds: i32)360 pub fn set_seconds(&self, seconds: i32) { 361 js!{ @(no_return) 362 @{self}.setSeconds(@{seconds}); 363 } 364 } 365 366 /// The setTime() method sets the Date object to the time represented by a number of milliseconds since 367 /// January 1, 1970, 00:00:00 UTC. 368 /// 369 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setTime) 370 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.settime set_time(&self, time: f64)371 pub fn set_time(&self, time: f64) { 372 js!{ @(no_return) 373 @{self}.setTime(@{time}); 374 } 375 } 376 377 /// The setUTCDate() method sets the day of the month for a specified date according to universal time. 378 /// 379 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCDate) 380 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcdate set_utc_date(&self, date: i32)381 pub fn set_utc_date(&self, date: i32) { 382 js!{ @(no_return) 383 @{self}.setUTCDate(@{date}); 384 } 385 } 386 387 /// The setUTCFullYear() method sets the full year for a specified date according to universal time. 388 /// 389 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear) 390 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcfullyear set_utc_full_year(&self, full_year: i32)391 pub fn set_utc_full_year(&self, full_year: i32) { 392 js!{ @(no_return) 393 @{self}.setUTCFullYear(@{full_year}); 394 } 395 } 396 397 /// The setUTCHours() method sets the hour for a specified date according to universal time, and returns the number 398 /// of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance. 399 /// 400 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours) 401 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutchours set_utc_hours(&self, hours: i32)402 pub fn set_utc_hours(&self, hours: i32) { 403 js!{ @(no_return) 404 @{self}.setUTCHours(@{hours}); 405 } 406 } 407 408 /// The setUTCMilliseconds() method sets the milliseconds for a specified date according to universal time. 409 /// 410 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds) 411 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcmilliseconds set_utc_milliseconds(&self, milliseconds: i32)412 pub fn set_utc_milliseconds(&self, milliseconds: i32) { 413 js!{ @(no_return) 414 @{self}.setUTCMilliseconds(@{milliseconds}); 415 } 416 } 417 418 /// The setUTCMinutes() method sets the minutes for a specified date according to universal time. 419 /// 420 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes) 421 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcminutes set_utc_minutes(&self, minutes: i32)422 pub fn set_utc_minutes(&self, minutes: i32) { 423 js!{ @(no_return) 424 @{self}.setUTCMinutes(@{minutes}); 425 } 426 } 427 428 /// The setUTCMonth() method sets the month for a specified date according to universal time. 429 /// 430 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth) 431 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcmonth set_utc_month(&self, month: i32)432 pub fn set_utc_month(&self, month: i32) { 433 js!{ @(no_return) 434 @{self}.setUTCMonth(@{month}); 435 } 436 } 437 438 /// The setUTCSeconds() method sets the seconds for a specified date according to universal time. 439 /// 440 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds) 441 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcseconds set_utc_seconds(&self, seconds: i32)442 pub fn set_utc_seconds(&self, seconds: i32) { 443 js!{ @(no_return) 444 @{self}.setUTCSeconds(@{seconds}); 445 } 446 } 447 448 /// The toDateString() method returns the date portion of a Date object in human readable form in American English. 449 /// 450 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) 451 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.todatestring 452 #[inline] to_date_string(&self) -> String453 pub fn to_date_string(&self) -> String { 454 js!( 455 return @{self}.toDateString(); 456 ).try_into().unwrap() 457 } 458 459 /// The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 460 /// characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero 461 /// UTC offset, as denoted by the suffix "Z". 462 /// 463 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) 464 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.toisostring 465 #[inline] to_iso_string(&self) -> String466 pub fn to_iso_string(&self) -> String { 467 js!( 468 return @{self}.toISOString(); 469 ).try_into().unwrap() 470 } 471 472 /// The toJSON() method returns a string representation of the Date object. 473 /// 474 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) 475 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.tojson 476 #[inline] to_json(&self) -> String477 pub fn to_json(&self) -> String { 478 js!( 479 return @{self}.toJSON(); 480 ).try_into().unwrap() 481 } 482 483 /// The toString() method returns a string representing the specified Date object. 484 /// 485 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) 486 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.tostring 487 #[inline] to_string(&self) -> String488 pub fn to_string(&self) -> String { 489 js!( 490 return @{self}.toString(); 491 ).try_into().unwrap() 492 } 493 494 /// The toTimeString() method returns the time portion of a Date object in human readable form in American English. 495 /// 496 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toTimeString) 497 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.totimestring 498 #[inline] to_time_string(&self) -> String499 pub fn to_time_string(&self) -> String { 500 js!( 501 return @{self}.toTimeString(); 502 ).try_into().unwrap() 503 } 504 505 /// The toUTCString() method converts a date to a string, using the UTC time zone. 506 /// 507 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) 508 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.toutcstring 509 #[inline] to_utc_string(&self) -> String510 pub fn to_utc_string(&self) -> String { 511 js!( 512 return @{self}.toUTCString(); 513 ).try_into().unwrap() 514 } 515 516 /// The valueOf() method returns the primitive value of a Date object. 517 /// 518 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf) 519 // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.valueof value_of(&self) -> f64520 pub fn value_of(&self) -> f64 { 521 js!( 522 return @{self}.valueOf(); 523 ).try_into().unwrap() 524 } 525 } 526 527 #[cfg(test)] 528 mod tests { 529 use super::*; 530 531 #[test] test_date_now()532 fn test_date_now() { 533 let now = Date::now(); 534 assert!( now > 0.0 ); 535 } 536 537 #[test] test_date_utc()538 fn test_date_utc() { 539 let now = Date::utc(96, 1, 2, 3, 4, 5, 0); 540 assert_eq!(now, 823230245000.0); 541 542 let now = Date::utc(0, 0, 0, 0, 0, 0, 0); 543 assert_eq!(now, -2209075200000.0); 544 } 545 546 #[test] test_date_parse()547 fn test_date_parse() { 548 let now = Date::parse("01 Jan 1970 00:00:00 GMT"); 549 assert_eq!(now, 0.0); 550 551 let now = Date::parse("04 Dec 1995 00:12:00 GMT"); 552 assert_eq!(now, 818035920000.0); 553 } 554 555 #[test] test_date_get_date()556 fn test_date_get_date() { 557 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 558 assert_eq!( now.get_date(), 19); 559 } 560 561 #[test] test_date_get_day()562 fn test_date_get_day() { 563 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 564 assert_eq!(now.get_day(), 2); 565 } 566 567 #[test] test_date_get_full_year()568 fn test_date_get_full_year() { 569 let now = Date::from_iso8601("August 19, 75 23:15:30"); 570 assert_eq!(now.get_full_year(), 1975); 571 } 572 573 #[test] test_date_get_hours()574 fn test_date_get_hours() { 575 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 576 assert_eq!(now.get_hours(), 23); 577 } 578 579 #[test] test_date_get_milliseconds()580 fn test_date_get_milliseconds() { 581 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 582 now.set_milliseconds(123); 583 assert_eq!(now.get_milliseconds(), 123); 584 } 585 586 #[test] test_date_get_minutes()587 fn test_date_get_minutes() { 588 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 589 assert_eq!(now.get_minutes(), 15); 590 } 591 592 #[test] test_date_get_month()593 fn test_date_get_month() { 594 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 595 assert_eq!(now.get_month(), 7); 596 } 597 598 #[test] test_date_get_seconds()599 fn test_date_get_seconds() { 600 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 601 assert_eq!(now.get_seconds(), 30); 602 } 603 604 #[test] test_date_get_time()605 fn test_date_get_time() { 606 let now = Date::from_iso8601("July 20, 69 00:20:18 GMT+00:00"); 607 assert_eq!(now.get_time(), -14254782000.0); 608 } 609 610 #[test] test_date_get_timezone_offset()611 fn test_date_get_timezone_offset() { 612 // this is impossible to test like this, since this function depends on local time only. 613 // and there is no easy way to mock the system time, so the only real thing to check 614 // is that two dates return the same timezone offset. 615 let t1 = Date::from_iso8601("August 19, 1975 23:15:30 GMT+07:00"); 616 let t2 = Date::from_iso8601("August 19, 1975 23:15:30 GMT-02:00"); 617 assert_eq!(t1.get_timezone_offset(), t2.get_timezone_offset()); 618 } 619 620 #[test] test_date_get_utc_date()621 fn test_date_get_utc_date() { 622 let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT+11:00"); 623 assert_eq!(now.get_utc_date(), 19); 624 let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT-11:00"); 625 assert_eq!(now.get_utc_date(), 20); 626 } 627 628 #[test] test_date_get_utc_day()629 fn test_date_get_utc_day() { 630 let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT+11:00"); 631 assert_eq!( now.get_utc_day(), 2 ); 632 let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT-11:00"); 633 assert_eq!( now.get_utc_day(), 3 ); 634 } 635 636 #[test] test_date_get_utc_full_year()637 fn test_date_get_utc_full_year() { 638 let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT+11:00"); 639 assert_eq!(now.get_utc_full_year(), 1975 ); 640 let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT-11:00"); 641 assert_eq!(now.get_utc_full_year(), 1976 ); 642 } 643 644 #[test] test_date_get_utc_milliseconds()645 fn test_date_get_utc_milliseconds() { 646 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 647 now.set_milliseconds(123); 648 assert_eq!(now.get_utc_milliseconds(), 123); 649 } 650 651 #[test] test_date_get_utc_minutes()652 fn test_date_get_utc_minutes() { 653 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 654 assert_eq!(now.get_utc_minutes(), 15); 655 } 656 657 #[test] test_date_get_utc_month()658 fn test_date_get_utc_month() { 659 let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT+11:00"); 660 assert_eq!(now.get_utc_month(), 11); 661 let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT-11:00"); 662 assert_eq!(now.get_utc_month(), 0); 663 } 664 665 #[test] test_date_set_date()666 fn test_date_set_date() { 667 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 668 now.set_date(3); 669 assert_eq!(now.get_date(), 3); 670 } 671 672 #[test] test_date_set_full_year()673 fn test_date_set_full_year() { 674 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 675 now.set_full_year(1969); 676 assert_eq!(now.get_full_year(), 1969); 677 } 678 679 #[test] test_date_set_hours()680 fn test_date_set_hours() { 681 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 682 now.set_hours(15); 683 assert_eq!(now.get_hours(), 15); 684 } 685 686 #[test] test_date_set_milliseconds()687 fn test_date_set_milliseconds() { 688 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 689 now.set_milliseconds(123); 690 assert_eq!(now.get_milliseconds(), 123); 691 } 692 693 #[test] test_date_set_minutes()694 fn test_date_set_minutes() { 695 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 696 now.set_minutes(42); 697 assert_eq!(now.get_minutes(), 42); 698 } 699 700 #[test] test_date_set_month()701 fn test_date_set_month() { 702 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 703 now.set_month(9); 704 assert_eq!(now.get_month(), 9); 705 } 706 707 #[test] test_date_set_seconds()708 fn test_date_set_seconds() { 709 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 710 now.set_seconds(59); 711 assert_eq!(now.get_seconds(), 59); 712 } 713 714 #[test] test_date_set_time()715 fn test_date_set_time() { 716 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 717 now.set_time(818035920000.0); 718 assert_eq!(now.to_utc_string(), "Mon, 04 Dec 1995 00:12:00 GMT"); 719 } 720 721 #[test] test_date_set_utc_date()722 fn test_date_set_utc_date() { 723 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 724 now.set_utc_date(3); 725 assert_eq!(now.get_utc_date(), 3); 726 } 727 728 #[test] test_date_set_utc_full_year()729 fn test_date_set_utc_full_year() { 730 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 731 now.set_utc_full_year(1969); 732 assert_eq!(now.get_utc_full_year(), 1969); 733 } 734 735 #[test] test_date_set_utc_hours()736 fn test_date_set_utc_hours() { 737 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 738 now.set_utc_hours(15); 739 assert_eq!(now.get_utc_hours(), 15); 740 } 741 742 #[test] test_date_set_utc_milliseconds()743 fn test_date_set_utc_milliseconds() { 744 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 745 now.set_utc_milliseconds(123); 746 assert_eq!(now.get_utc_milliseconds(), 123); 747 } 748 749 #[test] test_date_set_utc_minutes()750 fn test_date_set_utc_minutes() { 751 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 752 now.set_utc_minutes(42); 753 assert_eq!(now.get_utc_minutes(), 42); 754 } 755 756 #[test] test_date_set_utc_month()757 fn test_date_set_utc_month() { 758 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 759 now.set_utc_month(9); 760 assert_eq!(now.get_utc_month(), 9); 761 } 762 763 #[test] test_date_set_utc_seconds()764 fn test_date_set_utc_seconds() { 765 let now = Date::from_iso8601("August 19, 1975 23:15:30"); 766 now.set_utc_seconds(59); 767 assert_eq!(now.get_utc_seconds(), 59); 768 } 769 770 #[test] test_date_to_date_string()771 fn test_date_to_date_string() { 772 let now = Date::from_datetime(1993, 6, 28, 14, 39, 7, 0); 773 assert_eq!(now.to_date_string(), "Wed Jul 28 1993"); 774 } 775 776 #[test] test_date_to_iso_string()777 fn test_date_to_iso_string() { 778 let now = Date::from_iso8601("05 October 2011 14:48 UTC"); 779 assert_eq!(now.to_iso_string(), "2011-10-05T14:48:00.000Z"); 780 } 781 782 #[test] test_date_to_json()783 fn test_date_to_json() { 784 let now = Date::from_iso8601("August 19, 1975 23:15:30 UTC"); 785 assert_eq!(now.to_iso_string(), "1975-08-19T23:15:30.000Z"); 786 } 787 788 #[test] test_date_to_time_string()789 fn test_date_to_time_string() { 790 // not easy to test this due to time-zones 791 } 792 793 #[test] test_date_to_utc_string()794 fn test_date_to_utc_string() { 795 let now = Date::from_time(Date::utc(96, 1, 2, 3, 4, 5, 0)); 796 assert_eq!(now.to_utc_string(), "Fri, 02 Feb 1996 03:04:05 GMT"); 797 } 798 799 #[test] test_date_value_of()800 fn test_date_value_of() { 801 let now = Date::from_time(Date::utc(96, 1, 2, 3, 4, 5, 0)); 802 assert_eq!(now.value_of(), 823230245000.0); 803 } 804 805 }