Skip to content
GitLab
    • GitLab: the DevOps platform
    • Explore GitLab
    • Install GitLab
    • How GitLab compares
    • Get started
    • GitLab docs
    • GitLab Learn
  • Pricing
  • Talk to an expert
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
    Projects Groups Topics Snippets
  • Register
  • Sign in
  • FPC Source FPC Source
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
    • Locked files
  • Issues 1.4k
    • Issues 1.4k
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 85
    • Merge requests 85
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
    • Test cases
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Terraform modules
    • Model experiments
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • FPC
  • FPCFPC
  • FPC SourceFPC Source
  • Issues
  • #39757
Closed
Open
Issue created Jun 02, 2022 by Werner Pamler@wpam

Allow month names in StrToDate and StrToDateTime

Summary

Motivated by freepascal.org/lazarus/lazarus#39779 (closed) I thought about the issue that StrToDate and StrToDateTime allow only numeric values in their date/time parts. The format expected is taken from the ShortDateFormat of the FormatSettings record where the month is assumed to be in the one- or two-digit representation.

But why can there be only numbers in the month part? In the FormatSettings.ShortDateFormat the month can be specified as 'mmm' or even 'mmmm' - and this inserts the short or long month names in the strings created by DateToStr. However, these strings cannot be converted back to dates by calling the inverse StrToDate function? I know that Delphi does the same, it accepts only numeric month strings. But I don't see a real reason for this asymmetry.

Patch

strtodate_mmm.diff

I am enclosing a patch which extends the StrToDate function such that also the month text representations are accepted (both short and long forms). It requires only a few changes, and don't think that it will cause a noticeable speed drop. Since StrToDateTime internally calls StrToDate, it will be extended automatically.

Delphi compatibility is not affected for "number-only" date strings. In the extended syntax with month names, however, it will throw an exception while it will be accepted by the the FPC version. However, we already have this mild kind of incompatibility because we allow the input string to contain only a date part which is not accepted for Delphi either.

Example Project

Here is a simple project which raises an exception with current FPC; after application of my patch the project will run correctly:

program Project1;
uses
  SysUtils;
var
  fs: TFormatSettings;
  d1, d2: TDateTime;
  s: String;
begin
  fs := FormatSettings;
  fs.ShortDateFormat := 'yyyy/mmm/dd';   // Note the 3 'm'!
  fs.DateSeparator := '-';

  d1 := EncodeDate(2020, 1, 31);
  s := DateToStr(d1, fs);
  d2 := StrToDateTime(s, fs);
  WriteLn(s, ' ', d1:0:0, ' ', d2:0:0);

  ReadLn;
end.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking