I have a database with rows, with time values. I want a SUM of that time values (generaly, the format are %H:%M).
I used SUM value like that:
SELECT SUM(n_hores) FROM table;
But only I get 0.0.
How I can do that ?
Thanks
Time values in hh:mm format are strings, and strings cannot be summed meaningfully.
You should convert these time values into a number (strftime(%s) returns the number of seconds since 1970-01-01, so you have to subtract the offset from the start of the day to get the number of seconds corresponding to your time value):
SELECT sum(strftime('%s', MyTime) - strftime('%s', '00:00')) FROM MyTable
You can also convert this number of seconds back into a time string (time() returns it in hh:mm:ss format):
SELECT time(sum(strftime('%s', MyTime) - strftime('%s', '00:00'))) FROM MyTable
You're probably better off subtracting the two dates by getting the "number of milliseconds since the last epoch aka (modern epoch)." There should be a function, in SQLite, that returns the number of milliseconds since the last epoch. Once you've found this, the solution should be a simple subtraction and conversion to seconds, minutes and/or hours.
Related
I have a DateTime represented as long (8 bytes), that came from DateTime.ToBinary(), let's call it dateTimeBin. Is there an optimal way of dropping the Time information (I only care for the date) so I can compare it to a start of day? Lets say we have this sample value as a start of day.
DateTime startOfDay = new DateTime(2020,3,4,0,0,0);
long startOfDayBin = startOfDay.ToBinary();
I obviously know I can always convert to a DateTime object then get the date component. However, this operation is going to happen billions of times and every little performance tweak helps.
Is there an efficient way of extracting the Date info of dateTimeBin without converting it to DateTime? Or any arithmetic operation on the long that will return the date only?
Is there a way to match startOfDay (or startOfDayBin) and dateTimeBin if they have the same date components?
Is there a way to see if (dateTimeBin >= startOfDayBin), I don't think the long comparison is valid.
N.B. all the dates are UTC
Since you are working only with UTC dates - makes sense to use DateTime.Ticks instead of DateTime.ToBinary, because former has relatively clear meaning - number of ticks since epoch, just like the unix time, the only difference is unix time interval is second and not tick (where tick is 1/10.000.000 of a second), and epoch is midnight January 1st of 0001 year and not year 1970. While ToBinary only promises that you can restore original DateTime value back and that's it.
With ticks it's easy to extract time and date. To extract time, you need to remainder of division of ticks by number of ticks in a full day, so
long binTicks = myDateTime.Ticks;
long ticksInDay = 24L * 60 * 60 * 10_000_000;
long time = binTicks % ticksInDay;
You can then use convert that to TimeSpan:
var ts = TimeSpan.FromTicks(time);
for convenience, or use as is. The same with extracting only date: just substract time
long date = binTicks - (binTicks % ticksInDay);
Regular comparision (dateTimeBin >= startOfDayBin) in also valid for tick values.
I have an old MySQL database where I stored dates as C# DateTime.Ticks.
Now I want to convert that old database to a new structure for a PHP app that has needs a datefield. How do I convert DateTime.Ticks to MySQL dates?
I am looking for something in the following format YYYY-MM-DD HH:mm:ss:
SELECT someconversion(olddate) as newDate FROM table;
Thank you so much in advance
Dotnet Ticks are stored in 64-bit integers in units of 100ns (ten million per second) since an epoch of 0001-01-01 00:00:00 UTC.
MySQL Days (as used in FROM_DAYS()) count elapsed days from 0000-01-01.
For example 2016-01-09 00:00:00 in Ticks is 635879124000000000 or, for readability
635 879 124 000 000 000
So, presumably your oldate column datatype is BIGINT or DOUBLE in MySQL. Either that, or it's a text string.
The thing to know is that the UNIX Epoch, 1970-01-01 00:00:00 UTC, has this Ticks value: 621355968000000000. This is the ultimate magic number.
At any rate, here's how to convert Ticks within MySQL. We'll convert to UNIX timestamps, then to DATETIME columns.
SELECT FROM_UNIXTIME((yourcolumn/10000000.0) - (62135596800.0))
But, here's the thing. The output of this FROM_UNIXTIME() gets converted implicitly to your local time. You may want to preserve the UTC times when you store this data.
There are two ways to do that. One is to use the TIMESTAMP datatype to store these converted times. The other is to set your MySQL session timestamp to 'UTC' before you do this conversion operation, like this:
SET time_zone = 'UTC';
Here is another way to do the same thing, but one that doesn't depend on using Unix timestamps. (Those timestamps are only valid from 1970 to 2038.) It depends on converting ticks to days, then on converting the remainder to seconds. It converts ticks directly to a DATETIME data type.
select FROM_DAYS(365+(yourcolumn / 864000000000))
+ INTERVAL (yourcolumn % 864000000000) / 10000000 SECOND
The constant 864000000000 (864 000 000 000) is the number of ticks in a day.
Breaking this down:
yourcolumn / 864000000000 is the number of days since 0001-01-01
FROM_DAYS(365+(yourcolumn / 864000000000)) is the date.
(yourcolumn % 864000000000) / 10000000 is the remainder of the division in step 1, in seconds.
FROM_DAYS(date) + INTERVAL seconds SECOND gets the full timestamp.
is this what you want?
select DATE_FORMAT([yourcolumn],'%Y-%M-%D %H:%m:%s') from [yourtable];
Here's the link for variant date format Date format options
This was a pain for me, but what I did was to store datetime.ticks in DB as Bigint then...
Datetime MyDateTimeVar = new datetime(Convert.toint64(DB.ticks)); //This gets the datetime from ticks!
If I write a simple method to return the milliseconds between epoch time and DateTime.UtcNow, I get a proper answer. However, if I write a method to return the milliseconds between some arbitrary date and epoch time, the last three digits are always zero. 'Some arbitrary date' means that I pass in to the method the output of DateTime.Parse("arbitrary date string"). As near as I can make out, the DateTime object returned by .Parse is not returning all the significant digits.
Test method:
static void GetMillis()
{
DateTime dUtc = DateTime.UtcNow;
DateTime epoch = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
double utcmillis = (dUtc - epoch).TotalMilliseconds;
String timestamp = dUtc.ToString();
DateTime arbitrary = (DateTime.Parse(timestamp));
Console.WriteLine("Milliseconds between DateTime.UtcNow {0} \nand epoch time {1} are {2}", dUtc, epoch, utcmillis);
Console.WriteLine("Milliseconds between arbitrary date {0} \nand epoch time {1} are {2}", arbitrary, epoch, (arbitrary - epoch).TotalMilliseconds);
}
Output:
C:\src\vs\epochConverter\epochConverter\bin\Debug
{powem} [54] --> .\epochConverter.exe -debug
Milliseconds between DateTime.UtcNow 8/26/2012 11:12:31 PM
and epoch time 1/1/1970 12:00:00 AM are 1346022751385.8
Milliseconds between arbitrary date 8/26/2012 11:12:31 PM
and epoch time 1/1/1970 12:00:00 AM are 1346022751000
I don't know if I'm doing something grotesquely wrong or not understanding the math here. I've researched in MSDN and can't find anything relating to this difference. I really would like to be able to compute the millis as described -- is it possible?
Thanks.
mp
You want to examine the intermediate values of:
String timestamp = dUtc.ToString();
Just what it returns will depend on your local settings, but it'll be something like 8/26/2012 11:12:31, which is only accurate to the nearest second.
Parsing that of course gives a date-time with 0 milliseconds.
It is therefore correct that your milliseconds-since-epoch method has zeros at that point.
If however you did something like:
arbitrary = new DateTime(2012, 8, 26, 11, 12, 31, 123);
You'd get those 123 milliseconds influencing the result. You can also use a ToString and a ParseExact that includes fractions of a second, or a whole slew of other ways of obtaining a DateTime.
In all, your milliseconds-since-epoch worked perfectly, but your way of getting a date to test it was flawed.
The default DateTime.ToString() format does not include the milliseconds and this is where the data is being lost; it happens before the Parse. To obtain the milliseconds in the string representation, use a custom format:
DateTime.UtcNow.ToString()
// -> 8/26/2012 11:37:24 PM
DateTime.Parse("8/26/2012 11:37:24 PM").Millisecond
// -> 0
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK")
// -> 2012-08-26T23:41:17.3085938Z
DateTime.Parse("2012-08-26T23:41:17.3085938Z").Millisecond
// -> 308
See The Round-trip ("O", "o") Format Specifier
to type less. Or, in this case, consider avoiding the conversion entirely :-)
The math is sound.
The math looks reasonable here. Don't forget there are 1000 milliseconds in a 1 second, so any date computation from an arbitrary time that does not include milliseconds vs an almost identical time that includes milliseconds will have an error of +/- 1000 milliseconds.
In my MySQL data base table there are data in each 10 minutes intervals ,ex
//time format "yyyy-MM-dd hh:mm:ss"
Time | value
2012-03-02 1:10:00|2
2012-03-02 1:20:00|5
.
2012-03-02 2:00:00|3
The user can select a starting time, end time and the time interval(10 minutes,30 , 1hr,1 day) ,
ex if user select start time
:2012-03-02 1:15:00 , end time 2012-03-02 2:10:00 and interval as 10 minutes then result
Time | value.
2012-03-02 1:15:00|2
2012-03-02 1:25:00|5
.
2012-03-02 2:05:00|3
Value is the last data in the time interval,(ex data value of 15th would be the value of 10th minute)
What i do now is i get the whole set of data with in the time period without considering about the time interval and after that manipulate the data set manually(in my program) to get the result according to the user selected time interval,
time interval means :10 mints,30 mints, 1hr, 1 day
My c# program becomes slow because there are lots of iterations, is their any way i can query the data base directly and to get the data at once, your comments are highly appreciated
Instead of storing your time intervals as a value, You can avoid all these problem by storing time intervals as two DateTime fields representing the starting and ending interval, like this:
UserId.
StartingInterval: DateTime.
EndingInterval: DateTime.
Then you can get the time interval by subtracting the two datetime fields and for the set of data that fall into a set of interval by using the BETWEEN the two fields.
I don't agree that MGA's answer makes sense, since you aren't storing intervals, you are storing events - and each event is associated with one time, not two.
I can't quite understand the inputs to your query, since if the user gives you a start time and end time, the interval is already set, isn't it? I think you only need the interval if one of the times is set (in other words, you need "2012 03 02 10:00:00" and "1 hour" OR "2012 03 02 10:00:00" and "2012 03 02 10:10:00").
Let's assume that if the user gives you one date and interval your program can translate that into a start time and an end time, so your query is going to get two input parameters: start_time and end_time.
Here's a query that works (testing with command line temp variables):
SELECT value FROM mytable
WHERE `Time` >= #start_time
AND `Time` <= #end_time
ORDER by `Time` DESC
LIMIT 1;
You'll have to handle the case where nothing is returned in your program, of course.
I am working with Visual Studio 2010, MVC 3 and C#. I am creating some highcharts and need to have the x-axis be a date. I am pulling the dates from a database and adding them to and array that will then be passed to highcharts. I think highcharts requires the dates to be in millisecond format. Ho do I go about converting a DateTime of '12/20/2011 5:10:13 PM" for example to milliseconds?
Once you figure out what you want to calculate milliseconds from, you can just take one DateTime object from another to get a TimeSpan object. From TimeSpan you can get TotalMilliseconds.
In other words, if start and end are DateTime objects, you can do this:
double milliseconds = (end - start).TotalMilliseconds;
You can use the DateTime.Ticks property and convert the value to milliseconds.
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.
The .Ticks in C# DateTime gives you the value of any time in ticks. You can thereafter convert to milliseconds as shown below:
long dateticks = DateTime.Now.Ticks;
long datemilliseconds = dateticks / TimeSpan.TicksPerMillisecond;
DateTime[] dates = ;
var minDate = dates.Min();
var msDates = dates.Select(date => (date - minDate).TotalMilliseconds).ToArray();