C - How to convert time_t to tm? - mysql

I have a variable which using time_t data type. I want to convert this type into "YYYY-MM-DD HH:MM:SS". I just know if it's only works in localtime() example:
char buff[20];
time_t now = time(NULL);
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
any suggestion how to convert it? because I have the time which always increased every minute, not the fixed one like the localtime(). I need this conversion for matching with datetime type in MySQL database.

The functions gmtime and localtime (for UTC and local time respectively) will turn an arbitrary time_t into a struct tm with individual fields for year, month and so on.
You can then just use strftime as you have, or sprintf to create strings from them, such as with:
char buff[11];
sprintf (buff, "%04d-%02d-%02d",
mytm.tm_year + 1900,
mytm.tm_mon + 1,
mytm.tm_mday);

Related

to_timestamp() passing the json data which is int and has unix timestamp not working in postgresql

info{
...
created_at:'1667801192'
}
select to_timestamp(info->'created_at') from booking;
info column has unix timestamp
expecting the whole column to convert as date
SQL is a typed language; every expression has a data type. The type of info->'created_at' is json or jsonb (depending on the type of info), and there is no function to_timestamp that accepts these data types as input. You will have to perform a type cast:
to_timestamp(CAST (info ->> 'created_at' AS double precision))

Neo4j Date conversion and comparison?

I was working with MS access database.MY datetime is now like "05-03-2016 14:55:20" .I need to convert it into datetime format in neo4j.1.How to do it ? 2.After conversion I need to use date filter i.e I want to find all nodes created between 2 dates.How to do it ?
If Neo4j is pulling data from Access query, construct field in query that calculates date to a string that Neo4j can then convert to date with its DateTime function.
Format([fieldname], "yyyy/mm/dd hh:nn:ss")
You can use the APOC function apoc.date.parse() to convert your MS Access datetime string into the number of seconds from the UNIX epoch, and then construct a neo4j datetime from that value. For example, this will return a neo4j datetime that represents your sample time string:
RETURN datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
})
A neo4j temporal type can only be compared directly to the same type. For instance, to compare a datetime X to a date Y, you can convert X to a date before doing the comparison. The following sample query will return true:
WITH
datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
}) AS X,
date('2016-05-04') AS Y
RETURN date({date: X}) <= Y

User defined date format in VBA Access

I can't figure out how to tell VBA the correct date format, as it seems to assume in the wrong way around. So CDate("24/02/2016 10:59") is giving the correct date - dd/mm/yyy. However when it iterates through a date like CDate("01/03/2016 00:59") - it assumes number 03 is not a month but a day so VBA is assuming it's mm/dd/yyyy, so it's giving me a date 03/01/2016 which is wrong and messing my data. So how can I tell VBA it's actually dd/mm/yyyy format. VBA seems to automatically pick up nicely even if it's "2016/01/14", but it's failing when it's not really obvious which part of numbers are months e.g. both dd and mmare less than 12.
I'm reading this date from a CSV file using WS.Cells(irow, icol).Value.
This is what I tried so far:
datDate = CDate(Format(WS.Cells(iRow, iCell).Value, "dd-mmm-yyyy hh:mm:ss"))
When the CDate() function encounters an ambiguous "aa/bb/yyyy" date string it will be interpreted according to the order that the Month and Day appear in the short date format defined by the Regional Settings in Windows.
For example:
When my machine is set to "English (United States)" with a short date format of M/d/yyyy then Month(CDate("02/04/2016")) returns 2.
However, after changing my Regional Settings to "English (Canada)" with a short date format of dd/MM/yyyy then Month(CDate("02/04/2016")) returns 4.
Note that this is different from the behaviour of the Jet/ACE database engine when interpreting ambiguous #aa/bb/yyyy# date literals in SQL statements. In that case it will always interpret them as mm/dd/yyyy regardless of the regional settings.
The best solution, as always, is to ensure that the string representation uses an UNambiguous date format, e.g., 2016/02/04 will always be interpreted as Feb 4.
Use mm/dd/yyyy hh:nn:ss like:
#03/01/2016 16:32:58#
or DateSerial and TimeSerial:
DateSerial(2016, 03, 01) + TimeSerial(16, 32, 58)
To parse the string, use Mid:
TrueDate = Dateserial(Mid(s, 7, 4), Mid(s, 4, 2), Mid(s, 1, 2)) + TimeSerial(Mid(s, 12, 2), Mid(s, 15, 2), 0)
s = "01/03/2016 00:59"
' Returns: 2016-03-01 00:59:00
s = "24/02/2016 10:59"
' Returns: 2016-02-24 10:59:00
Change the format of the date before you convert. Something like this;
Dim strDate As String, strFormatDate As String
Dim dte As Date
strDate = "01/03/2016 00:59"
strFormatDate = Format(strDate, "dd-mmm-yyyy hh:nn")
dte = CDate(strFormatDate)
This changes the date string to 01-Mar-2016 00:59 which should convert to a date data type without confusion.

Use the same buffer for separate DATE and TIME

I am using the MySQL C api along with prepared statements. When issuing a INSERT query with a TIME field the result is in HHH:MM:SS format, where the hours are the amount of hours elapsed this month. For instance if the date is 2015-02-21, and the time is 21:30:00 the time would be displayed as 525:30:00 but I want to use the HH:MM:SS format instead (e.g 21:30:00), which would be the actual time of the day.
sbind[3].buffer_type=MYSQL_TYPE_DATE;
sbind[3].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[3].is_null= 0;
sbind[3].length= 0;
sbind[4] = sbind[3];
sbind[4].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
This code will prepare the date field as yyyy-mm-dd and fill it with the date in tm_info. Likewise it will do the same thing for the time field but in the HHH:MM:SS format.
A unfashionable way which works is to use a separate MYSQL_TIME structure for the time, but I aim for a more elegant way to handle this.
(EDIT: Here I have included the relevant client side code
MYSQL_TIME ts;
MYSQL_STMT *stmt;
MYSQL_BIND sbind[2];
...
char query[QUERY_BUFFER_SIZE];
strcpy(query, "INSERT INTO `mytable` (date,time) VALUES(?,?)");
if(mysql_stmt_prepare(stmt, query, strlen(query))){
return mysql_stmt_errno(stmt);
}
...
time_t rawtime;
time(&rawtime); // get current time
struct tm *tm_info = localtime ( &rawtime );
...
memset(sbind,0,sizeof(sbind));
sbind[0].buffer_type=MYSQL_TYPE_DATE;
sbind[0].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[0].is_null= 0;
sbind[0].length= 0;
sbind[1] = sbind[0];
sbind[1].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
if(mysql_stmt_execute(stmt)){
return mysql_stmt_errno(stmt);
}
This assumes mysql is a valid connection. The table mytable in this case only contains a DATE type and a TIME type.
)
That's the natural representation for the TIME type - it, as the name suggests, only holds time, and this is the natural way to express time larger than a day. It does, as the documentation suggests, use HH:MM:SS for smaller values.
Since TIME does not care about shenanigans like leap seconds, to exclude full days, just take tm_hour%24. But, to allow for shenanigans like DST transitions, you have nothing to do but add the TIME to the starting point of the specific month and do DATETIME arithmetic with the stock functions.
#c45602234:
Trying to outsmart libmysql FAILED (mysql-connector-c-6.1.5/libmysql/libmysql.c:1964):
static void store_param_date(NET *net, MYSQL_BIND *param)
{
MYSQL_TIME tm= *((MYSQL_TIME *) param->buffer);
tm.hour= tm.minute= tm.second= tm.second_part= 0;
net_store_datetime(net, &tm);
}
As you can see, it always uses up the entire structure (apparently, so that relevant functions always work as expected).

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.