@@ -14,7 +14,7 @@ impl Time {
1414 /// Serialize this instance to `out` in a format suitable for use in header fields of serialized git commits or tags.
1515 pub fn write_to ( & self , mut out : impl std:: io:: Write ) -> std:: io:: Result < ( ) > {
1616 let mut itoa = itoa:: Buffer :: new ( ) ;
17- out. write_all ( itoa. format ( self . seconds_since_unix_epoch ) . as_bytes ( ) ) ?;
17+ out. write_all ( itoa. format ( self . seconds ) . as_bytes ( ) ) ?;
1818 out. write_all ( b" " ) ?;
1919 out. write_all ( match self . sign {
2020 Sign :: Plus => b"+" ,
@@ -24,7 +24,7 @@ impl Time {
2424 const ZERO : & [ u8 ; 1 ] = b"0" ;
2525
2626 const SECONDS_PER_HOUR : i32 = 60 * 60 ;
27- let offset = self . offset_in_seconds . abs ( ) ;
27+ let offset = self . offset . abs ( ) ;
2828 let hours = offset / SECONDS_PER_HOUR ;
2929 assert ! ( hours < 25 , "offset is more than a day: {hours}" ) ;
3030 let minutes = ( offset - ( hours * SECONDS_PER_HOUR ) ) / 60 ;
@@ -40,30 +40,48 @@ impl Time {
4040 out. write_all ( itoa. format ( minutes) . as_bytes ( ) ) . map ( |_| ( ) )
4141 }
4242
43- /// Computes the number of bytes necessary to render this time .
43+ /// Computes the number of bytes necessary to write it using [`Time::write_to()`] .
4444 pub fn size ( & self ) -> usize {
45- // TODO: this is not year 2038 safe…but we also can't parse larger numbers (or represent them) anyway. It's a trap nonetheless
46- // that can be fixed by increasing the size to usize.
47- ( if self . seconds_since_unix_epoch >= 1_000_000_000 {
45+ ( if self . seconds >= 10_000_000_000_000_000_000 {
46+ 20
47+ } else if self . seconds >= 1_000_000_000_000_000_000 {
48+ 19
49+ } else if self . seconds >= 100_000_000_000_000_000 {
50+ 18
51+ } else if self . seconds >= 10_000_000_000_000_000 {
52+ 17
53+ } else if self . seconds >= 1_000_000_000_000_000 {
54+ 16
55+ } else if self . seconds >= 100_000_000_000_000 {
56+ 15
57+ } else if self . seconds >= 10_000_000_000_000 {
58+ 14
59+ } else if self . seconds >= 1_000_000_000_000 {
60+ 13
61+ } else if self . seconds >= 100_000_000_000 {
62+ 12
63+ } else if self . seconds >= 10_000_000_000 {
64+ 11
65+ } else if self . seconds >= 1_000_000_000 {
4866 10
49- } else if self . seconds_since_unix_epoch >= 100_000_000 {
67+ } else if self . seconds >= 100_000_000 {
5068 9
51- } else if self . seconds_since_unix_epoch >= 10_000_000 {
69+ } else if self . seconds >= 10_000_000 {
5270 8
53- } else if self . seconds_since_unix_epoch >= 1_000_000 {
71+ } else if self . seconds >= 1_000_000 {
5472 7
55- } else if self . seconds_since_unix_epoch >= 100_000 {
73+ } else if self . seconds >= 100_000 {
5674 6
57- } else if self . seconds_since_unix_epoch >= 10_000 {
75+ } else if self . seconds >= 10_000 {
5876 5
59- } else if self . seconds_since_unix_epoch >= 1_000 {
77+ } else if self . seconds >= 1_000 {
6078 4
61- } else if self . seconds_since_unix_epoch >= 100 {
79+ } else if self . seconds >= 100 {
6280 3
63- } else if self . seconds_since_unix_epoch >= 10 {
81+ } else if self . seconds >= 10 {
6482 2
6583 } else {
6684 1
67- } ) + 2 /*space + sign*/ + 2 /*hours*/ + 2 /*minutes*/
85+ } ) + 2 /*space + offset sign*/ + 2 /*offset hours*/ + 2 /*offset minutes*/
6886 }
6987}
0 commit comments