You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by "John E. Conlon" <jc...@verticon.com> on 2004/01/24 06:31:47 UTC

Mapping duration

Would like to map/bind a xsd:Duration to a Java long primative that
represents a delay or duration in milliseconds. I see that there is a
Duration class, but don't see a way for me to get a total number of
milliseconds from it.

Any ideas?

thanks,
John Conlon


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by "John E. Conlon" <jc...@verticon.com>.
Yes it is ugly. If one looks at:
http://www.w3.org/TR/xmlschema-2/#duration
& 
http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes
 the Castor method is a far cry from the spec. 

The good thing for my application is that my durations are typically
under an hour. 

For now I will just have to document this for the users and move on.

thanks again,
John



On Mon, 2004-01-26 at 17:12, Jochen Wiedmann wrote:
> John E. Conlon wrote:
> 
> > Yes it is a bag of worms. I have been using Castor's Duration. Wonder how they are doing it? 
> 
> Just been looking into the Castor CVS. IMO it's a really ugly hack:
> 
>    public long toLong() {
>          long result = 0;
>          //30.42 days in a month (365/12)
>          //Horner method
>          result = ( (long) ( ((((( (_year*12L) +_month ) * 30.42
>                                      +_day)*24L
>                                      +_hour)*60L
>                                      +_minute)*60L
>                                      +_second)*1000L
>                                      +_millisecond));
> 
>          result = isNegative()? -result : result;
>          return result;
>      }
> 
> 
> > May continue using Castor's Duration with JAXMe. If it is viable, how
> > would we be able to include it in the JAXMe?
> 
> The Castor license would permit to do so. But, given the above code (which 
> is more a heuristic than an algorithm) I think it isn't worth the trouble 
> and we'd pick up more problems and trouble than solving.
> 
> If the user wants to live with such calculations, he can do for himself. We 
> should only implement specified API's in the sense of the Duration class 
> posted by Ias on this list or otherwise trustable. My opinion of course, and 
>   subject to discussion.
> 
> 
> Jochen
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: jaxme-dev-help@ws.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by Jochen Wiedmann <jo...@ispsoft.de>.
John E. Conlon wrote:

> Yes it is a bag of worms. I have been using Castor's Duration. Wonder how they are doing it? 

Just been looking into the Castor CVS. IMO it's a really ugly hack:

   public long toLong() {
         long result = 0;
         //30.42 days in a month (365/12)
         //Horner method
         result = ( (long) ( ((((( (_year*12L) +_month ) * 30.42
                                     +_day)*24L
                                     +_hour)*60L
                                     +_minute)*60L
                                     +_second)*1000L
                                     +_millisecond));

         result = isNegative()? -result : result;
         return result;
     }


> May continue using Castor's Duration with JAXMe. If it is viable, how
> would we be able to include it in the JAXMe?

The Castor license would permit to do so. But, given the above code (which 
is more a heuristic than an algorithm) I think it isn't worth the trouble 
and we'd pick up more problems and trouble than solving.

If the user wants to live with such calculations, he can do for himself. We 
should only implement specified API's in the sense of the Duration class 
posted by Ias on this list or otherwise trustable. My opinion of course, and 
  subject to discussion.


Jochen

---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by "John E. Conlon" <jc...@verticon.com>.
Hi Jochen,

On Mon, 2004-01-26 at 15:16, Jochen Wiedmann wrote:
> Hi, John,
> 
> thanks for the tests and the hint for a bug. I've fixed the handling of 
> absent values in Duration.java again and added your tests (slightly modified 
> ) to MarshallerTest.java.
> 
> 
> > Concerning the last test. I may not have communicated the issue of the
> > returning the 'Total' duration time as a millisecond very clearly.  The
> > following are what one would expect:
> 
> I am sorry, but I won't do any work on that issue, as long as the JAXB 
> specification doesn't clearly specify the requirements. Reason is, that 
> these cannot be calculated precisely in all cases. (Days may have an 
> additional second, months may have 28-31 days, years may have 365-366 days.)
Yes it is a bag of worms. I have been using Castor's Duration. Wonder how they are doing it? 

> If you are interested to work on that, I am ready to discuss the issues here 
> and forward patches, though.
May continue using Castor's Duration with JAXMe. If it is viable, how
would we be able to include it in the JAXMe?
 John


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by Jochen Wiedmann <jo...@ispsoft.de>.
Hi, John,

thanks for the tests and the hint for a bug. I've fixed the handling of 
absent values in Duration.java again and added your tests (slightly modified 
) to MarshallerTest.java.


> Concerning the last test. I may not have communicated the issue of the
> returning the 'Total' duration time as a millisecond very clearly.  The
> following are what one would expect:

I am sorry, but I won't do any work on that issue, as long as the JAXB 
specification doesn't clearly specify the requirements. Reason is, that 
these cannot be calculated precisely in all cases. (Days may have an 
additional second, months may have 28-31 days, years may have 365-366 days.)

If you are interested to work on that, I am ready to discuss the issues here 
and forward patches, though.


Jochen


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by "John E. Conlon" <jc...@verticon.com>.
Thanks for your fast response. 


Ran following tests on latest cvs Duration.

  /**
   * Tests that toString returns an appropriate xsd:duration value
   */
  public void testExplicitToString() {
    String dur1 = "P0Y0M0DT0H2M60S";
    Duration duration1 = Duration.valueOf(dur1);
    String actualReturn = duration1.toString();
    assertEquals("return value not as expected", dur1, actualReturn);
  }

  /**
   * Tests that toString returns an appropriate xsd:duration value
   */
  public void testImplicitToString() {
    String dur2 = "PT2M60S";
    Duration duration2 = Duration.valueOf(dur2);
    String actualReturn = duration2.toString();
    assertEquals("return value not as expected ", dur2, actualReturn);
  }
  
  /**
   * Test that getMillis returns the total time of duration in
milliseconds
   */
  public void testMillis() {
    String dur2 = "PT2M60S";
    Duration duration2 = Duration.valueOf(dur2);
    long actualReturn = duration2.getMillis();
    assertEquals("return value not as expected ", 180000l,
actualReturn);
  }

First test passed the second two failed. 
The second test is returning an unexpected result which is
P-1Y-1M-1DT-1H2M60S which my parser is telling me is invalid.

Concerning the last test. I may not have communicated the issue of the
returning the 'Total' duration time as a millisecond very clearly.  The
following are what one would expect:

Duration =PT2M60S Miliseconds =180000
Duration =P10YT2M60S Miliseconds =315394740000

thanks again,
John


On Mon, 2004-01-26 at 10:44, Jochen Wiedmann wrote:
> John E. Conlon wrote:
> 
> > <IMHO> one needs the total time in miliseconds as a standard way to set
> > a duration in Java. (Using the java.util.Timer) Therefore I would
> > propose the mapping for xsd:duration as into a long.  It would be
> > sufficient to just add a static method to the Duration class to parse a
> > Duration string and return a long. And by the way - this is what Castor
> > is doing with their Duration type.
> > </IMHO>
> > 
> > 
> > One more thing I noticed while testing duration even though "PT2M60S" is
> > a valid duration -
> >  
> > junit.framework.AssertionFailedError: Duration.valueOf("PT2M60S") failed
> > with java.text.ParseException: MessageFormat parse error!
> 
> After thinking a little bit, I tend to agree with you and have changed the 
> Duration class to have methods
> 
>      int getSeconds();
>      long getMillis();
> 
> Likewise, I have changed the parser to accept durations that have, for 
> example, years omitted. This applies to the CVS version. I'd be glad, if you 
> would checkout and test it.
> 
> 
> Jochen
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: jaxme-dev-help@ws.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by Jochen Wiedmann <jo...@ispsoft.de>.
John E. Conlon wrote:

> <IMHO> one needs the total time in miliseconds as a standard way to set
> a duration in Java. (Using the java.util.Timer) Therefore I would
> propose the mapping for xsd:duration as into a long.  It would be
> sufficient to just add a static method to the Duration class to parse a
> Duration string and return a long. And by the way - this is what Castor
> is doing with their Duration type.
> </IMHO>
> 
> 
> One more thing I noticed while testing duration even though "PT2M60S" is
> a valid duration -
>  
> junit.framework.AssertionFailedError: Duration.valueOf("PT2M60S") failed
> with java.text.ParseException: MessageFormat parse error!

After thinking a little bit, I tend to agree with you and have changed the 
Duration class to have methods

     int getSeconds();
     long getMillis();

Likewise, I have changed the parser to accept durations that have, for 
example, years omitted. This applies to the CVS version. I'd be glad, if you 
would checkout and test it.


Jochen


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by "John E. Conlon" <jc...@verticon.com>.
Yes I saw the getSeconds() method in the Duration class. It returns the
seconds component of the Duration. If Duration is PT2M60S it will return
60 as a double.  What I am trying to do is schedule the a task for
running after a particular duration or delay using a java.util.Timer
which takes a delay in miliseconds as a long like this:

 timer.schedule(masterTask, delay);

<IMHO> one needs the total time in miliseconds as a standard way to set
a duration in Java. (Using the java.util.Timer) Therefore I would
propose the mapping for xsd:duration as into a long.  It would be
sufficient to just add a static method to the Duration class to parse a
Duration string and return a long. And by the way - this is what Castor
is doing with their Duration type.
</IMHO>


One more thing I noticed while testing duration even though "PT2M60S" is
a valid duration -
 
junit.framework.AssertionFailedError: Duration.valueOf("PT2M60S") failed
with java.text.ParseException: MessageFormat parse error!


John



 On Sat, 2004-01-24 at 12:01, Jochen Wiedmann wrote:
> John E. Conlon wrote:
> 
> > Would like to map/bind a xsd:Duration to a Java long primative that
> > represents a delay or duration in milliseconds. I see that there is a
> > Duration class, but don't see a way for me to get a total number of
> > milliseconds from it.
> 
> As the JAXB specification doesn not specify how durations are being
> implemented, I have modelled the seconds as a double. In other words,
> the milliseconds are the fraction of the seconds. Probably a wrong
> decision that requires rethinking.
> 
> 
> Jochen
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Re: Mapping duration

Posted by Jochen Wiedmann <jo...@ispsoft.de>.
Ias wrote:

> FYI, JAXP 1.3 defines javax.xml.datatype.Duration for xsd:Duration. The
> attached files are APIs from its public draft. Probably we can get some idea
> for designing and implementing logics for mapping xsd:Duration to Java.

Thanks for the info. Had a short look at it and found it quite complex to
implement. I wont do that. Most probably we have to wait until some external
open source implementation becomes available and we may copy that.


Jochen

---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


RE: Mapping duration

Posted by Ias <ia...@tmax.co.kr>.
FYI, JAXP 1.3 defines javax.xml.datatype.Duration for xsd:Duration. The
attached files are APIs from its public draft. Probably we can get some idea
for designing and implementing logics for mapping xsd:Duration to Java.

Regards,

Ias

=========================================================
Lee, Changshin (Korean name)
Ias (International name)
               Company Web Site: http://www.tmax.co.kr
               Personal Web Site: http://www.iasandcb.pe.kr
---------------------------------------------------------
JSR 201, JSR 204 and JSR 222 EG member
Apache Axis (JAX-RPC and SAAJ) and JaxMe (JAXB) committer
R&D Institute
Tmax Soft, Inc.
========================================================= 

> -----Original Message-----
> From: Jochen Wiedmann [mailto:joe@ispsoft.de] 
> Sent: Sunday, January 25, 2004 3:02 AM
> To: jconlon@verticon.com
> Cc: jaxme-dev@ws.apache.org
> Subject: Re: Mapping duration
> 
> John E. Conlon wrote:
> 
> > Would like to map/bind a xsd:Duration to a Java long primative that 
> > represents a delay or duration in milliseconds. I see that 
> there is a 
> > Duration class, but don't see a way for me to get a total number of 
> > milliseconds from it.
> 
> As the JAXB specification doesn not specify how durations are 
> being implemented, I have modelled the seconds as a double. 
> In other words, the milliseconds are the fraction of the 
> seconds. Probably a wrong decision that requires rethinking.
> 
> 
> Jochen
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: jaxme-dev-help@ws.apache.org
> 

Re: Mapping duration

Posted by Jochen Wiedmann <jo...@ispsoft.de>.
John E. Conlon wrote:

> Would like to map/bind a xsd:Duration to a Java long primative that
> represents a delay or duration in milliseconds. I see that there is a
> Duration class, but don't see a way for me to get a total number of
> milliseconds from it.

As the JAXB specification doesn not specify how durations are being
implemented, I have modelled the seconds as a double. In other words,
the milliseconds are the fraction of the seconds. Probably a wrong
decision that requires rethinking.


Jochen


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org