Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machine

后端 未结 11 829
小蘑菇
小蘑菇 2020-11-29 16:36

I have a sqlite (v3) table with this column definition:

\"timestamp\" DATETIME DEFAULT CURRENT_TIMESTAMP

The server that this database live

相关标签:
11条回答
  • 2020-11-29 16:41

    In the (admitted rare) case that a local datatime is wanted (I, for example, store local time in one of my database since all I care is what time in the day is was and I don't keep track of where I was in term of time zones...), you can define the column as

    "timestamp" TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M','now', 'localtime'))
    

    The %Y-%m-%dT%H:%M part is of course optional; it is just how I like my time to be stored. [Also, if my impression is correct, there is no "DATETIME" datatype in sqlite, so it does not really matter whether TEXT or DATETIME is used as data type in column declaration.]

    0 讨论(0)
  • 2020-11-29 16:41

    When having a column defined with "NOT NULL DEFAULT CURRENT_TIMESTAMP," inserted records will always get set with UTC/GMT time.

    Here's what I did to avoid having to include the time in my INSERT/UPDATE statements:

    --Create a table having a CURRENT_TIMESTAMP:
    CREATE TABLE FOOBAR (
        RECORD_NO INTEGER NOT NULL,
        TO_STORE INTEGER,
        UPC CHAR(30),
        QTY DECIMAL(15,4),
        EID CHAR(16),
        RECORD_TIME NOT NULL DEFAULT CURRENT_TIMESTAMP)
    
    --Create before update and after insert triggers:
    CREATE TRIGGER UPDATE_FOOBAR BEFORE UPDATE ON FOOBAR
        BEGIN
           UPDATE FOOBAR SET record_time = datetime('now', 'localtime')
           WHERE rowid = new.rowid;
        END
    
    CREATE TRIGGER INSERT_FOOBAR AFTER INSERT ON FOOBAR
        BEGIN
           UPDATE FOOBAR SET record_time = datetime('now', 'localtime')
           WHERE rowid = new.rowid;
        END
    

    Test to see if it works...

    --INSERT a couple records into the table:
    INSERT INTO foobar (RECORD_NO, TO_STORE, UPC, PRICE, EID)
        VALUES (0, 1, 'xyz1', 31, '777')
    
    INSERT INTO foobar (RECORD_NO, TO_STORE, UPC, PRICE, EID)
        VALUES (1, 1, 'xyz2', 32, '777')
    
    --UPDATE one of the records:
    UPDATE foobar SET price = 29 WHERE upc = 'xyz2'
    
    --Check the results:
    SELECT * FROM foobar
    

    Hope that helps.

    0 讨论(0)
  • 2020-11-29 16:41

    Time ( 'now', 'localtime' ) and Date ( 'now', 'localtime' ) works.

    0 讨论(0)
  • 2020-11-29 16:42

    SELECT datetime(CURRENT_TIMESTAMP, 'localtime')

    0 讨论(0)
  • 2020-11-29 16:47

    You can also just convert the time column to a timestamp by using strftime():

    SELECT strftime('%s', timestamp) as timestamp FROM ... ;
    

    Gives you:

    1454521888

    'timestamp' table column can be a text field even, using the current_timestamp as DEFAULT.

    Without strftime:

    SELECT timestamp FROM ... ;
    

    Gives you:

    2016-02-03 17:51:28

    0 讨论(0)
  • 2020-11-29 16:57

    The current time, in your machine's timezone:

    select time(time(), 'localtime');
    

    As per http://www.sqlite.org/lang_datefunc.html

    0 讨论(0)
提交回复
热议问题