File tree Expand file tree Collapse file tree 5 files changed +56
-13
lines changed Expand file tree Collapse file tree 5 files changed +56
-13
lines changed Original file line number Diff line number Diff line change 1+ //! Hints to the compiler that affects how code should be emitted or optimized.
2+
3+ #![ allow( dead_code) ] // may be used in the future and has minimal overhead
4+
5+ /// Indicate that a given branch is **not** likely to be taken, relatively speaking.
6+ #[ inline( always) ]
7+ #[ cold]
8+ pub ( crate ) const fn cold_path ( ) { }
9+
10+ /// Indicate that a given condition is likely to be true.
11+ #[ inline( always) ]
12+ pub ( crate ) const fn likely ( b : bool ) -> bool {
13+ if !b {
14+ cold_path ( ) ;
15+ }
16+ b
17+ }
18+
19+ /// Indicate that a given condition is likely to be false.
20+ #[ inline( always) ]
21+ pub ( crate ) const fn unlikely ( b : bool ) -> bool {
22+ if b {
23+ cold_path ( ) ;
24+ }
25+ b
26+ }
Original file line number Diff line number Diff line change 88#![ doc( test( attr( deny( warnings) ) ) ) ]
99
1010pub mod convert;
11+ mod hint;
1112pub mod util;
Original file line number Diff line number Diff line change 11//! Utility functions.
22
3+ use crate :: hint;
4+
35/// Returns if the provided year is a leap year in the proleptic Gregorian calendar. Uses
46/// [astronomical year numbering](https://en.wikipedia.org/wiki/Astronomical_year_numbering).
57///
@@ -61,3 +63,26 @@ pub const fn weeks_in_year(year: i32) -> u8 {
6163 _ => 52 ,
6264 }
6365}
66+
67+ /// Get the number of days in the month of a given year.
68+ ///
69+ /// ```rust
70+ /// # use time_core::util::days_in_month;
71+ /// assert_eq!(days_in_month(2, 2020), 29);
72+ /// ```
73+ ///
74+ /// Note: This function is not exposed by the `time` crate. It is an implementation detail.
75+ pub const fn days_in_month ( month : u8 , year : i32 ) -> u8 {
76+ debug_assert ! ( month >= 1 ) ;
77+ debug_assert ! ( month <= 12 ) ;
78+
79+ if hint:: unlikely ( month == 2 ) {
80+ if is_leap_year ( year) {
81+ 29
82+ } else {
83+ 28
84+ }
85+ } else {
86+ 30 | month ^ ( month >> 3 )
87+ }
88+ }
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ use core::str::FromStr;
77use powerfmt:: smart_display:: { FormatterOptions , Metadata , SmartDisplay } ;
88
99use self :: Month :: * ;
10- use crate :: { error, hint , util} ;
10+ use crate :: { error, util} ;
1111
1212/// Months of the year.
1313#[ repr( u8 ) ]
@@ -72,16 +72,7 @@ impl Month {
7272 /// assert_eq!(Month::February.length(2020), 29);
7373 /// ```
7474 pub const fn length ( self , year : i32 ) -> u8 {
75- let val = self as u8 ;
76- if hint:: unlikely ( val == 2 ) {
77- if util:: is_leap_year ( year) {
78- 29
79- } else {
80- 28
81- }
82- } else {
83- 30 | val ^ ( val >> 3 )
84- }
75+ util:: days_in_month ( self , year)
8576 }
8677
8778 /// Get the previous month.
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ pub(crate) enum DateAdjustment {
2121/// assert_eq!(util::days_in_month(Month::February, 2020), 29);
2222/// ```
2323pub const fn days_in_month ( month : Month , year : i32 ) -> u8 {
24- month . length ( year)
24+ time_core :: util :: days_in_month ( month as u8 , year)
2525}
2626
2727/// Get the number of days in the month of a given year.
@@ -36,7 +36,7 @@ pub const fn days_in_month(month: Month, year: i32) -> u8 {
3636 note = "use `days_in_month` or `Month::length` instead"
3737) ]
3838pub const fn days_in_year_month ( year : i32 , month : Month ) -> u8 {
39- month . length ( year)
39+ days_in_month ( month , year)
4040}
4141
4242/// Update time zone information from the system.
You can’t perform that action at this time.
0 commit comments