Turning MySQL Timestamp into R date format - mysql

I have a MySQL table where new entries are given a timestamp like this:
`timestamp` timestamp NULL default CURRENT_TIMESTAMP,
and looking like this:
2014-01-01 01:01:01
And I would like to be able to plot these timestamps in a ggplot2 scatterchart using:
[...]
if(myxaxis == "timestamp") {
p = p + scale_x_datetime(as.POSIXct("xvalue"))
}
print(p)
[...]
I get an error:
Error: character string is not in a standard unambiguous format
How can I transform my timestamp to the correct date format for ggplot2's scale_x_date?

At the moment, it looks like the as.POSIX function is trying to work out what date and time the actual string "xvalue" is. If your date/time string is stored in a variable called xvalue, then try removing the double quotes:
p = p + scale_x_datetime(as.POSIXct(xvalue))

Related

Unable to insert data into MYSQL due to UTC

I am trying to use FullCalendar v4 and cannot post data to MySQL. I have narrowed the problem down to $start and $end having UTC on the end and MySQL won't take it even though my datatype is TIMESTAMP. If I manually assign standard datetime data (without UTC) to $start and $end it will post to table. I have the statements commented out in the event.php that work, by overriding the data in the $POST.
My thought is I have something askew in MySQL that is causing the TIMESTAMP datatype to actually be a DATETIME datatype. I have deleted and created the table with SQL Statement shown below.
Running -> MySQL 8.01.5, Windows Server 2016, PHP 7.2.7, using jQuery
...
CREATE TABLE calendarsystem.events (
id int(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(45) NOT NULL ,
start TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
end TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
resourceId VARCHAR(45) NOT NULL ,
PRIMARY KEY (id)
);
...
The code to add_event.php:
<?php
$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];
$resourceId = $_POST['resourceId'];
//$title = 'wtf';
//$start = '2019-03-25 16:00:00';
//$end = '2019-03-25T17:00:00';
//$resourceId = 'b';
try {
require "db_config.php";
} catch(Exception $e) {
exit('Unable to connect to database.');
}
$sql = "INSERT INTO events (title, start, end, resourceId) VALUES (:title, :start, :end, :resourceId )";
$q = $bdd->prepare($sql);
q->execute(array(':title'=>$title,':start'=>$start,':end'=>$end,':resourceId'=>$resourceId));
?>
...
If I open MySQL Workbench and try to add the data with the UTC copied from the output window of chrome I get the following error when applying:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
INSERT INTO calendarsystem.events (start, end, title, resourceId) VALUES ('2019-03-25T14:00:00-05:00', '2019-03-25T15:00:00-05:00', 'xxx', 'b');
ERROR 1292: 1292: Incorrect datetime value: '2019-03-25T14:00:00-05:00' for column 'start' at row 1
SQL Statement:
INSERT INTO calendarsystem.events (start, end, title, resourceId) VALUES ('2019-03-25T14:00:00-05:00', '2019-03-25T15:00:00-05:00', 'xxx', 'b')
Sorry the post formatting is crappy
I think MySQL isn't recognizing the 'T' character or the trailing time offset in the string value.
'2019-03-25T15:00:00-05:00'
^ ^^^^^^
One way to fix the problem would be to remove that T character and the offset
'2019-03-25 15:00:00'
^
We expect MySQL to recognize a string in that format.
Alternatively, we could use STR_TO_DATE function with appropriate format model so MySQL can interpret datetime/timestamp values from strings in different formats.
The relevant topic in the MySQL Referenced Manual is here:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html

SparkSQL between timestamp in where(filter) clause (V.S. In MySQL)

Describe:
I have a table with timestamp column and i want to get the number of values where the timestamp in specific time window.
My code is as shown in here:
String startTime = "2018-08-08 00:00:00";
String endTime = "2018-08-08 23:59:59";
productDF.where("CREATETIME >= '" + startTime + "' AND CREATETIME <= '" + endTime + "'").count();
I also tried between...and...sentence; and also:
productDF.where(unix_timestamp(col("CREATETIME"), "yyyy-mm-dd hh:mm:ss")
.cast("timestamp")
.between(
Timestamp.valueOf(startTime),
Timestamp.valueOf(endTime)
)).count();
The result i get is 6843.
But when i operate the sql sentence using Navicat:
SELECT COUNT(*) FROM my_table
WHERE CREATETIME BETWEEN '2018-08-08 00:00:00' and '2018-08-08 23:59:59';
it shows 7689.
Problem:
I want to know why i get the different results in Spark and Mysql.....what am i missing here??
Problem solved!
The problem happened because of the TIMEZONE.
In spark env., it get the timezone from_unixtime..So need to set the config.
.config("spark.sql.session.timeZone", "UTC")
But I still don't understand why the spark sql session flow the system timezone instead of just select from the column.....

Symfony - Select object where date = today

I have a entity repository containing code
$qb = $this->createQueryBuilder('tdd');
$qb->select('tdd')
->where($qb->expr()->eq('tdd.disable_date', ':dat'))
->setParameter('dat', new \DateTime());
return $qb->getQuery()->getOneOrNullResult();
And Entity containing only one member (disable_date)
/**
* #ORM\Column(type="date", unique=true, nullable=false)
* #ORM\Id()
*/
private $disable_date;
When I try to check if today's date is in database, code above returns NULL.
Date in DB exists.
What the heck?
Because new DateTime() creates object similar to this:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-04-12 07:24:34.697668"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
And you trying to match exact date with time (including seconds and ms). And thats why results is null
Check date without h:i:s
I solved this using midnight modificator.
When you declare in doctrine a Date field, in database it will be stored as date time, but Entity uses a DateTime interface.
So if you try to compare a new \DateTime (current date and time) with the date stored en database, parsed by doctrine as date stored + current time, comparasion never will be equal.
Using the midnight modificator you are really comparing equals.
$dateInDatabase = '2020-04-22';
$parsedByDoctrine = new \DateTime($dateInDatabase); // 2020-04-22 00:00:00
$now = new \DateTime(); // 2020-04-22 17:20:00
$todayMidnight = new \DateTime('midnight'); // 2020-04-22 00:00:00
$now === $parsedInDatabase // FALSE
$todayMidnight === $parsedInDatabase // TRUE
This can be applied in the createQueryBuilder or in the findBy methods
php.net/manual/en/datetime.formats.relative.php

DHMS to concatenate date and time to datetime causes the first date to be 1/1/1960

I am creating a time series of intraday (1 minute interval) time series. I have the variables date YYYYMMDD and time HH:MM:SS and would like to create the datetime YYYYMMDDTHH:MM:SS (or whatever format is not too important).
When looking at the output (just sample dates):
Date Time Datetime
20000101 9:30:00 1960-01-01T09:30:00
20000101 9:31:00 2000-01-01T09:31:00
.
.
.
20000102 9:30:00 1960-01-01T09:30:00
20000102 9:31:00 2000-01-02T09:31:00
SO whenever time is 9:30:00 the concatenation via dhms(date, 0, 0, time) gives me the wrong value.
My code actually picks stock prices in certain interval from higher frequency data:
data xtemp2;
set _v_&tables;
by symbol date time;
format datetime e8601dt. itime rtime time12.;
if first.symbol = 1 or first.date = 1 then do;
/* Initialize time and price when new symbol or date starts; */
rtime = time;
iprice = bid;
oprice = ofr;
itime = &start_time;
datetime = time;
end;
if time >= itime then do;
output;
itime = itime + &interval_seconds;
do while(time >= itime);
output;
itime = itime + &interval_seconds;
end;
end;
rtime = time;
iprice = bid;
oprice = ofr;
datetime = dhms(date, 0,0,itime);
retain itime datetime iprice oprice;
run;
Is it something in my code? because looking at the distinct date and time variable shows the correct date.
I wanted to combine these because I have a time series for each stock and would like to match merge them which - if I understand correctly - requires one unique id that could be my datetime variable.
The issue seems to be with this piece of conditional logic:
if first.symbol = 1 or first.date = 1 then do;
This means that the first instance of those by groups will execute datetime = time; instead of datetime = dhms(date, 0,0,itime); (presuming time >= itime is true).
I suggest replacing datetime = time; with datetime = dhms(date, 0,0,itime); in the first instance.

str to date mysql function default day

is it possible to default the day field when using the string to date function in MySQL ?
I am using this:
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m') AS date)
the input values I am receiving are in the format of
201305
201203
etc
so the str_to_date function works fine in converting that but it shows the day as 00 which I would like to default all of them to 01.
I tried various forms of the str_to_date function but none worked.
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m%01') AS date)
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m-01') AS date)
You could use this:
cast(STR_TO_DATE(CONCAT(#sampl_date, '01'),'%Y%m%d') AS date)