Recommendation for date and time format

From ISXKB

Jump to: navigation, search

Contents

Date

Misleading date formats

Software developers sometimes still use very localised formats for date and time representations.

Localised date/time representations can lead to many confusions, and it screws up date sorting.

08-02-07 -> Huh?
07-07-08 -> What?

Correct date format

There has been a standard for about 20 years already (ISO 8601).

Correct:
07-06-28 -> 2007-06-28

Time

Misleading time formats

Misleading 12 hour time format
Misleading 12 hour time format

Similar issues arrise when using the deprecated 12 hours time format. The image on the right has been taken from a screenshot of a cheap internet router.

It is a perfect bad example. As the one who is supposed to configure this device you won't have a clue what times to set up.

The router's configuration mask even shows another error: The first day of the week is Monday, not Sunday.

Correct time format

The hour is represented by two digits, and a day has 24 hours. In computer technology, leading spaces should be filled up with zeros to make the string the same size throughout the day.

Correct:

00:00
01:00
02:00
03:00
...
23:00

Inno Setup

Inno Setup's log file

Inno Setup's log files fully comply with the standard.

Example:

2007-10-27 14:09:55.905   Log opened. (Time zone: UTC+01:00)
2007-10-27 14:09:55.905   Setup version: Inno Setup version 5.1.13

Inno Setup functions

In Inno Setup the function GetDateTimeString () returns a string value for date and time. The declaration of GetDateTimeString () is:

GetDateTimeString(const DateTimeFormat: String; const DateSeparator, TimeSeparator: Char): String;

In order to use the standard's format for date and time the function requires to be called like this:

MyDateTimeString := GetDateTimeString ('yyyy/mm/dd hh:nn:ss', '-', ':');

This results in the following value for MyDateTimeString: YYYY-MM-DD hh:mm:ss (which is exactly what the standard requires)

If you use the function like this you won't have to bother with localised date/time formats anymore.

Inno Setup Preprocessor functions

The preprocessor ISPP supports a function with the same name. The difference to Inno Setup's GetDateTimeString () is that it is evaluated at compile time, not during the installation.

str GetDateTimeString(str, str, str)

The function can be used to create ISPP definitions that contain compilation date and time.

#define MyInstCreationDateTime GetDateTimeString ('yyyy/mm/dd hh:nn:ss', '-', ':');
#define MyInstCreationDate GetDateTimeString ('yyyy/mm/dd', '-', ':');

Examples

Inno Setup

Before applying an update you could backup the application's directory into a subfolder named 'Archive-YYYY-MM-DD hh.mm.ss'). The colon (':') can't be used as a delimiter between the hour and the minutes because Windows does not allow a colon to be part of a file name.

[Files]
Source: {app}\*; DestDir: {app}\Archive-{code:GetTodaysName}; Flags: external setntfscompression skipifsourcedoesntexist

[Code]
var
  TodaysName : String;

function GetToday : String;
begin
  Result := GetDateTimeString ('yyyy/mm/dd hh.nn.ss', '-', #0);
end;

function GetTodaysName (Param: String): String;
begin
  if ('' = TodaysName) then
  begin
    TodaysName := GetToday ();
  end;
  Result := TodaysName;
end;

See also Backup of application directory for a better explanation of the above code.

ISPP

The preprocessor's macro function could be used to create an output file (Setup.exe) with the compilation date in it:

[Setup]
OutputBaseFilename=Setup_{#MyInstCreationDate}

This would create output files 'Setup_YYYY-MM-DD.exe' like 'Setup_2007-10-11.exe'.

See also

External links

Personal tools
Ads: