File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 1
1
use std:: io;
2
+ use std:: num:: NonZeroU8 ;
2
3
3
4
use time:: format_description:: well_known:: iso8601:: { DateKind , OffsetPrecision , TimePrecision } ;
4
5
use time:: format_description:: well_known:: { iso8601, Iso8601 , Rfc2822 , Rfc3339 } ;
@@ -226,6 +227,26 @@ fn iso_8601() -> time::Result<()> {
226
227
Ok ( ( ) )
227
228
}
228
229
230
+ #[ test]
231
+ fn iso_8601_issue_678 ( ) -> time:: Result < ( ) > {
232
+ macro_rules! assert_format_config {
233
+ ( $formatted: literal $( , $( $config: tt) +) ?) => {
234
+ assert_eq!(
235
+ datetime!( 2021 -01 -02 03 : 04 : 05.999_999_999 UTC ) . format(
236
+ & Iso8601 :: <{ iso8601:: Config :: DEFAULT $( $( $config) +) ?. encode( ) } >
237
+ ) ?,
238
+ $formatted
239
+ ) ;
240
+ } ;
241
+ }
242
+
243
+ assert_format_config ! ( "2021-01-02T03:04:05.999999999Z" , . set_time_precision( TimePrecision :: Second { decimal_digits: NonZeroU8 :: new( 9 ) } ) ) ;
244
+ assert_format_config ! ( "2021-01-02T03:04:05.999999Z" , . set_time_precision( TimePrecision :: Second { decimal_digits: NonZeroU8 :: new( 6 ) } ) ) ;
245
+ assert_format_config ! ( "2021-01-02T03:04:05.999Z" , . set_time_precision( TimePrecision :: Second { decimal_digits: NonZeroU8 :: new( 3 ) } ) ) ;
246
+
247
+ Ok ( ( ) )
248
+ }
249
+
229
250
#[ test]
230
251
fn format_time ( ) -> time:: Result < ( ) > {
231
252
let format_output = [
Original file line number Diff line number Diff line change 2
2
3
3
pub ( crate ) mod formattable;
4
4
mod iso8601;
5
+
5
6
use core:: num:: NonZeroU8 ;
6
7
use std:: io;
7
8
@@ -73,6 +74,10 @@ pub(crate) fn format_float(
73
74
) -> io:: Result < usize > {
74
75
match digits_after_decimal {
75
76
Some ( digits_after_decimal) => {
77
+ // Truncate the decimal points up to the precision
78
+ let trunc_num = 10_f64 . powi ( digits_after_decimal. get ( ) . cast_signed ( ) . extend ( ) ) ;
79
+ let value = f64:: trunc ( value * trunc_num) / trunc_num;
80
+
76
81
let digits_after_decimal = digits_after_decimal. get ( ) . extend ( ) ;
77
82
let width = digits_before_decimal. extend :: < usize > ( ) + 1 + digits_after_decimal;
78
83
write ! ( output, "{value:0>width$.digits_after_decimal$}" ) ?;
You can’t perform that action at this time.
0 commit comments