Storing dates and calculating age in SQLite - c++

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

Related

Summing inside Redshift UDF

I have a dates tables, columns:
date: the date e.g. '2018-06-29'
business_day: 1 or 0 to indicate if date is business day or not
I want to create a User-Defined function in Redshift that outputs between two dates how many business days. In a select statement is will look like :
select business_days(start_date timestamp, end_date timestamp) as integer
for example:
2018-06-29 is a friday business day
2018-06-30 saturday non business day
2018-07-01 sunday non business day
2018-07-02 business day
select business_days( '2018-06-29', '2018-07-02') should output 2
Redshift does not allow aggregates inside a function and I am looking for a workaround.
Amazon Redshift User-Defined Functions cannot access the network, nor can they access data stored in tables.
As an alternative approach, you could create a Python function that can calculate the difference between two dates.
If you merely wish to skip weekends, you could Count number of days between dates, ignoring weekends.
If you have more complex logic, such as skipping holidays that vary by year, you will need to supply that information to the Python function, such as including the special dates within the function itself (eg in a list of holiday days).

What is the best way to store time in DynamoDB when accuracy is important?

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

What is KSP date formatting?

I am dealing with a dataset and I have to deal with the date formatting. I need to know different ways I can use this dates feature to predict the data.
Thanks
There are different ways to use this date data. For example, if your date is x implies it is the distance from the 1st of the month. Similarly, it is some distance y months from the first of the year (January).
The day also matters. If the day is Friday => it's 5 days from the week start. You can include this as a different feature.
KSP is another way of reducing the date into a useful feature. Year +(#ofdays from the start of the year -0.5) /#ofdays in the year(356 or 355) leap year or not a leap year.
Hope this is useful.

how to choose SQLite date format

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

Convert MYSQL Timestamp to time_t

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.