Sqlite has a different approach in storing time than other databases:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
**TEXT** as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
**REAL** as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
**INTEGER** as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
what is the best way to store date data in a sqlite database ?. TEXT, REAL or INTEGER ?
I'm interested in something like "using TEXT is space space consuming, using INTEGER is fine but you will have a big problem in the year 2038"
Integers are stored on 64 bits since SQLite 3.0 so year 2038 is not really a problem.
With REAL or INTEGER, you must perform calculations in fractions of days or fractions of seconds when you insert. With Integer, the resolution will be one second. With TEXT, it is one millisecond.
If you are concerned about total space and do not need any milliseconds or dates prior to 1970, then go for INTEGERs.
Is this correct?
Format Resolution Min Year Max Year Bytes/Date
Text Milliseconds 0 AD 9999 AD 23 or 46
Real Milliseconds? 4713 BC ???? 8
Integer Seconds 1970 AD ???? 8
The easiest way if you don't need fractions of seconds is epoch (integer number of seconds before or after 1970-01-01 00:00) accurate from -4714-11-24 until 5352-11-01 10:52:47
For calculation to human readable dates there is the function strftime
Related
I have a question regarding to the intck function in SAS.
Can INTCK handle milliseconds? I read the SAS Community Posts, but it seems that it can only handle up to seconds. What should I do if I want to calculate the time difference in milliseconds?
I am also thinking directly subtracting one time from another, such as TIME_M - LAG_TIME. Will this work?
UPDATE:
It seems that directly subtract will work. My time is in the format of 9:00:01.321, where the 321 is the millisecond. It seems that 9:00:01.321 - 9:00:01.320 = 0.001.
I am just not sure if this method is official.
INTCK is most often used to calculate complex date and time intervals - i.e., date and time intervals that don't have a direct proportional relationship to the base date or time units (days and seconds, respectively). It can also be used to code more clearly (i.e., hours is directly proportional to seconds (*3600) but intck('HOUR',...) may be more clear).
As such, you're certainly welcome to add or subtract directly when using seconds or a proportion thereof (milliseconds would be one such proportion). That's how I would do it in my code.
You could also define a custom time interval if you wanted to take advantage of intck for a readability purpose.
I am using AWS DynamoDB for storing time series data.
Accuracy is very important for this application. But, DynamoDB does not support native Date type to store.
I have two options:
Use UNIX Epoch Time
Use Date as string and store
Both options seem to have their problems:
The Epoch Time has the problem of leap seconds and losing accuracy.
The String Time type may have problems for sorting or querying between range of dates or times.
Which one should I use when accuracy is a key factor?
Use ISO format.
Stores milliseconds
Stores an optional timezone
Supports range queries for dates and date/time
Sample date formatted according to ISO 8601:
2016-12-02T00:52:34.256Z
Here is the piece of code I mean:
COleDateTime dateTime(year, month, day, hour, min, sec);
time_t timeT = ((double)dateTime * 86400) - 2209161600;
86400 is the number of seconds in the day and 2209161600 is a time stamp for 3rd Jan 2040 00:00:00.
Why are these numbers part of the calculation, as while there is explanations on using this piece of code there is no information on WHY its being used. This is an example of a website that tells you to use this when doing this conversion, but has no explanation as to why: http://mfctips.com/2013/01/28/convert-utc-formatted-string-to-unix-time_t-or-ctime/
Any help/explanation would be appreciated.
2209161600 is ~70 years - a time duration, not a time point.
COleDateTime has a different epoch which starts in year ~1900, whereas Unix epoch starts in year 1970.
Hence, to go from duration since 1900 to duration since 1970 you need to subtract from the duration 70 years.
You could just use standard mktime function to convert from a broken down local time representation to time_t. Or timegm to convert from a broken down UTC time representation.
In my program I need to save peoples birthday in a sqlite database. Then within the program I need to display the birthday as well as their age too.
Is their a special way to save dates in one column or do I need to save them seperatly day, month and the year.
My other question is how to get their age accurately as years, month and days. is it possible to do this with some kind of function.
The date can be stored in a single column as text, a real or an int and can be used with the builtin date functions.
Documentation:
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any
of these formats and freely convert between formats using the built-in
date and time functions.
An example of selecting the users age from the database would be:
-- '2001-01-01' would be the text column where you store the birthdate
SELECT (strftime('%Y', 'now') - strftime('%Y', '2001-01-01')) - (strftime('%m-%d', 'now') < strftime('%m-%d', '2001-01-01')) AS 'Years'
[reference]
The sqlite documentation at http://www.sqlite.org/datatype3.html recommends to use ISO8601 strings. SQLite also provides some functions you can use in queries: http://www.sqlite.org/lang_datefunc.html
I'm writing a multi-threaded program that needs to be able to check if a row requires updating and act accordingly.
I had problems using the built in date/time functions of MySql and so decided to just store the "lastupdate" timestamp as an integer in the table. However, I'm having problems converting this timestamp to time_t so that I can use the time functions with it.
Any help is greatly appreciated.
The MySql timestamp data type can be stored as a number in either YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD format.
In Unix and POSIX-compliant systems, time_t is typically an integer which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970.
In MySQL you can use the UNIX_TIMESTAMP() and FROM_UNIXTIME() functions convert between TIMESTAMP values and Unix timestamp values.
Query Example: SELECT Unix_Timestamp(Date_Entered) FROM Foo;
Try FROM_UNIXTIME and TO_UNIXTIME and leave your dates as dates in the database.