Jump to content
PizzaProgram

Convert ISuperObject TDateTime to RFC3339

Recommended Posts

I've just realized that my code is not good enough, because it contains 3 digits after the second part, not just 2, as RFC3339 specifies.

function ftdt_ISO(const dt: TDateTime; RFC3339: Boolean = False): string;   // ISO8601 formátum: "2022-04-19T11:30:26.090+02:00"  RFC3339 formátum: "2022-04-19T11:30:26.09+02:00"
const
    RFCDateLongTimeMask = 'yyyy-mm-dd"T"hh:nn:ss.zz' ;
    ISODateLongTimeMask = RFCDateLongTimeMask   + 'z' ;
var
    TZINFO  : TTimeZoneInformation;
    _b      : Double;
begin
    case Windows.GetTimeZoneInformation(TZINFO) of
        TIME_ZONE_ID_STANDARD:  _b :=  TZINFO.Bias / (60*24);
        TIME_ZONE_ID_DAYLIGHT:  _b := (TZINFO.Bias+ TZINFO.DaylightBias) / (60*24);
    else                        _b := 0;
    end;
    Result := FormatDateTime( IfThen(RFC3339, RFCDateLongTimeMask, ISODateLongTimeMask), dt)
        + ifThen(_b <= 0, '+', '-') + FormatDateTime( 'hh:nn', _b) ;
end;

 

I'm using it this way:

var o : ISuperObject;
begin ...
  o := SO();
  o.S['started'] := ftdt_ISO( myTime, True ); // result: 2022-11-19T18:18:18.123+01:00 instead of ..18.12+..

 

Is there a working way to do it under Delphi 7 ?

Thanks for the help! 🙂

 

 

Share this post


Link to post

Date/time handling in ISuperObject was lacking, so I added it myself, from the changes at the top of the OverbyteIcsSuperObject unit:

 

Added new datetime get/set type for Delphi TDateTime, saves as ISO 8601/RFC3339 string:
 obj.AsDateTime,  obj.AsObject.DT['foo'],  obj.DT['foo'],  obj.AsArray.DT[0]

 

I also improved the parser to return error information upon failure instead of a nil object. 

 

Angus

 

Share this post


Link to post
19 hours ago, Angus Robertson said:

obj.AsDateTime

Yes, I know that, thank you for doing it! That was the first thing I've tried.

But that format gets rejected by our Government's server. 😞 (Our dictatorial gov. su**s.)

 

{"sentValue":"2022-11-20T14:52:52+0100","errorKey":"MissmatchType","errorMessage":"Field Type not appropriate!"}

 

They are most likely using Java. We (programmers) have no idea why it getting rejected.

In their examples they always use this format:

2022-11-11T12:34:56.78+01:00

 

RFC3339 is more strict than ISO.

 

Edit:

1. max frac. 2 digits after second are allowed

2. there must be a ":" sign for timezone part.

Edited by PizzaProgram

Share this post


Link to post

Found it, here is the fix for OverbyteICSUtils.pas line 1676:

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// V8.62 Get time zone bias as string, ie -0730 
// V8.71 Get time zone bias as RFC3339 compatible string, ie -07:30 
function IcsGetLocalTZBiasStr: String;
var
    Bias: Integer;
    Sign: String;
begin
    Bias := IcsGetLocalTimeZoneBias;
    if Bias > 0 then
        Sign := '-'
    else
        Sign := '+';
    Bias := Abs(Bias);
    Result := Format('%s%.2u%s%.2u', [Sign, Bias div 60, ':', Bias mod 60]);
end;

 ... also changed %d = decimal to %u  = unsigned, because Bias can be negative. (I'm not sure, if it's necessary, but it's better to make sure.)

Tested it, works nice with our current +01:00 winter-timezone.

 

Cheers 😉

Share this post


Link to post

But the function you changed also builds the time zone of emails and HTTP headers, where a colon is breaking, probably, 

 

Unfortunately I do break ICS occasionally, but take a lot of care to avoid doing so.

 

Angus

 

Share this post


Link to post
23 hours ago, Angus Robertson said:

time zone of emails and HTTP headers

OFF:

Oh... now I understand! Sorry, never thought about that.

Using ICS since this few month only.

 

 (PS: Sorry I didn't sent you a postcard yet, I hate Hungarian government post offices and rarely leave my house. But ICS is great! I love it and I'm thinking about You every day and how I could repay You for this great work...)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×