Convert MYSQL Timestamp to time_t - c++

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.

Related

Duration wrongly converted to Date-Time type

I am getting data from an excel file which include a column which shows duration as mm:ss:ms
But when I import the data to Power BI Desktop with Power Query, it converts this column to date/time format.
I don't know how can I solve this issue.
Any idea?
I already tried the code below:
format(((TableName[Duration] / 60)/60)/24, "HH:mm:ss")
and already tried to change the source column type from the excel to general.
PowerBI does not currently support milliseconds in date time datatype. You can convert the excel doc to general, load it into PowerBI, and multiply the value by 86400 to get the total number of seconds. Otherwise you'll need to bring it in as a string and do string manipulations to pull out the information you want.
Hope it helps.

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

A SAS value cannot be converted to a Teradata date

I've got a temporary work table with a date variable source_datetime in SAS DIS. This variable is in the DATETIME22.6 format.
I have a teradata table with a date field target_date (type DATE), and using a table loader I am attempting to map source_datetime to target_date. When I run the transformation I get the error
ERROR: A SAS value cannot be converted to a Teradata date
The temporary work table is populated with good data. When I attempt the conversion from DATETIME22.6 to DATE9. the output looks like "*********".
Much gratitude.
I know very little about either DIS or Teradata, but I don't think either are related to your problem.
Datetime values are the number of seconds since Jan 1, 1960 00:00:00. Date values are the number of days since Jan 1, 1960.
It sounds like you are trying to apply the date9 format to a datetime value. If you do this, it will usually look like ********* because the number of seconds is way too high to be represented as a date. If you want to keep the datetime value but have it formatted like a date, use the dtdate9 format. Otherwise, you could convert the datetime value to a date value with the datepart() function and then use the date9 format.

Storing dates and calculating age in SQLite

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

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