How to handle Period with negative length?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to handle Period with negative length?

Gemmill, Craig

Hi all-

 

I’m new to using Joda-time, but I have a question about using Period with negative times.  According to the XML Schema xs:duration datatype, and as far as I can tell, the ISO8601 format, a negative duration would be expressed with a leading minus sign.  For example, minus 120 days would be “–P120D”.  However, I tried a quick ‘roundtrip’ test using org.joda.time.Period and org.joda.time.format.ISOPeriodFormat, and this doesn’t seem to work:

 

import org.joda.time.Period;

import org.joda.time.format.ISOPeriodFormat;

 

public class PeriodTester

{

  public static void main(String[] args)

  {

    Period p = new Period(-8);

    String s = ISOPeriodFormat.standard().print(p);

    Period p1 = ISOPeriodFormat.standard().parsePeriod(s);

    System.out.println("p:"+p+"; ->s:"+s+"; ->p1:"+p1);

  }

}

 

I would expect this to output:

p:-PT0.008S; ->s:-PT0.008S; ->p1:-PT0.008S

 

But instead I get

p:PT-0.008S; ->s:PT-0.008S; ->p1:PT0.008S

 

So this is not able to survive an encode/decode.  It appears that the encoder is not putting the minus sign in the right place, but the decoder is expecting a leading sign?

 

Is there a better way to encode the Period so that it uses a leading minus sign?

 

Thanks!

 


------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to handle Period with negative length?

jodastephen
Joda-Time doe snot support this XML encoding with a leading sign. Its
not a standard ISO-8601 format AFAICT. Instead, we use P-120D.
Stephen

On 19 June 2013 15:31, Gemmill, Craig <[hidden email]> wrote:

> Hi all-
>
>
>
> I’m new to using Joda-time, but I have a question about using Period with
> negative times.  According to the XML Schema xs:duration datatype, and as
> far as I can tell, the ISO8601 format, a negative duration would be
> expressed with a leading minus sign.  For example, minus 120 days would be
> “–P120D”.  However, I tried a quick ‘roundtrip’ test using
> org.joda.time.Period and org.joda.time.format.ISOPeriodFormat, and this
> doesn’t seem to work:
>
>
>
> import org.joda.time.Period;
>
> import org.joda.time.format.ISOPeriodFormat;
>
>
>
> public class PeriodTester
>
> {
>
>   public static void main(String[] args)
>
>   {
>
>     Period p = new Period(-8);
>
>     String s = ISOPeriodFormat.standard().print(p);
>
>     Period p1 = ISOPeriodFormat.standard().parsePeriod(s);
>
>     System.out.println("p:"+p+"; ->s:"+s+"; ->p1:"+p1);
>
>   }
>
> }
>
>
>
> I would expect this to output:
>
> p:-PT0.008S; ->s:-PT0.008S; ->p1:-PT0.008S
>
>
>
> But instead I get
>
> p:PT-0.008S; ->s:PT-0.008S; ->p1:PT0.008S
>
>
>
> So this is not able to survive an encode/decode.  It appears that the
> encoder is not putting the minus sign in the right place, but the decoder is
> expecting a leading sign?
>
>
>
> Is there a better way to encode the Period so that it uses a leading minus
> sign?
>
>
>
> Thanks!
>
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to handle Period with negative length?

Gemmill, Craig
Thanks for the quick reply!

I don't have a copy of the ISO8601 standard, unfortunately it costs $134.  But the W3C XML Schema datatype xs:duration uses the same format (see http://www.w3.org/TR/xmlschema-2/#duration), and they indicate that the sign should lead the entire string.  Is the XML Schema datatype just a subset of the ISO8601 format that specifies a particular placement of the sign?

Is there a way I could build my own PeriodFormatterBuilder to define this?  I have been poking around with it, but I don't see an API for controlling where the minus sign is placed.  One of the threads from a few years ago (http://sourceforge.net/mailarchive/message.php?msg_id=28027662) seems to indicate that the fields are independently signed, so I'm not sure how easy it would be to aggregate this into an overall sign - you'd potentially have to sum the fields to determine the overall sign.

Thanks for any ideas!
-Craig


-----Original Message-----
From: Stephen Colebourne [mailto:[hidden email]]
Sent: Wednesday, June 19, 2013 10:48 AM
To: Discussion of the Joda project
Subject: Re: [Joda-interest] How to handle Period with negative length?

Joda-Time doe snot support this XML encoding with a leading sign. Its not a standard ISO-8601 format AFAICT. Instead, we use P-120D.
Stephen

On 19 June 2013 15:31, Gemmill, Craig <[hidden email]> wrote:

> Hi all-
>
>
>
> I'm new to using Joda-time, but I have a question about using Period
> with negative times.  According to the XML Schema xs:duration
> datatype, and as far as I can tell, the ISO8601 format, a negative
> duration would be expressed with a leading minus sign.  For example,
> minus 120 days would be "-P120D".  However, I tried a quick
> 'roundtrip' test using org.joda.time.Period and
> org.joda.time.format.ISOPeriodFormat, and this doesn't seem to work:
>
>
>
> import org.joda.time.Period;
>
> import org.joda.time.format.ISOPeriodFormat;
>
>
>
> public class PeriodTester
>
> {
>
>   public static void main(String[] args)
>
>   {
>
>     Period p = new Period(-8);
>
>     String s = ISOPeriodFormat.standard().print(p);
>
>     Period p1 = ISOPeriodFormat.standard().parsePeriod(s);
>
>     System.out.println("p:"+p+"; ->s:"+s+"; ->p1:"+p1);
>
>   }
>
> }
>
>
>
> I would expect this to output:
>
> p:-PT0.008S; ->s:-PT0.008S; ->p1:-PT0.008S
>
>
>
> But instead I get
>
> p:PT-0.008S; ->s:PT-0.008S; ->p1:PT0.008S
>
>
>
> So this is not able to survive an encode/decode.  It appears that the
> encoder is not putting the minus sign in the right place, but the
> decoder is expecting a leading sign?
>
>
>
> Is there a better way to encode the Period so that it uses a leading
> minus sign?
>
>
>
> Thanks!
>
>
>
>
> ----------------------------------------------------------------------
> -------- This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to handle Period with negative length?

jodastephen
I'd be happy to see a pull request with tests that allowed the period
formatter builder to be extended to support an option for a single
sign up front. However, it does get complicated as each field is
independently signed as you say, thus calculating what a prefix sign
might be is more complicated.

Stephen


On 19 June 2013 16:00, Gemmill, Craig <[hidden email]> wrote:

> Thanks for the quick reply!
>
> I don't have a copy of the ISO8601 standard, unfortunately it costs $134.  But the W3C XML Schema datatype xs:duration uses the same format (see http://www.w3.org/TR/xmlschema-2/#duration), and they indicate that the sign should lead the entire string.  Is the XML Schema datatype just a subset of the ISO8601 format that specifies a particular placement of the sign?
>
> Is there a way I could build my own PeriodFormatterBuilder to define this?  I have been poking around with it, but I don't see an API for controlling where the minus sign is placed.  One of the threads from a few years ago (http://sourceforge.net/mailarchive/message.php?msg_id=28027662) seems to indicate that the fields are independently signed, so I'm not sure how easy it would be to aggregate this into an overall sign - you'd potentially have to sum the fields to determine the overall sign.
>
> Thanks for any ideas!
> -Craig
>
>
> -----Original Message-----
> From: Stephen Colebourne [mailto:[hidden email]]
> Sent: Wednesday, June 19, 2013 10:48 AM
> To: Discussion of the Joda project
> Subject: Re: [Joda-interest] How to handle Period with negative length?
>
> Joda-Time doe snot support this XML encoding with a leading sign. Its not a standard ISO-8601 format AFAICT. Instead, we use P-120D.
> Stephen
>
> On 19 June 2013 15:31, Gemmill, Craig <[hidden email]> wrote:
>> Hi all-
>>
>>
>>
>> I'm new to using Joda-time, but I have a question about using Period
>> with negative times.  According to the XML Schema xs:duration
>> datatype, and as far as I can tell, the ISO8601 format, a negative
>> duration would be expressed with a leading minus sign.  For example,
>> minus 120 days would be "-P120D".  However, I tried a quick
>> 'roundtrip' test using org.joda.time.Period and
>> org.joda.time.format.ISOPeriodFormat, and this doesn't seem to work:
>>
>>
>>
>> import org.joda.time.Period;
>>
>> import org.joda.time.format.ISOPeriodFormat;
>>
>>
>>
>> public class PeriodTester
>>
>> {
>>
>>   public static void main(String[] args)
>>
>>   {
>>
>>     Period p = new Period(-8);
>>
>>     String s = ISOPeriodFormat.standard().print(p);
>>
>>     Period p1 = ISOPeriodFormat.standard().parsePeriod(s);
>>
>>     System.out.println("p:"+p+"; ->s:"+s+"; ->p1:"+p1);
>>
>>   }
>>
>> }
>>
>>
>>
>> I would expect this to output:
>>
>> p:-PT0.008S; ->s:-PT0.008S; ->p1:-PT0.008S
>>
>>
>>
>> But instead I get
>>
>> p:PT-0.008S; ->s:PT-0.008S; ->p1:PT0.008S
>>
>>
>>
>> So this is not able to survive an encode/decode.  It appears that the
>> encoder is not putting the minus sign in the right place, but the
>> decoder is expecting a leading sign?
>>
>>
>>
>> Is there a better way to encode the Period so that it uses a leading
>> minus sign?
>>
>>
>>
>> Thanks!
>>
>>
>>
>>
>> ----------------------------------------------------------------------
>> -------- This SF.net email is sponsored by Windows:
>>
>> Build for Windows Store.
>>
>> http://p.sf.net/sfu/windows-dev2dev
>> _______________________________________________
>> Joda-interest mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/joda-interest
>>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to handle Period with negative length?

Gemmill, Craig
Ok, I will investigate if I can make a general solution.  I may have a workaround for my usage, but I probably wouldn't suggest anything to the project unless I had a more general solution.  Thanks for your time!
-Craig

-----Original Message-----
From: Stephen Colebourne [mailto:[hidden email]]
Sent: Wednesday, June 19, 2013 11:38 AM
To: Discussion of the Joda project
Subject: Re: [Joda-interest] How to handle Period with negative length?

I'd be happy to see a pull request with tests that allowed the period formatter builder to be extended to support an option for a single sign up front. However, it does get complicated as each field is independently signed as you say, thus calculating what a prefix sign might be is more complicated.

Stephen


On 19 June 2013 16:00, Gemmill, Craig <[hidden email]> wrote:

> Thanks for the quick reply!
>
> I don't have a copy of the ISO8601 standard, unfortunately it costs $134.  But the W3C XML Schema datatype xs:duration uses the same format (see http://www.w3.org/TR/xmlschema-2/#duration), and they indicate that the sign should lead the entire string.  Is the XML Schema datatype just a subset of the ISO8601 format that specifies a particular placement of the sign?
>
> Is there a way I could build my own PeriodFormatterBuilder to define this?  I have been poking around with it, but I don't see an API for controlling where the minus sign is placed.  One of the threads from a few years ago (http://sourceforge.net/mailarchive/message.php?msg_id=28027662) seems to indicate that the fields are independently signed, so I'm not sure how easy it would be to aggregate this into an overall sign - you'd potentially have to sum the fields to determine the overall sign.
>
> Thanks for any ideas!
> -Craig
>
>
> -----Original Message-----
> From: Stephen Colebourne [mailto:[hidden email]]
> Sent: Wednesday, June 19, 2013 10:48 AM
> To: Discussion of the Joda project
> Subject: Re: [Joda-interest] How to handle Period with negative length?
>
> Joda-Time doe snot support this XML encoding with a leading sign. Its not a standard ISO-8601 format AFAICT. Instead, we use P-120D.
> Stephen
>
> On 19 June 2013 15:31, Gemmill, Craig <[hidden email]> wrote:
>> Hi all-
>>
>>
>>
>> I'm new to using Joda-time, but I have a question about using Period
>> with negative times.  According to the XML Schema xs:duration
>> datatype, and as far as I can tell, the ISO8601 format, a negative
>> duration would be expressed with a leading minus sign.  For example,
>> minus 120 days would be "-P120D".  However, I tried a quick
>> 'roundtrip' test using org.joda.time.Period and
>> org.joda.time.format.ISOPeriodFormat, and this doesn't seem to work:
>>
>>
>>
>> import org.joda.time.Period;
>>
>> import org.joda.time.format.ISOPeriodFormat;
>>
>>
>>
>> public class PeriodTester
>>
>> {
>>
>>   public static void main(String[] args)
>>
>>   {
>>
>>     Period p = new Period(-8);
>>
>>     String s = ISOPeriodFormat.standard().print(p);
>>
>>     Period p1 = ISOPeriodFormat.standard().parsePeriod(s);
>>
>>     System.out.println("p:"+p+"; ->s:"+s+"; ->p1:"+p1);
>>
>>   }
>>
>> }
>>
>>
>>
>> I would expect this to output:
>>
>> p:-PT0.008S; ->s:-PT0.008S; ->p1:-PT0.008S
>>
>>
>>
>> But instead I get
>>
>> p:PT-0.008S; ->s:PT-0.008S; ->p1:PT0.008S
>>
>>
>>
>> So this is not able to survive an encode/decode.  It appears that the
>> encoder is not putting the minus sign in the right place, but the
>> decoder is expecting a leading sign?
>>
>>
>>
>> Is there a better way to encode the Period so that it uses a leading
>> minus sign?
>>
>>
>>
>> Thanks!
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> -
>> -------- This SF.net email is sponsored by Windows:
>>
>> Build for Windows Store.
>>
>> http://p.sf.net/sfu/windows-dev2dev
>> _______________________________________________
>> Joda-interest mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/joda-interest
>>
>
> ----------------------------------------------------------------------
> -------- This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
> ----------------------------------------------------------------------
> -------- This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...