The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,854 other subscribers

Archive for the ‘Delphi’ Category

ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)

Posted by jpluimers on 2011/07/19

Recently I needed a way of concerting back and forth ISO 8601 DateTime values used in XML from Delphi.

Thoug the Delphi DateUtils unit has some ISO 8601 features for calculating week numbers, you actually need to the XSBuiltIns unit for converting back and forth to ISO 8601 text representation of a DateTime.

I remembered answering the related In Delphi is there a function to convert XML date and time to TDateTime question on StackOverflow a while ago (and forgot that this was back in 2009 <g>).

ISO 8601 dates can either include a time-zone, or be in UTC, which is not part of that answer. So lets elaborate on that answer a bit now:

UTC times in ISO 8601 format end in a Z time zone designator like this:

    <Created>2011-06-29T17:01:45.505Z</Created>

The Z UTC indicator is basically a shortcut for a timezone offset of +00:00 or -00:00, effectively being a zero (or zulu) timezone.

Nonzero timezones start with an optional + or -, followed by the hours and minutes offset from UTC, for instance +01:00 for the Central European Time zone.

    <Created>2011-06-29T17:01:45.505+01:00</Created>

When you want historical time zones, then you need the Delphi TZDB interface to the historical TZ database.

To do the timezone calculations, I used the TimeZoneBias function from Indy, which is either in the IdGlobal unit (Indy <= version 9) or the IdGlobalProtocols unit (Indy 10 and up).

Conversion is done by using the TXSDateTime (that like all the XS conversion classes descends from TRemotableXS in the InvokeRegistry unit).

Most of the classes descending from TRemotableXS contain two methods: NativeToXS and XSToNative doing the underlying conversions.

Since I didn’t need the historical reference in the TZDB, this is the code that I came up with:

unit Iso8601Unit;

interface

type
  TIso8601 = class(TObject)
  public
    class function DateTimeFromIso8601(const Value: string): TDateTime; static;
    class function UtcDateTimeToIso8601(const Value: TDateTime): string; static;
    class function DateTimeToIso8601(const Value: TDateTime): string; static;
    class function UtcNow: TDateTime; static;
    class function ToUtc(const Value: TDateTime): TDateTime; static;
    class function FromUtc(const Value: TDateTime): TDateTime; static;
  end;

implementation

uses
  IdGlobalProtocols, {IdGlobal for Index   SysUtils,
  XSBuiltIns;

class function TIso8601.DateTimeFromIso8601(const Value: string): TDateTime;
begin
  with TXSDateTime.Create() do
  try
    XSToNative(value); // convert from WideString
    Result := AsDateTime; // convert to TDateTime  finally
  finally
    Free();
  end;
end;

class function TIso8601.UtcDateTimeToIso8601(const Value: TDateTime): string;
begin
  with TXSDateTime.Create() do
  try
    AsUTCDateTime := Value;
    Result := NativeToXS; // convert to WideString
  finally
    Free();
  end;
end;

class function TIso8601.DateTimeToIso8601(const Value: TDateTime): string;
begin
  with TXSDateTime.Create() do
  try
    AsDateTime := Value; // convert from TDateTime
    Result := NativeToXS; // convert to WideString
  finally
    Free();
  end;
end;

class function TIso8601.UtcNow: TDateTime;
begin
  Result := ToUtc(Now);
end;

class function TIso8601.ToUtc(const Value: TDateTime): TDateTime;
var
  Bias: TDateTime;
begin
  Bias := TimeZoneBias;
  Result := Value + TimeZoneBias;
end;

class function TIso8601.FromUtc(const Value: TDateTime): TDateTime;
var
  Bias: TDateTime;
begin
  Bias := TimeZoneBias;
  Result := Value - TimeZoneBias;
end;

end.

–jeroen

via In Delphi is there a function to convert XML date and time to TDateTime – Stack Overflow.

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | 7 Comments »

Delphi commandline oddity with double-quoted arguments

Posted by jpluimers on 2011/07/14

Boy, I was wrong. Somewhere in the back of my head I knew it.

ParamStr goes from zero (the name of the EXE) through ParamCount (the last parameter). Duh :)

 

I’ll keep this so you can have a good laugh:

 

When you add double quoted arguments to the commandline of your Delphi app, strange things can happen: the first parameter seems to be gone, while it is there.

It appears that the ParamCount/ParamStr logic fails here, and cannot be repaired because of backward compatibility.

Lets look at this commandline:

“..\bin\DumpCommandLine.exe” “C:\Program Files\test.xml” “C:\Program Files\meMySelfAndI.xml”

The demo code below will output something like this:

ParamCount:     2
ParamStr():
0       C:\develop\DumpCommandLine\bin\DumpCommandLine.exe
1       C:\Program Files\test.xml
CommandLine:
"..\bin\DumpCommandLine.exe" "C:\Program Files\test.xml" "C:\Program Files\meMySelfAndI.xml"
CommandLineStrings.Count:       3
CommandLineStrings[]:
0       ..\bin\DumpCommandLine.exe
1       C:\Program Files\test.xml
2       C:\Program Files\meMySelfAndI.xml

You see that regular ParamCount/ParamStr calls will mis the “C:\Program Files\test.xml” parameter.
But getting it through CommandLineStrings will correctly get it.

This is the dump code:

unit CommandlineDemoUnit;

interface

procedure DumpCommandLineToConsole;

implementation

uses
  Classes, CommandlineUnit;

procedure DumpCommandLineToConsole;
var
  CommandLineStrings: TStrings;
  I: Integer;
begin
  Writeln('ParamCount:', #9, ParamCount);
  Writeln('ParamStr():');
  for I := 0 to ParamCount - 1 do
    Writeln(I, #9, ParamStr(I));
  Writeln('CommandLine:');
  Writeln(CommandLine);
  CommandLineStrings := CreateCommandLineStrings();
  Writeln('CommandLineStrings.Count:', #9, CommandLineStrings.Count);
  Writeln('CommandLineStrings[]:');
  try
    for I := 0 to CommandLineStrings.Count - 1 do
      Writeln(I, #9, CommandLineStrings[I]);
  finally
    CommandLineStrings.Free;
  end;
end;

end.

And this the code to get the CommandLine and CommandLineStrings, which will fill a TStrings result using the CommaText property (it uses a default QuoteChar of of double quote #34 and Delimiter of space #32, this will work nicely):

unit CommandlineUnit;

interface

uses
  Classes;

function CommandLine: string;
function CreateCommandLineStrings: TStrings;

implementation

uses
  Windows;

function CommandLine: string;
begin
  Result := GetCommandLine();
end;

function CreateCommandLineStrings: TStrings;
begin
  Result := TStringList.Create();
  Result.CommaText := CommandLine;
end;

end.

Note that you need to manually free the TStrings object to avoid memory leaks.

–jeroen

Posted in Delphi, Development, Software Development | 13 Comments »

Code Quality: the rate of WTFs

Posted by jpluimers on 2011/07/06

OSnews has an interesting view on code quality: the number of WTFs/minute.

I know it is from 2008, but it is so true, so I’m glad I re-found it.

–jeroen

via: wtfm.jpg (500×471).

Posted in .NET, Agile, Delphi, Development, Opinions, Software Development | Leave a Comment »

winapi – Best way to do non-flickering, segmented graphics updates in Delphi? – Stack Overflow

Posted by jpluimers on 2011/07/05

Recently, Jon Lennart Aasenden (of Surface Library fame) asked a nice winapi – Best way to do non-flickering, segmented graphics updates in Delphi question on StackOverflow.

Though the question is marked Delphi, the boundaries and solution very generic, and apply to any graphics library or GUI you develop: Windows, Mac, iOS, et cetera:

  • Avoid double buffering when using GUI connections
  • Draw only what you need
  • Avoid redrawing whenever possible (for instance by letting the OS perform scrolling for you)
–jeroen

Posted in .NET, Delphi, Development, Software Development, xCode/Mac/iPad/iPhone/iOS/cocoa | 2 Comments »

The importance of static links on the web: many (really) old links still work

Posted by jpluimers on 2011/06/30

Doing a re-haul of a Delphi project from almost 15 years ago, and some modifications a few year later, I came across a list of favourites I used back then.

It also reminded me when beginning that project, there was no Google Search; you had Yahoo SearchHotBot and later on Northern Light, but AltaVista was the best.
Finding your way around was much harder, but luckily later on Google Search was there :)

So here is a sample of how static the web is:

Few suffer from link rot: I was pretty amazed that most of the links still work!

–jeroen

Posted in Delphi, Development, Power User, Software Development | 4 Comments »

Weaving: Source code, IL, ByteCode, Native

Posted by jpluimers on 2011/06/28

Weaving extends your program adding new functionality.

It can be at different levels for difference purposes (AOP, Debugging, Automated Testing, etc).

This .net – What is IL Weaving? question on StackOverflow contains a few pointers to interesting reading material.

–jeroen

Posted in .NET, Debugging, Delphi, Development, Software Development | Leave a Comment »

Delphi and COBOL syntax highlighters

Posted by jpluimers on 2011/06/21

I’ve been working on a project that uses both COBOL and Delphi.

For documentation purposes, Syntax Highlighted code makes your code so much easier to read.

Delphi has GExperts for source code export (in either HTML or RTF), but it took me a while to find a good syntax highlighter for COBOL.

I finally found a COBOL syntax highighter at tohtml.com: it exports to HTML.

I’m glad I found that site, as they have a ton of syntax highlighters, divided into groups.

Quite amusing to see COBOL classified as ‘rare’ (given that it has one of the largest code bases in the world).

This is what they support:

  • main: Java
  • main: C
  • main: Visual Basic
  • main: PHP
  • main: C++
  • main: Perl
  • main: Python
  • main: C#
  • main: Ruby
  • main: JS.NET
  • main: VB.NET
  • main: Pascal
  • main: JavaScript
  • inet: html
  • inet: css
  • inet: css for html
  • inet: css for svg
  • inet: jsp
  • inet: xhtml transitional
  • inet: xhtml strict
  • inet: xhtml frameset
  • inet: asp – VBScript
  • inet: asp – JavaScript
  • inet: asp – PerlScript
  • inet: SVG 1.0
  • inet: ColdFusion
  • inet: ActionScript
  • inet: VBScript
  • xml: xml
  • xml: dtd
  • xml: xslt 1.0
  • xml: XML Schema
  • xml: Relax NG
  • xml: xlink
  • database: Clarion
  • database: Clipper
  • database: FoxPro
  • database: SQLJ (Java sql)
  • database: Paradox
  • database: SQL, PL/SQL
  • database: MySQL
  • scripts: Batch/Config.sys/NTcmd
  • scripts: sh/ksh/bash script
  • scripts: Apache httpd.conf
  • scripts: Config, INI and CTL
  • scripts: Colorer HRC
  • scripts: Colorer HRD
  • scripts: Delphi form
  • scripts: Java Compiler Compiler
  • scripts: Java properties
  • scripts: Lex
  • scripts: YACC
  • scripts: makefile
  • scripts: Regedit
  • scripts: Resources
  • scripts: TeX
  • scripts: OpenVMS DCL
  • scripts: VRML
  • scripts.install: RAR Install Script
  • scripts.install: Nullsoft Install Script
  • scripts.install: InnoSetup script
  • scripts.install: IS script
  • rare: ASM
  • rare: 1C
  • rare: Ada
  • rare: ABAP/4
  • rare: AutoIt 2.x
  • rare: AWK
  • rare: Dssp
  • rare: ADSP-21xx Asm
  • rare: Baan
  • rare: Cache/Open-M
  • rare: Cobol
  • rare: Eiffel
  • rare: Forth
  • rare: Fortran
  • rare: Haskell
  • rare: Icon
  • rare: IDL
  • rare: Lisp
  • rare: MatLab
  • rare: Modula2 and Oberon2
  • rare: PicAsm
  • rare: Rexx
  • rare: Standard ML
  • rare: OCaml
  • rare: Tcl/Tk
  • rare: Sicstus Prolog
  • rare: Turbo Prolog
  • rare: Verilog HDL
  • rare: VHDL
  • rare: z80asm
  • rare: asm80
  • rare: 8051 asm
  • rare: AVR asm
  • other: files.bbs
  • other: Diff/Patch
  • other: message
  • other: plain text
  • other: default type

--jeroen

Posted in COBOL, Delphi, Development, LISP, Software Development | 6 Comments »

SVN tree to source code of Marco Cantu’s Delphi Books

Posted by jpluimers on 2011/06/09

If you love the Delphi books by Marco Cantu as much as I do, then you certainly will love that Marco has created a public SVN repository at code.marcocantu.com containing all the samples of his Delphi 6, 7, 2007, 2009, 2010 and XE books.

Way to go Marco!

–jeroen

Posted in Delphi, Development, Software Development | 2 Comments »

GExperts version 1.35 was released on June 5th, 2011

Posted by jpluimers on 2011/06/08

Earlier this week, the new GExperts 1.35 Release got published: a free set of expert extensions supporting Delphi 6 through XE.

It is an incremental release has a few new features of which I like the new subgroup support in the Grep Search extensions most.

Now hopefully, Thomas Müller will release an experimental GExperts build of version 1.35 soon: his experimental builds integrate a source code formatter that works across many Delphi versions and often formats better than the one included in Delphi 2010 and up.

–jeroen

Posted in Delphi, Development, Software Development | 4 Comments »

Delphi sorcery – great new blog by Stefan Glienke: DataBinding, MEF, Lamda Expressions and more

Posted by jpluimers on 2011/06/07

I just came across the great new Delphi sorcery blog by Stefan Glienke from Soest, NRW, Germany.

He uses some cool new features recently introduced into Delphi for new ways of DataBindingYield-Return, Delphi Lambda Expressions, Delphi Dependency Injection with MEF and more.

Sample code is at his Delphi Sorcery Google Project.

I hoped that somebody would do all that, as this shows the real power of what Delphi can do.

Now it’s there for

–jeroen

Posted in Delphi, Development, Software Development | 2 Comments »