@@ -47,6 +47,15 @@ impl Display for TimeUnit {
4747}
4848
4949impl TimeUnit {
50+ #[ inline]
51+ #[ cfg( feature = "cn_unit" ) ]
52+ fn is_cn_unit ( c : char ) -> bool {
53+ [
54+ '年' , '月' , '周' , '日' , '天' , '时' , '分' , '秒' , '毫' , '微' , '纳' ,
55+ ]
56+ . contains ( & c)
57+ }
58+
5059 pub ( crate ) fn duration ( & self , time_str : impl AsRef < str > ) -> DResult < u64 > {
5160 let time = time_str
5261 . as_ref ( )
@@ -93,7 +102,28 @@ impl FromStr for TimeUnit {
93102 "µs" | "µsec" | "µsecond" | "us" | "usec" | "usecond" | "microsecond"
94103 | "microseconds" => Ok ( TimeUnit :: MicroSecond ) ,
95104 "ns" | "nsec" | "nanosecond" | "nanoseconds" => Ok ( TimeUnit :: NanoSecond ) ,
96- _ => Err ( DError :: ParseError ( Self :: expect_err ( s) ) ) ,
105+
106+ #[ cfg( feature = "cn_unit" ) ]
107+ "年" => Ok ( TimeUnit :: Year ) ,
108+ #[ cfg( feature = "cn_unit" ) ]
109+ "月" => Ok ( TimeUnit :: Month ) ,
110+ #[ cfg( feature = "cn_unit" ) ]
111+ "周" => Ok ( TimeUnit :: Week ) ,
112+ #[ cfg( feature = "cn_unit" ) ]
113+ "日" | "天" => Ok ( TimeUnit :: Day ) ,
114+ #[ cfg( feature = "cn_unit" ) ]
115+ "时" => Ok ( TimeUnit :: Hour ) ,
116+ #[ cfg( feature = "cn_unit" ) ]
117+ "分" => Ok ( TimeUnit :: Minute ) ,
118+ #[ cfg( feature = "cn_unit" ) ]
119+ "秒" => Ok ( TimeUnit :: Second ) ,
120+ #[ cfg( feature = "cn_unit" ) ]
121+ "毫秒" => Ok ( TimeUnit :: MilliSecond ) ,
122+ #[ cfg( feature = "cn_unit" ) ]
123+ "微秒" => Ok ( TimeUnit :: MicroSecond ) ,
124+ #[ cfg( feature = "cn_unit" ) ]
125+ "纳秒" => Ok ( TimeUnit :: NanoSecond ) ,
126+ _ => Err ( DError :: ParseError ( Self :: expect_err ( case) ) ) ,
97127 }
98128 }
99129}
@@ -105,7 +135,17 @@ impl_expect_err!(
105135) ;
106136
107137pub ( crate ) fn unit_abbr1 ( input : & mut & str ) -> WResult < TimeUnit > {
108- take_while ( 1 .., |c : char | c. is_alpha ( ) || c == 'µ' )
138+ let set = |c : char | c. is_alpha ( ) || c == 'µ' ;
139+ let set = {
140+ #[ cfg( feature = "cn_unit" ) ]
141+ {
142+ move |c : char | set ( c) || TimeUnit :: is_cn_unit ( c)
143+ }
144+ #[ cfg( not( feature = "cn_unit" ) ) ]
145+ set
146+ } ;
147+
148+ take_while ( 1 .., set)
109149 . parse_to ( )
110150 . context ( StrContext :: Expected ( StrContextValue :: Description (
111151 TimeUnit :: get_expect_val ( ) ,
@@ -182,6 +222,55 @@ mod tests {
182222 ) ;
183223 }
184224
225+ #[ cfg( feature = "cn_unit" ) ]
226+ #[ test]
227+ fn test_time_cn_unit_abbr ( ) {
228+ assert_eq ! (
229+ unit_abbr1. parse_peek( & Partial :: new( "年" ) ) ,
230+ Ok ( ( "" , TimeUnit :: Year ) )
231+ ) ;
232+ assert_eq ! (
233+ unit_abbr1. parse_peek( & Partial :: new( "月" ) ) ,
234+ Ok ( ( "" , TimeUnit :: Month ) )
235+ ) ;
236+ assert_eq ! (
237+ unit_abbr1. parse_peek( & Partial :: new( "周" ) ) ,
238+ Ok ( ( "" , TimeUnit :: Week ) )
239+ ) ;
240+ assert_eq ! (
241+ unit_abbr1. parse_peek( & Partial :: new( "日" ) ) ,
242+ Ok ( ( "" , TimeUnit :: Day ) )
243+ ) ;
244+ assert_eq ! (
245+ unit_abbr1. parse_peek( & Partial :: new( "天" ) ) ,
246+ Ok ( ( "" , TimeUnit :: Day ) )
247+ ) ;
248+ assert_eq ! (
249+ unit_abbr1. parse_peek( & Partial :: new( "时" ) ) ,
250+ Ok ( ( "" , TimeUnit :: Hour ) )
251+ ) ;
252+ assert_eq ! (
253+ unit_abbr1. parse_peek( & Partial :: new( "分" ) ) ,
254+ Ok ( ( "" , TimeUnit :: Minute ) )
255+ ) ;
256+ assert_eq ! (
257+ unit_abbr1. parse_peek( & Partial :: new( "秒" ) ) ,
258+ Ok ( ( "" , TimeUnit :: Second ) )
259+ ) ;
260+ assert_eq ! (
261+ unit_abbr1. parse_peek( & Partial :: new( "毫秒" ) ) ,
262+ Ok ( ( "" , TimeUnit :: MilliSecond ) )
263+ ) ;
264+ assert_eq ! (
265+ unit_abbr1. parse_peek( & Partial :: new( "微秒" ) ) ,
266+ Ok ( ( "" , TimeUnit :: MicroSecond ) )
267+ ) ;
268+ assert_eq ! (
269+ unit_abbr1. parse_peek( & Partial :: new( "纳秒" ) ) ,
270+ Ok ( ( "" , TimeUnit :: NanoSecond ) )
271+ ) ;
272+ }
273+
185274 #[ test]
186275 fn test_time_unit ( ) {
187276 let ( input, format) = unit_abbr1. parse_peek ( "m123" ) . unwrap ( ) ;
0 commit comments