www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - time units and types in phobos

reply "Ben Hinkle" <bhinkle mathworks.com> writes:
Reading the doc for std.socker I noticed it defines timeval. Time units and 
data types are typically hard to standardize. Some functions take 
milliseconds, some take micro, some take ints some take longs and some take 
timevals (or timespecs, too?). Remembering which takes what is a pain. What 
do people think of making a std module (or maybe reusing std.date) for 
time-related types and functions? I'm thinking of something along the lines 
of the attached module (modified from my locks library) which is based on 
Java's TimeUnit: 
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/TimeUnit.html

Any objections to writing up some doc and asking Walter to include in 
phobos?

To illustrate the API here are the unittests:

  sleep(2,sec);
  sleep(toTimeval(30,msec));

  assert( toMillis(100,sec) == 100_000 );
  assert( toMicros(100,sec) == 100_000_000 );
  assert( toNanos(100,sec) == 100_000_000_000 );
  assert( toSeconds(100,sec) == 100 );
  assert( convert(100,sec,msec) == 100_000 );
  assert( toMicros(5_000,nsec) == 5 );
  assert( toSeconds(5_000_000,usec) == 5 );
  assert( toSeconds(50,usec) == 0 );
  assert( toNanos(5_000_000_000_000,sec) == long.max );

  timeval tv = toTimeval(100,sec);
  assert( tv.seconds == 100 );
  assert( tv.microseconds == 0 );
  assert( toMillis(tv) == 100_000 );
  assert( toMicros(tv) == 100_000_000 );
  assert( toNanos(tv) == 100_000_000_000 );
  timeval tv2;
  tv2.seconds = 5;
  tv2.microseconds = 7;
  assert( toMicros(tv2) == 5_000_007 );
  assert( toMicros(tv2 + tv) == 105_000_007 );
  assert( toMicros(tv2 - tv) == -94999993 );

-Ben 


begin 666 timeunit.d
M+RHJ#0H *B!7<FET=&5N(&)Y($1O=6< 3&5A('=I=&  87-S:7-T86YC92!F
M<F]M(&UE;6)E<G, ;V8 2D-0($I34BTQ-C8-"B J($5X<&5R="!'<F]U<"!A
M;F0 <F5L96%S960 =&\ =&AE('!U8FQI8R!D;VUA:6XL(&%S(&5X<&QA:6YE
M9"!A= T*("H :'1T<#HO+V-R96%T:79E8V]M;6]N<RYO<F<O;&EC96YS97,O
M<'5B;&EC9&]M86EN#0H *B!0;W)T960 =&\ 1"!B>2!"96X 2&EN:VQE(&%N
M9"!A9&1E9"!T:6UE=F%L#0H *B!%;6%I;"!C;VUM96YT<R!A;F0 8G5G(')E
M<&]R=', =&\ 8F5N+FAI;FML94!G;6%I;"YC;VT-"B J+PT*#0IM;V1U;&4 
M=&EM975N:70[#0H-"B\J*B!<96YU;2!4:6UE3W5T#0H *B!!(#QT=#Y4:6UE
M56YI=#PO='0^(')E<')E<V5N=', =&EM92!D=7)A=&EO;G, 870 82!G:79E
M;B!U;FET(&]F#0H *B!G<F%N=6QA<FET>2!A;F0 <')O=FED97, =71I;&ET
M>2!M971H;V1S('1O(&-O;G9E<G0 86-R;W-S('5N:71S+ T*("H 02 \='0^
M5&EM955N:70\+W1T/B!D;V5S(&YO="!M86EN=&%I;B!T:6UE(&EN9F]R;6%T
M:6]N+"!B=70 ;VYL>0T*("H :&5L<', ;W)G86YI>F4 86YD('5S92!T:6UE
M(')E<')E<V5N=&%T:6]N<R!T:&%T(&UA>2!B92!M86EN=&%I;F5D#0H *B!S
M97!A<F%T96QY(&%C<F]S<R!V87)I;W5S(&-O;G1E>'1S+ T*("HO#0IE;G5M
M(%1I;655;FET('L-"B  3F%N;U-E8V]N9', /2 P+ T*("!-:6-R;U-E8V]N
M9',L( T*("!-:6QL:5-E8V]N9',L#0H (%-E8V]N9',-"GT-" T*+R\ 4VAO
M<G1H86YD(&%B8G)E=FEA=&EO;G, 9F]R('1I;64 =6YI=',-"F%L:6%S(%1I
M;655;FET+E-E8V]N9', <V5C.PT*86QI87, 5&EM955N:70N36EL;&E396-O
M;F1S(&US96,[#0IA;&EA<R!4:6UE56YI="Y-:6-R;U-E8V]N9', =7-E8SL-
M"F%L:6%S(%1I;655;FET+DYA;F]396-O;F1S(&YS96,[#0H-"B\J*B!,;V]K
M=7  =&%B;&4 9F]R(&-O;G9E<G-I;VX 9F%C=&]R<R J+PT*<')I=F%T92!C
M;VYS="!I;G1;5&EM955N:70N;6%X*S%=(&UU;'1I<&QI97)S(#T 6R -"B  
M,2P #0H (#$P,# L( T*(" Q,# P7S P,"P #0H (#$P,#!?,# P7S P," -
M"ET[#0H ("  #0HO*BH #0H *B!,;V]K=7  =&%B;&4 =&\ 8VAE8VL <V%T
M=7)A=&EO;BX ($YO=&4 =&AA="!B96-A=7-E('=E(&%R90T*("H 9&EV:61I
M;F< =&AE<V4 9&]W;BP =V4 9&]N)W0 :&%V92!T;R!D96%L('=I=&  87-Y
M;6UE=')Y(&]F#0H *B!-24XO34%8('9A;'5E<RX-"B J+PT*<')I=F%T92!C
M;VYS="!L;VYG6U1I;655;FET+FUA>"LQ72!O=F5R9FQO=W, /2!;( T*(" P
M+" O+R!U;G5S960-"B  ;&]N9RYM87  +R Q,# P+ T*("!L;VYG+FUA>" O
M(#$P,#!?,# P+ T*("!L;VYG+FUA>" O(#$P,#!?,# P7S P," -"ET[#0H-
M"B\J* T*("H 4&5R9F]R;2!C;VYV97)S:6]N(&)A<V5D(&]N(&=I=F5N(&1E
M;'1A(')E<')E<V5N=&EN9R!T:&4-"B J(&1I9F9E<F5N8V4 8F5T=V5E;B!U
M;FET<PT*("H 7'!A<F%M(&1E;'1A('1H92!D:69F97)E;F-E(&EN(&EN9&5X
M('9A;'5E<R!O9B!S;W5R8V4 86YD('1A<F=E="!U;FET<PT*("H 7'!A<F%M
M(&1U<F%T:6]N('1H92!D=7)A=&EO; T*("H 7')E='5R;B!C;VYV97)T960 
M9'5R871I;VX ;W( <V%T=7)A=&5D('9A;'5E#0H *B\-"G!R:79A=&4 ;&]N
M9R!D;T-O;G9E<G0H:6YT(&1E;'1A+"!L;VYG(&1U<F%T:6]N*2![#0H (&EF
M("AD96QT82 ]/2 P*0T*("  (')E='5R;B!D=7)A=&EO;CL-"B  :68 *&1E
M;'1A(#P ,"D #0H ("  <F5T=7)N(&1U<F%T:6]N("\ ;75L=&EP;&EE<G-;
M+61E;'1A73L-"B  :68 *&1U<F%T:6]N(#X ;W9E<F9L;W=S6V1E;'1A72D-
M"B  ("!R971U<FX ;&]N9RYM87 [#0H (&EF("AD=7)A=&EO;B \("UO=F5R
M9FQO=W-;9&5L=&%=*0T*("  (')E='5R;B!L;VYG+FUI;CL-"B  <F5T=7)N
M(&1U<F%T:6]N("H ;75L=&EP;&EE<G-;9&5L=&%=.PT*?0T*#0HO*BH-"B J
M($-O;G9E<G0 =&AE(&=I=F5N('1I;64 9'5R871I;VX :6X =&AE(&=I=F5N
M('5N:70 =&\ =&AI<PT*("H =6YI="X ($-O;G9E<G-I;VYS(&9R;VT 9FEN
M97( =&\ 8V]A<G-E<B!G<F%N=6QA<FET:65S#0H *B!T<G5N8V%T92P <V\ 
M;&]S92!P<F5C:7-I;VXN($9O<B!E>&%M<&QE(&-O;G9E<G1I;F<-"B J(#QT
M=#XY.3D\+W1T/B!M:6QL:7-E8V]N9', =&\ <V5C;VYD<R!R97-U;'1S(&EN
M#0H *B \='0^,#PO='0^+B!#;VYV97)S:6]N<R!F<F]M(&-O87)S97( =&\ 
M9FEN97( 9W)A;G5L87)I=&EE<PT*("H =VET:"!A<F=U;65N=', =&AA="!W
M;W5L9"!N=6UE<FEC86QL>2!O=F5R9FQO=R!S871U<F%T92!T;PT*("H /'1T
M/FQO;F<N;6EN/"]T=#X :68 ;F5G871I=F4 ;W( /'1T/FQO;F<N;6%X/"]T
M=#X-"B J(&EF('!O<VET:79E+ T*("H-"B J(%QP87)A;2!D=7)A=&EO;B!T
M:&4 =&EM92!D=7)A=&EO;B!I;B!T:&4 9VEV96X /'1T/G5N:70\+W1T/ T*
M("H 7'!A<F%M(&9R;VU5;FET('1H92!U;FET(&]F('1H92 \='0^9'5R871I
M;VX\+W1T/B!A<F=U;65N= T*("H 7'!A<F%M('1O56YI="!T:&4 =6YI="!O
M9B!T:&4 <F5S=6QT#0H *B!<<F5T=7)N('1H92!C;VYV97)T960 9'5R871I
M;VX-"B J(&]R(#QT=#YL;VYG+FUI;CPO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5C;VYD<RAL;VYG(&1U
M<F%T:6]N+"!4:6UE56YI="!F<F]M56YI="D >PT*("!R971U<FX 9&]#;VYV
M97)T*&9R;VU5;FET("T 5&EM955N:70N4V5C;VYD<RP 9'5R871I;VXI.PT*
M?0T*#0IU;FET=&5S="![#0H (&%S<V5R="  =&]-:6QL:7,H,3 P+'-E8RD 
M/3T ,3 P7S P," I.PT*("!A<W-E<G0H('1O36EC<F]S*#$P,"QS96,I(#T]
M(#$P,%\P,#!?,# P("D[#0H (&%S<V5R="  =&].86YO<R Q,# L<V5C*2 ]
M/2 Q,#!?,# P7S P,%\P,#  *3L-"B  87-S97)T*"!T;U-E8V]N9',H,3 P
M+'-E8RD /3T ,3 P("D[#0H (&%S<V5R="  8V]N=F5R=" Q,# L<V5C+&US
M96,I(#T](#$P,%\P,#  *3L-"B  87-S97)T*"!T;TUI8W)O<R U7S P,"QN
M<V5C*2 ]/2 U("D[#0H (&%S<V5R="  =&]396-O;F1S*#5?,# P7S P,"QU
M<V5C*2 ]/2 U("D[#0H (&%S<V5R="  =&]396-O;F1S*#4P+'5S96,I(#T]
M(#  *3L-"B  87-S97)T*"!T;TYA;F]S*#5?,# P7S P,%\P,#!?,# P+'-E
M8RD /3T ;&]N9RYM87  *3L-"GT-" T*<W1R=6-T('1I;65V86P >PT*("!I
M;G0 <V5C;VYD<SL-"B  :6YT(&UI8W)O<V5C;VYD<SL-" T*("!T:6UE=F%L
M(&]P061D*'1I;65V86P ='8R*2![#0H ("  =&EM979A;"!R97,[#0H ("  
M<F5S+FUI8W)O<V5C;VYD<R ](&UI8W)O<V5C;VYD<R K('1V,BYM:6-R;W-E
M8V]N9',[#0H ("  ;&]N9R!C87)R>2 ]('1O4V5C;VYD<RAR97,N;6EC<F]S
M96-O;F1S+&US96,I.PT*("  (')E<RYM:6-R;W-E8V]N9', )3T ,5\P,#!?
M,# P.PT*("  (')E<RYS96-O;F1S(#T <V5C;VYD<R K('1V,BYS96-O;F1S
M("L 8V%R<GD[#0H ("  <F5T=7)N(')E<SL-"B  ?0T*#0H ('1I;65V86P 
M;W!3=6(H=&EM979A;"!T=C(I('L-"B  ("!T:6UE=F%L(')E<SL-"B  ("!R
M97,N;6EC<F]S96-O;F1S(#T ;6EC<F]S96-O;F1S("T ='8R+FUI8W)O<V5C
M;VYD<SL-"B  ("!L;VYG(&-A<G)Y(#T =&]396-O;F1S*')E<RYM:6-R;W-E
M8V]N9',L;7-E8RD[#0H ("  <F5S+FUI8W)O<V5C;VYD<R E/2 Q7S P,%\P
M,# [#0H ("  <F5S+G-E8V]N9', /2!S96-O;F1S("T ='8R+G-E8V]N9', 
M*R!C87)R>3L-"B  ("!R971U<FX <F5S.PT*("!]#0H-"GT-" T*=&EM979A
M;"!T;U1I;65V86PH;&]N9R!D=7)A=&EO;BP 5&EM955N:70 =6YI="D >PT*
M("!T:6UE=F%L('1V.PT*("!T=BYS96-O;F1S(#T =&]396-O;F1S*&1U<F%T
M:6]N+'5N:70I.PT*("!T=BYM:6-R;W-E8V]N9', /2!T;TUI8W)O<RAD=7)A
M=&EO;BQU;FET*2 E(#$P,#!?,# P.PT*("!R971U<FX ='8[#0I]#0H-"FQO
M;F< =&].86YO<RAT:6UE=F%L('1V*2![#0H (')E='5R;B!T;TYA;F]S*'1V
M+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK#0H ("  ("  ("!T;TYA;F]S
M*'1V+FUI8W)O<V5C;VYD<RQ4:6UE56YI="Y-:6-R;U-E8V]N9',I.PT*?0T*
M#0IL;VYG('1O36EC<F]S*'1I;65V86P ='8I('L-"B  <F5T=7)N('1O36EC
M<F]S*'1V+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK='8N;6EC<F]S96-O
M;F1S.PT*?0T*#0IL;VYG('1O36EL;&ES*'1I;65V86P ='8I('L-"B  <F5T
M=7)N('1O36EL;&ES*'1V+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK#0H 
M("  ("  ("!T;TUI;&QI<RAT=BYM:6-R;W-E8V]N9',L5&EM955N:70N36EC
M<F]396-O;F1S*3L-"GT-" T*;&]N9R!T;U-E8V]N9',H=&EM979A;"!T=BD 
M>PT*("!R971U<FX ='8N<V5C;VYD<RMT;U-E8V]N9',H='8N;6EC<F]S96-O
M;F1S+%1I;655;FET+DUI8W)O4V5C;VYD<RD[#0I]#0H-"G5N:71T97-T('L-
M"B  =&EM979A;"!T=B ]('1O5&EM979A;" Q,# L<V5C*3L-"B  87-S97)T
M*"!T=BYS96-O;F1S(#T](#$P," I.PT*("!A<W-E<G0H('1V+FUI8W)O<V5C
M;VYD<R ]/2 P("D[#0H (&%S<V5R="  =&]-:6QL:7,H='8I(#T](#$P,%\P
M,#  *3L-"B  87-S97)T*"!T;TUI8W)O<RAT=BD /3T ,3 P7S P,%\P,#  
M*3L-"B  87-S97)T*"!T;TYA;F]S*'1V*2 ]/2 Q,#!?,# P7S P,%\P,#  
M*3L-" T*("!T:6UE=F%L('1V,CL-"B  ='8R+G-E8V]N9', /2 U.PT*("!T
M=C(N;6EC<F]S96-O;F1S(#T -SL-"B  87-S97)T*"!T;TUI8W)O<RAT=C(I
M(#T](#5?,# P7S P-R I.PT*("!A<W-E<G0H('1O36EC<F]S*'1V,B K('1V
M*2 ]/2 Q,#5?,# P7S P-R I.PT*("!A<W-E<G0H('1O36EC<F]S*'1V,B M
M('1V*2 ]/2 M.30Y.3DY.3, *3L-"GT-" T*=F5R<VEO;B H5VEN9&]W<RD 
M>PT*#0H ('!R:79A=&4 :6UP;W)T('-T9"YC+G=I;F1O=W,N=VEN9&]W<SL-
M" T*("!V;VED('-L965P*&QO;F< 9'5R871I;VXL(%1I;655;FET('5N:70I
M('L-"B  ("!L;VYG('0 /2!T;TUI;&QI<RAD=7)A=&EO;BQU;FET*3L-"B  
M("!T(#T =#YU:6YT+FUA>" _('5I;G0N;6%X(#H =#L-"B  ("!T(#T =#PP
M(#\ ," Z('0[#0H ("  4VQE97 H8V%S="AU:6YT*70I.PT*("!]#0H-"B  
M=F]I9"!S;&5E<"AT:6UE=F%L('1V*2![#0H ("  ;&]N9R!T(#T =&]-:6QL
M:7,H='8I.PT*("  ('0 /2!T/G5I;G0N;6%X(#\ =6EN="YM87  .B!T.PT*
M("  ('0 /2!T/#  /R P(#H =#L-"B  ("!3;&5E<"AC87-T*'5I;G0I="D[
M#0H ('T-" T*?2!E;'-E('9E<G-I;VX *&QI;G5X*2![#0H-"B  <')I=F%T
M92!I;7!O<G0 <W1D+F,N=&EM93L-" T*("!V;VED('-L965P*&QO;F< 9'5R
M871I;VXL(%1I;655;FET('5N:70I('L-"B  ("!L;VYG('0 /2!T;TUI8W)O
M<RAD=7)A=&EO;BQU;FET*3L-"B  ("!T(#T =#YU:6YT+FUA>" _('5I;G0N
M;6%X(#H =#L-"B  ("!T(#T =#PP(#\ ," Z('0[#0H ("  =7-L965P*&-A
M<W0H=6EN="ET*3L-"B  ?0T*#0H ('9O:60 <VQE97 H=&EM979A;"!T=BD 
M>PT*("  (&QO;F< =" ]('1O36EC<F]S*'1V*3L-"B  ("!T(#T =#YU:6YT
M+FUA>" _('5I;G0N;6%X(#H =#L-"B  ("!T(#T =#PP(#\ ," Z('0[#0H 
M("  =7-L965P*&-A<W0H=6EN="ET*3L-"B  ?0T*#0I]#0H-"G5N:71T97-T
M('L-"B  <VQE97 H,BQS96,I.PT*("!S;&5E<"AT;U1I;65V86PH,S L;7-E
'8RDI.PT*?0``
`
end
Feb 28 2005
parent Kris <Kris_member pathlink.com> writes:
In article <cvvrdn$dq6$1 digitaldaemon.com>, Ben Hinkle says...
Reading the doc for std.socker I noticed it defines timeval. Time units and 
data types are typically hard to standardize. Some functions take 
milliseconds, some take micro, some take ints some take longs and some take 
timevals (or timespecs, too?). Remembering which takes what is a pain. What 
do people think of making a std module (or maybe reusing std.date) for 
time-related types and functions? I'm thinking of something along the lines 
of the attached module (modified from my locks library) which is based on 
Java's TimeUnit: 
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/TimeUnit.html

Any objections to writing up some doc and asking Walter to include in 
phobos?
No objection, but there's an equivalent in Mango also.
To illustrate the API here are the unittests:

  sleep(2,sec);
  sleep(toTimeval(30,msec));

  assert( toMillis(100,sec) == 100_000 );
  assert( toMicros(100,sec) == 100_000_000 );
  assert( toNanos(100,sec) == 100_000_000_000 );
  assert( toSeconds(100,sec) == 100 );
  assert( convert(100,sec,msec) == 100_000 );
  assert( toMicros(5_000,nsec) == 5 );
  assert( toSeconds(5_000_000,usec) == 5 );
  assert( toSeconds(50,usec) == 0 );
  assert( toNanos(5_000_000_000_000,sec) == long.max );

  timeval tv = toTimeval(100,sec);
  assert( tv.seconds == 100 );
  assert( tv.microseconds == 0 );
  assert( toMillis(tv) == 100_000 );
  assert( toMicros(tv) == 100_000_000 );
  assert( toNanos(tv) == 100_000_000_000 );
  timeval tv2;
  tv2.seconds = 5;
  tv2.microseconds = 7;
  assert( toMicros(tv2) == 5_000_007 );
  assert( toMicros(tv2 + tv) == 105_000_007 );
  assert( toMicros(tv2 - tv) == -94999993 );

-Ben 


begin 666 timeunit.d
M+RHJ#0H *B!7<FET=&5N(&)Y($1O=6< 3&5A('=I=&  87-S:7-T86YC92!F
M<F]M(&UE;6)E<G, ;V8 2D-0($I34BTQ-C8-"B J($5X<&5R="!'<F]U<"!A
M;F0 <F5L96%S960 =&\ =&AE('!U8FQI8R!D;VUA:6XL(&%S(&5X<&QA:6YE
M9"!A= T*("H :'1T<#HO+V-R96%T:79E8V]M;6]N<RYO<F<O;&EC96YS97,O
M<'5B;&EC9&]M86EN#0H *B!0;W)T960 =&\ 1"!B>2!"96X 2&EN:VQE(&%N
M9"!A9&1E9"!T:6UE=F%L#0H *B!%;6%I;"!C;VUM96YT<R!A;F0 8G5G(')E
M<&]R=', =&\ 8F5N+FAI;FML94!G;6%I;"YC;VT-"B J+PT*#0IM;V1U;&4 
M=&EM975N:70[#0H-"B\J*B!<96YU;2!4:6UE3W5T#0H *B!!(#QT=#Y4:6UE
M56YI=#PO='0^(')E<')E<V5N=', =&EM92!D=7)A=&EO;G, 870 82!G:79E
M;B!U;FET(&]F#0H *B!G<F%N=6QA<FET>2!A;F0 <')O=FED97, =71I;&ET
M>2!M971H;V1S('1O(&-O;G9E<G0 86-R;W-S('5N:71S+ T*("H 02 \='0^
M5&EM955N:70\+W1T/B!D;V5S(&YO="!M86EN=&%I;B!T:6UE(&EN9F]R;6%T
M:6]N+"!B=70 ;VYL>0T*("H :&5L<', ;W)G86YI>F4 86YD('5S92!T:6UE
M(')E<')E<V5N=&%T:6]N<R!T:&%T(&UA>2!B92!M86EN=&%I;F5D#0H *B!S
M97!A<F%T96QY(&%C<F]S<R!V87)I;W5S(&-O;G1E>'1S+ T*("HO#0IE;G5M
M(%1I;655;FET('L-"B  3F%N;U-E8V]N9', /2 P+ T*("!-:6-R;U-E8V]N
M9',L( T*("!-:6QL:5-E8V]N9',L#0H (%-E8V]N9',-"GT-" T*+R\ 4VAO
M<G1H86YD(&%B8G)E=FEA=&EO;G, 9F]R('1I;64 =6YI=',-"F%L:6%S(%1I
M;655;FET+E-E8V]N9', <V5C.PT*86QI87, 5&EM955N:70N36EL;&E396-O
M;F1S(&US96,[#0IA;&EA<R!4:6UE56YI="Y-:6-R;U-E8V]N9', =7-E8SL-
M"F%L:6%S(%1I;655;FET+DYA;F]396-O;F1S(&YS96,[#0H-"B\J*B!,;V]K
M=7  =&%B;&4 9F]R(&-O;G9E<G-I;VX 9F%C=&]R<R J+PT*<')I=F%T92!C
M;VYS="!I;G1;5&EM955N:70N;6%X*S%=(&UU;'1I<&QI97)S(#T 6R -"B  
M,2P #0H (#$P,# L( T*(" Q,# P7S P,"P #0H (#$P,#!?,# P7S P," -
M"ET[#0H ("  #0HO*BH #0H *B!,;V]K=7  =&%B;&4 =&\ 8VAE8VL <V%T
M=7)A=&EO;BX ($YO=&4 =&AA="!B96-A=7-E('=E(&%R90T*("H 9&EV:61I
M;F< =&AE<V4 9&]W;BP =V4 9&]N)W0 :&%V92!T;R!D96%L('=I=&  87-Y
M;6UE=')Y(&]F#0H *B!-24XO34%8('9A;'5E<RX-"B J+PT*<')I=F%T92!C
M;VYS="!L;VYG6U1I;655;FET+FUA>"LQ72!O=F5R9FQO=W, /2!;( T*(" P
M+" O+R!U;G5S960-"B  ;&]N9RYM87  +R Q,# P+ T*("!L;VYG+FUA>" O
M(#$P,#!?,# P+ T*("!L;VYG+FUA>" O(#$P,#!?,# P7S P," -"ET[#0H-
M"B\J* T*("H 4&5R9F]R;2!C;VYV97)S:6]N(&)A<V5D(&]N(&=I=F5N(&1E
M;'1A(')E<')E<V5N=&EN9R!T:&4-"B J(&1I9F9E<F5N8V4 8F5T=V5E;B!U
M;FET<PT*("H 7'!A<F%M(&1E;'1A('1H92!D:69F97)E;F-E(&EN(&EN9&5X
M('9A;'5E<R!O9B!S;W5R8V4 86YD('1A<F=E="!U;FET<PT*("H 7'!A<F%M
M(&1U<F%T:6]N('1H92!D=7)A=&EO; T*("H 7')E='5R;B!C;VYV97)T960 
M9'5R871I;VX ;W( <V%T=7)A=&5D('9A;'5E#0H *B\-"G!R:79A=&4 ;&]N
M9R!D;T-O;G9E<G0H:6YT(&1E;'1A+"!L;VYG(&1U<F%T:6]N*2![#0H (&EF
M("AD96QT82 ]/2 P*0T*("  (')E='5R;B!D=7)A=&EO;CL-"B  :68 *&1E
M;'1A(#P ,"D #0H ("  <F5T=7)N(&1U<F%T:6]N("\ ;75L=&EP;&EE<G-;
M+61E;'1A73L-"B  :68 *&1U<F%T:6]N(#X ;W9E<F9L;W=S6V1E;'1A72D-
M"B  ("!R971U<FX ;&]N9RYM87 [#0H (&EF("AD=7)A=&EO;B \("UO=F5R
M9FQO=W-;9&5L=&%=*0T*("  (')E='5R;B!L;VYG+FUI;CL-"B  <F5T=7)N
M(&1U<F%T:6]N("H ;75L=&EP;&EE<G-;9&5L=&%=.PT*?0T*#0HO*BH-"B J
M($-O;G9E<G0 =&AE(&=I=F5N('1I;64 9'5R871I;VX :6X =&AE(&=I=F5N
M('5N:70 =&\ =&AI<PT*("H =6YI="X ($-O;G9E<G-I;VYS(&9R;VT 9FEN
M97( =&\ 8V]A<G-E<B!G<F%N=6QA<FET:65S#0H *B!T<G5N8V%T92P <V\ 
M;&]S92!P<F5C:7-I;VXN($9O<B!E>&%M<&QE(&-O;G9E<G1I;F<-"B J(#QT
M=#XY.3D\+W1T/B!M:6QL:7-E8V]N9', =&\ <V5C;VYD<R!R97-U;'1S(&EN
M#0H *B \='0^,#PO='0^+B!#;VYV97)S:6]N<R!F<F]M(&-O87)S97( =&\ 
M9FEN97( 9W)A;G5L87)I=&EE<PT*("H =VET:"!A<F=U;65N=', =&AA="!W
M;W5L9"!N=6UE<FEC86QL>2!O=F5R9FQO=R!S871U<F%T92!T;PT*("H /'1T
M/FQO;F<N;6EN/"]T=#X :68 ;F5G871I=F4 ;W( /'1T/FQO;F<N;6%X/"]T
M=#X-"B J(&EF('!O<VET:79E+ T*("H-"B J(%QP87)A;2!D=7)A=&EO;B!T
M:&4 =&EM92!D=7)A=&EO;B!I;B!T:&4 9VEV96X /'1T/G5N:70\+W1T/ T*
M("H 7'!A<F%M(&9R;VU5;FET('1H92!U;FET(&]F('1H92 \='0^9'5R871I
M;VX\+W1T/B!A<F=U;65N= T*("H 7'!A<F%M('1O56YI="!T:&4 =6YI="!O
M9B!T:&4 <F5S=6QT#0H *B!<<F5T=7)N('1H92!C;VYV97)T960 9'5R871I
M;VX-"B J(&]R(#QT=#YL;VYG+FUI;CPO='0^(&EF(&-O;G9E<G-I;VX =V]U
M;&0 ;F5G871I=F5L>0T*("H ;W9E<F9L;W<L(&]R(#QT=#YL;VYG+FUA>#PO
M='0^(&EF(&ET('=O=6QD('!O<VET:79E;'D ;W9E<F9L;W<N#0H *B\-"FQO
M;F< 8V]N=F5R="AL;VYG(&1U<F%T:6]N+"!4:6UE56YI="!F<F]M56YI="P 
M5&EM955N:70 =&]5;FET*2![#0H (')E='5R;B!D;T-O;G9E<G0H9G)O;55N
M:70 +2!T;U5N:70L(&1U<F%T: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PT*?0T*#0HO*BH-"B J($-O;G9E<G0 =&\ 
M;6EL;&ES96-O;F1S+ T*("H 7'!A<F%M(&1U<F%T:6]N('1H92!D=7)A=&EO
M; T*("H 7'!A<F%M(&9R;VU5;FET('1H92!U;FET(&]F('1H92 \='0^9'5R
M871I;VX\+W1T/B!A<F=U;65N= T*("H 7')E='5R;B!T:&4 8V]N=F5R=&5D
M(&1U<F%T:6]N+ T*("H ;W( /'1T/FQO;F<N;6EN/"]T=#X :68 8V]N=F5R
M<VEO;B!W;W5L9"!N96=A=&EV96QY#0H *B!O=F5R9FQO=RP ;W( /'1T/FQO
M;F<N;6%X/"]T=#X :68 :70 =V]U;&0 <&]S:71I=F5L>2!O=F5R9FQO=RX-
M"B J+PT*;&]N9R!T;TUI;&QI<RAL;VYG(&1U<F%T:6]N+"!4:6UE56YI="!F
M<F]M56YI="D >PT*("!R971U<FX 9&]#;VYV97)T*&9R;VU5;FET("T 5&EM
M955N:70N36EL;&E396-O;F1S+"!D=7)A=&EO;BD[#0I]#0H-"B\J* T*("H 
M0V]N=F5R="!T;R!S96-O;F1S+ T*("H 7'!A<F%M(&1U<F%T:6]N('1H92!D
M=7)A=&EO; T*("H 7'!A<F%M(&9R;VU5;FET('1H92!U;FET(&]F('1H92 \
M='0^9'5R871I;VX\+W1T/B!A<F=U;65N= T*("H 7')E='5R;B!T:&4 8V]N
M=F5R=&5D(&1U<F%T:6]N+ T*("HO#0IL;VYG('1O4V5C;VYD<RAL;VYG(&1U
M<F%T:6]N+"!4:6UE56YI="!F<F]M56YI="D >PT*("!R971U<FX 9&]#;VYV
M97)T*&9R;VU5;FET("T 5&EM955N:70N4V5C;VYD<RP 9'5R871I;VXI.PT*
M?0T*#0IU;FET=&5S="![#0H (&%S<V5R="  =&]-:6QL:7,H,3 P+'-E8RD 
M/3T ,3 P7S P," I.PT*("!A<W-E<G0H('1O36EC<F]S*#$P,"QS96,I(#T]
M(#$P,%\P,#!?,# P("D[#0H (&%S<V5R="  =&].86YO<R Q,# L<V5C*2 ]
M/2 Q,#!?,# P7S P,%\P,#  *3L-"B  87-S97)T*"!T;U-E8V]N9',H,3 P
M+'-E8RD /3T ,3 P("D[#0H (&%S<V5R="  8V]N=F5R=" Q,# L<V5C+&US
M96,I(#T](#$P,%\P,#  *3L-"B  87-S97)T*"!T;TUI8W)O<R U7S P,"QN
M<V5C*2 ]/2 U("D[#0H (&%S<V5R="  =&]396-O;F1S*#5?,# P7S P,"QU
M<V5C*2 ]/2 U("D[#0H (&%S<V5R="  =&]396-O;F1S*#4P+'5S96,I(#T]
M(#  *3L-"B  87-S97)T*"!T;TYA;F]S*#5?,# P7S P,%\P,#!?,# P+'-E
M8RD /3T ;&]N9RYM87  *3L-"GT-" T*<W1R=6-T('1I;65V86P >PT*("!I
M;G0 <V5C;VYD<SL-"B  :6YT(&UI8W)O<V5C;VYD<SL-" T*("!T:6UE=F%L
M(&]P061D*'1I;65V86P ='8R*2![#0H ("  =&EM979A;"!R97,[#0H ("  
M<F5S+FUI8W)O<V5C;VYD<R ](&UI8W)O<V5C;VYD<R K('1V,BYM:6-R;W-E
M8V]N9',[#0H ("  ;&]N9R!C87)R>2 ]('1O4V5C;VYD<RAR97,N;6EC<F]S
M96-O;F1S+&US96,I.PT*("  (')E<RYM:6-R;W-E8V]N9', )3T ,5\P,#!?
M,# P.PT*("  (')E<RYS96-O;F1S(#T <V5C;VYD<R K('1V,BYS96-O;F1S
M("L 8V%R<GD[#0H ("  <F5T=7)N(')E<SL-"B  ?0T*#0H ('1I;65V86P 
M;W!3=6(H=&EM979A;"!T=C(I('L-"B  ("!T:6UE=F%L(')E<SL-"B  ("!R
M97,N;6EC<F]S96-O;F1S(#T ;6EC<F]S96-O;F1S("T ='8R+FUI8W)O<V5C
M;VYD<SL-"B  ("!L;VYG(&-A<G)Y(#T =&]396-O;F1S*')E<RYM:6-R;W-E
M8V]N9',L;7-E8RD[#0H ("  <F5S+FUI8W)O<V5C;VYD<R E/2 Q7S P,%\P
M,# [#0H ("  <F5S+G-E8V]N9', /2!S96-O;F1S("T ='8R+G-E8V]N9', 
M*R!C87)R>3L-"B  ("!R971U<FX <F5S.PT*("!]#0H-"GT-" T*=&EM979A
M;"!T;U1I;65V86PH;&]N9R!D=7)A=&EO;BP 5&EM955N:70 =6YI="D >PT*
M("!T:6UE=F%L('1V.PT*("!T=BYS96-O;F1S(#T =&]396-O;F1S*&1U<F%T
M:6]N+'5N:70I.PT*("!T=BYM:6-R;W-E8V]N9', /2!T;TUI8W)O<RAD=7)A
M=&EO;BQU;FET*2 E(#$P,#!?,# P.PT*("!R971U<FX ='8[#0I]#0H-"FQO
M;F< =&].86YO<RAT:6UE=F%L('1V*2![#0H (')E='5R;B!T;TYA;F]S*'1V
M+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK#0H ("  ("  ("!T;TYA;F]S
M*'1V+FUI8W)O<V5C;VYD<RQ4:6UE56YI="Y-:6-R;U-E8V]N9',I.PT*?0T*
M#0IL;VYG('1O36EC<F]S*'1I;65V86P ='8I('L-"B  <F5T=7)N('1O36EC
M<F]S*'1V+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK='8N;6EC<F]S96-O
M;F1S.PT*?0T*#0IL;VYG('1O36EL;&ES*'1I;65V86P ='8I('L-"B  <F5T
M=7)N('1O36EL;&ES*'1V+G-E8V]N9',L5&EM955N:70N4V5C;VYD<RDK#0H 
M("  ("  ("!T;TUI;&QI<RAT=BYM:6-R;W-E8V]N9',L5&EM955N:70N36EC
M<F]396-O;F1S*3L-"GT-" T*;&]N9R!T;U-E8V]N9',H=&EM979A;"!T=BD 
M>PT*("!R971U<FX ='8N<V5C;VYD<RMT;U-E8V]N9',H='8N;6EC<F]S96-O
M;F1S+%1I;655;FET+DUI8W)O4V5C;VYD<RD[#0I]#0H-"G5N:71T97-T('L-
M"B  =&EM979A;"!T=B ]('1O5&EM979A;" Q,# L<V5C*3L-"B  87-S97)T
M*"!T=BYS96-O;F1S(#T](#$P," I.PT*("!A<W-E<G0H('1V+FUI8W)O<V5C
M;VYD<R ]/2 P("D[#0H (&%S<V5R="  =&]-:6QL:7,H='8I(#T](#$P,%\P
M,#  *3L-"B  87-S97)T*"!T;TUI8W)O<RAT=BD /3T ,3 P7S P,%\P,#  
M*3L-"B  87-S97)T*"!T;TYA;F]S*'1V*2 ]/2 Q,#!?,# P7S P,%\P,#  
M*3L-" T*("!T:6UE=F%L('1V,CL-"B  ='8R+G-E8V]N9', /2 U.PT*("!T
M=C(N;6EC<F]S96-O;F1S(#T -SL-"B  87-S97)T*"!T;TUI8W)O<RAT=C(I
M(#T](#5?,# P7S P-R I.PT*("!A<W-E<G0H('1O36EC<F]S*'1V,B K('1V
M*2 ]/2 Q,#5?,# P7S P-R I.PT*("!A<W-E<G0H('1O36EC<F]S*'1V,B M
M('1V*2 ]/2 M.30Y.3DY.3, *3L-"GT-" T*=F5R<VEO;B H5VEN9&]W<RD 
M>PT*#0H ('!R:79A=&4 :6UP;W)T('-T9"YC+G=I;F1O=W,N=VEN9&]W<SL-
M" T*("!V;VED('-L965P*&QO;F< 9'5R871I;VXL(%1I;655;FET('5N:70I
M('L-"B  ("!L;VYG('0 /2!T;TUI;&QI<RAD=7)A=&EO;BQU;FET*3L-"B  
M("!T(#T =#YU:6YT+FUA>" _('5I;G0N;6%X(#H =#L-"B  ("!T(#T =#PP
M(#\ ," Z('0[#0H ("  4VQE97 H8V%S="AU:6YT*70I.PT*("!]#0H-"B  
M=F]I9"!S;&5E<"AT:6UE=F%L('1V*2![#0H ("  ;&]N9R!T(#T =&]-:6QL
M:7,H='8I.PT*("  ('0 /2!T/G5I;G0N;6%X(#\ =6EN="YM87  .B!T.PT*
M("  ('0 /2!T/#  /R P(#H =#L-"B  ("!3;&5E<"AC87-T*'5I;G0I="D[
M#0H ('T-" T*?2!E;'-E('9E<G-I;VX *&QI;G5X*2![#0H-"B  <')I=F%T
M92!I;7!O<G0 <W1D+F,N=&EM93L-" T*("!V;VED('-L965P*&QO;F< 9'5R
M871I;VXL(%1I;655;FET('5N:70I('L-"B  ("!L;VYG('0 /2!T;TUI8W)O
M<RAD=7)A=&EO;BQU;FET*3L-"B  ("!T(#T =#YU:6YT+FUA>" _('5I;G0N
M;6%X(#H =#L-"B  ("!T(#T =#PP(#\ ," Z('0[#0H ("  =7-L965P*&-A
M<W0H=6EN="ET*3L-"B  ?0T*#0H ('9O:60 <VQE97 H=&EM979A;"!T=BD 
M>PT*("  (&QO;F< =" ]('1O36EC<F]S*'1V*3L-"B  ("!T(#T =#YU:6YT
M+FUA>" _('5I;G0N;6%X(#H =#L-"B  ("!T(#T =#PP(#\ ," Z('0[#0H 
M("  =7-L965P*&-A<W0H=6EN="ET*3L-"B  ?0T*#0I]#0H-"G5N:71T97-T
M('L-"B  <VQE97 H,BQS96,I.PT*("!S;&5E<"AT;U1I;65V86PH,S L;7-E
'8RDI.PT*?0``
`
end
Feb 28 2005