Recent

Author Topic: SQLite3 Date Problem  (Read 4153 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16301
  • Censorship about opinions does not belong here.
Re: SQLite3 Date Problem
« Reply #45 on: December 06, 2024, 09:05:58 am »
The way I handle dates in any database is:
- I always use simply TDateTime,
- So I can store as a double. (TDateTime is a double)
- I always store as UTC
- And convert to local time for display only.
- Using FormatSettings
The reason is that this way the database table is always pure and works in any time zone.
SQLite3 can store doubles.
The above is actually what you would learn in any SQL database course regarding date and time.
That way I always have the correct local time in Amsterdam, London and Vilnius - where I happen to be quite often - or whatever time zone with the same code. That is what UTC is meant for.
The above is the same if you prefer to store unix timestamps. Store them as UTC.
« Last Edit: December 06, 2024, 09:22:28 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Zvoni

  • Hero Member
  • *****
  • Posts: 2782
Re: SQLite3 Date Problem
« Reply #46 on: December 06, 2024, 11:43:01 am »
My Opinion...
The way I handle dates in any database is:
- I always use simply TDateTime,  --> Agreed. If it's available, use it
- So I can store as a double. (TDateTime is a double) --> If no DateTime-type is available, i store it as Text in extended ISO-Format
- I always store as UTC  --> Disagree. To store as UTC only makes sense, if there are multiple Clients connecting from different timezones. If it's a local Database (SQLite!), it's local. Period! i use localtime, so the Data inside the DB is in sync with the Computer it resides on.
- And convert to local time for display only.  --> only if stored as UTC
- Using FormatSettings --> that goes without saying
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 16301
  • Censorship about opinions does not belong here.
Re: SQLite3 Date Problem
« Reply #47 on: December 06, 2024, 11:53:07 am »
Even for local databases - which SQLite isn't - it makes sense to always handle dates as UTC unless it is for your mother's agenda application.(i.e. hobby programmers)
If I smell bad code it usually is bad code and that includes my own code.

Zvoni

  • Hero Member
  • *****
  • Posts: 2782
Re: SQLite3 Date Problem
« Reply #48 on: December 06, 2024, 11:54:12 am »
Even for local databases - which SQLite isn't - it makes sense to always handle dates as UTC unless it is for your mother's agenda application.(i.e. hobby programmers)
Since when is SQLite NOT a local Database?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

egsuh

  • Hero Member
  • *****
  • Posts: 1519
Re: SQLite3 Date Problem
« Reply #49 on: December 10, 2024, 06:29:10 am »
I think poweld's solution is right. but,

Code: Pascal  [Select][+][-]
  1.       SQLQuery.SQL.Text := 'SELECT xDate, xNumber from transactions';
  2.       DataSource1.DataSet := SQLQuery1;
  3.       DBGrid1.DataSource := DataSource1;
  4.       SQLQuery1.Open;
  5.       for i := 0 to DBGrid1.Columns.Count - 1 do
  6.       begin
  7.         if DBGrid1.Columns[i].Field.DataType = ftDate then
  8.           DBGrid1.Columns[i].DisplayFormat := 'dd/mm/yyyy';
  9.       end;
  10.       //...

Only that ftDateTime instead of ftDate

        if DBGrid1.Columns[ i ].Field.DataType = ftDateTime then


might work, depending on SQLite's definition of the field type.
     

silvercoder70

  • Full Member
  • ***
  • Posts: 118
    • Tim Coates
Re: SQLite3 Date Problem
« Reply #50 on: December 10, 2024, 10:08:07 am »
Even for local databases - which SQLite isn't - it makes sense to always handle dates as UTC unless it is for your mother's agenda application.(i.e. hobby programmers)
Since when is SQLite NOT a local Database?

I'm with you Zvoni :) I regard it as local as well.

There are many ways this cat can be skinned (relatively speaking) ... (1) strftime() works, (2) setting up on DisplayText handler at the field level, or (3) setting displayFormat of column?

Also I would only though store a date/datetime as a numeric of sometime that easily allows for the date functions to be used on the field in a query, and that is whether I'm working in seconds, hours, days or months. Don't have to make it harder than it needs to be. (I also have a couple of vids on (1) and (2) as well.)
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

 

TinyPortal © 2005-2018