@@ -321,6 +321,33 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
321
321
Some ( ( value, pos) )
322
322
}
323
323
324
+ fn match_fractional_seconds ( ss : & str , pos : uint ) -> ( i32 , uint ) {
325
+ let len = ss. len ( ) ;
326
+ let mut value = 0_i32 ;
327
+ let mut multiplier = NSEC_PER_SEC / 10 ;
328
+ let mut pos = pos;
329
+
330
+ loop {
331
+ if pos >= len {
332
+ break ;
333
+ }
334
+ let range = ss. char_range_at ( pos) ;
335
+
336
+ match range. ch {
337
+ '0' .. '9' => {
338
+ pos = range. next ;
339
+ // This will drop digits after the nanoseconds place
340
+ let digit = range. ch as i32 - '0' as i32 ;
341
+ value += digit * multiplier;
342
+ multiplier /= 10 ;
343
+ }
344
+ _ => break
345
+ }
346
+ }
347
+
348
+ ( value, pos)
349
+ }
350
+
324
351
fn match_digits_in_range ( ss : & str , pos : uint , digits : uint , ws : bool ,
325
352
min : i32 , max : i32 ) -> Option < ( i32 , uint ) > {
326
353
match match_digits ( ss, pos, digits, ws) {
@@ -441,6 +468,11 @@ fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
441
468
Some ( item) => { let ( v, pos) = item; tm. tm_mday = v; Ok ( pos) }
442
469
None => Err ( ~"Invalid day of the month")
443
470
} ,
471
+ 'f' => {
472
+ let ( val, pos) = match_fractional_seconds ( s, pos) ;
473
+ tm. tm_nsec = val;
474
+ Ok ( pos)
475
+ }
444
476
'F' => {
445
477
parse_type ( s, pos, 'Y' , & mut * tm)
446
478
. chain ( |pos| parse_char ( s, pos, '-' ) )
@@ -773,6 +805,7 @@ fn do_strftime(format: &str, tm: &Tm) -> ~str {
773
805
}
774
806
'd' => fmt ! ( "%02d" , tm. tm_mday as int) ,
775
807
'e' => fmt ! ( "%2d" , tm. tm_mday as int) ,
808
+ 'f' => fmt ! ( "%09d" , tm. tm_nsec as int) ,
776
809
'F' => {
777
810
fmt ! ( "%s-%s-%s" ,
778
811
parse_type( 'Y' , tm) ,
@@ -1011,12 +1044,12 @@ mod tests {
1011
1044
Err(_) => ()
1012
1045
}
1013
1046
1014
- let format = " %a %b %e %T %Y ";
1047
+ let format = " %a %b %e %T . %f %Y ";
1015
1048
assert_eq!(strptime(" ", format), Err(~" Invalid time"));
1016
1049
assert!(strptime(" Fri Feb 13 15 : 31 : 30 ", format)
1017
1050
== Err(~" Invalid time"));
1018
1051
1019
- match strptime(" Fri Feb 13 15 : 31 : 30 2009 ", format) {
1052
+ match strptime(" Fri Feb 13 15 : 31 : 30.01234 2009 ", format) {
1020
1053
Err(e) => fail!(e),
1021
1054
Ok(ref tm) => {
1022
1055
assert!(tm.tm_sec == 30_i32);
@@ -1030,7 +1063,7 @@ mod tests {
1030
1063
assert!(tm.tm_isdst == 0_i32);
1031
1064
assert!(tm.tm_gmtoff == 0_i32);
1032
1065
assert!(tm.tm_zone == ~" ");
1033
- assert!(tm.tm_nsec == 0_i32 );
1066
+ assert!(tm.tm_nsec == 12340000_i32 );
1034
1067
}
1035
1068
}
1036
1069
@@ -1187,6 +1220,7 @@ mod tests {
1187
1220
assert_eq!(local.strftime(" %D "), ~" 02 /13 /09 ");
1188
1221
assert_eq!(local.strftime(" %d"), ~" 13 ");
1189
1222
assert_eq!(local.strftime(" %e"), ~" 13 ");
1223
+ assert_eq!(local.strftime(" %f"), ~" 000054321 ");
1190
1224
assert_eq!(local.strftime(" %F "), ~" 2009 -02 -13 ");
1191
1225
// assert!(local.strftime(" %G ") == " 2009 ");
1192
1226
// assert!(local.strftime(" %g") == " 09 ");
0 commit comments