Compare two NSDate with .nanosecond granularity - uikit

I've tried to compare two NSDate using .nanosecond granularity:
let d1 = date1.absoluteDate // 501178812.31100011 (2016-11-18T16:20:12.311Z)
let d2 = date2.absoluteDate // 501178812.21199989 (2016-11-18T16:20:12.212Z)
let result = Calendar.current.compare(d1, to: d2, toGranularity: .nanosecond) // .orderedSame???
However while nanoseconds are different (d1=31100011 and d2=21199989) the comparison results is .orderedSame.
Is this a bug of NSDate or a precision-related issue?
I'm not sure it's a bug because on docs I've found:
"[NSDate]...makes possible a wide and fine-grained range of date and time
values, giving precision within milliseconds for dates 10,000 years
apart..."
But...why then compare method allows nanosecond as granularity?

Related

Calculate Total Amount for a specific period

I want to calculate total value between two dates in DAX. I have a formula SUM(C5:C16) in excel sheet, which C5 is the sales amount for a specific date (last year + 1month), and C16 is the sales amount for current row date.
I tried this formula in DAX, but it did not return sum value:
var Rolling = CALCULATE(sum('proces'[HOURS]),DATESINPERIOD('Date'[DateField],ENDOFMONTH('proces'[date_start]),-12,MONTH))
Also, I tried this one, but it is not working:
=SumX (
var prev=DATEADD(DATEADD('proces'[date_start] ,-1,YEAR),+1,MONTH)
return
Filter ( 'proces',
'proces'[date_end] <= Earlier ( 'proces'[date_end] ) &&
'proces'[date_start]>=prev,
'proces'[HOURS])
Also, I tried this one but it returns nothing
=CALCULATE(
SUMX('proces','proces'[HOURS]),
DATESBETWEEN(
'Date'[DateField],
STARTOFMONTH(DATEADD(LASTDATE('Date'[DateField]),-1,MONTH)),
ENDOFMONTH(DATEADD('Date'[DateField],-1,MONTH))
)
)
You seem to be confused about variables in DAX and your formulas are not even valid DAX expressions. Learn about variables in the official documentation:
Use variables to improve your DAX formulas
If you need further help with calculating the your total amount, add sample data to your question.

NEST (2.x) Date Histogram Aggregation with fractional interval values

I am using NEST (2.3.3) object initializer syntax for creating Date Histogram Aggregation. How can I specify the Fractional values for the Interval?
DateHistogramAggregation dateHistogram =
new DateHistogramAggregation("dateHistogram")
{
Field = "TimestampFieldName",
Interval = DateInterval.Hour
}
In the above data histogram aggregation I want to specify for example 1.5 hours. Is there a way I can do that?
Interval is a Union<DateInterval, Time> which means that it can take either a DateInterval enum value or a Time instance. Additionally, a string has an implicit conversion to an instance of Time. Putting these together, to set an interval of 1.5 hours would be
DateHistogramAggregation dateHistogram =
new DateHistogramAggregation("dateHistogram")
{
Field = "TimestampFieldName",
Interval = new Time("1.5h")
};
In this case, we can't take advantage of the implicit conversion from string to Time (and then Time to Union<DateInterval,Time>) because there is no implicit conversion from string to Union<DateInterval, Time>. In this case, we can just use the Time constructor and pass it a string value for 1.5 hours, and assign this instance of Time to the interval.

D3js getting just a part of a value and converting it to decimal

I'm trying to get a decimal value for a value that is expressed in a date. My problem is that I need only the time in the date, not the actual date itself. So here part of my .json data:
var data = [
{
"Id":"1390794254991",
"Tz":"Europe/Amsterdam",
"From":"27. 01. 2014 4:44",
"To":"27. 01. 2014 12:09",
"Sched":"08. 02. 2014 5:24",
"Hours":"7.430"
}]
So basically i would like to take the "From" value and get just the time returned in a decimal value. I don't think converting the time to a decimal number with Time Formatting, it's just the splitting of a value that seems pretty hard to me. Any of you guys have a clue how this could be achieved?
Thanks,
Cas
Edit: Okay, here we go. I made a JSFiddle to clear things up. Because there is a TIME (HH:MM) in my date, the rectangles in my barchart don't come on a specific day in the xAxis. INSTEAD they come out on a day AND time on the xAxis.
I just need it to be the specific date, not the date AND time.
I think I have to change something in the following part:
dataFiltered.forEach(function(d) {
d.From = parseDate(d.From);
});
Just don't really see what...
With d3, you'd construct a time parser, give it a string to parse, and get back a Date object.
var parser = d3.time.format('%d. %m. %Y %H:%M');
var date = parser.parse("05. 52. 2014 4:32");
Then you can do as you wish with this date object, including
var hours = date.getHours() // 4
var minutes = date.getMinutes() // 32
But, did you mean you don't want to use d3 (despite your question being tagged with d3)? If so, you can also use RegExp:
var dateString = "05. 52. 2014 4:32";
var matches = dateString.match(/(\d?\d):(\d\d)$/);
// yields ["4:32", "4", "32"]
Then you can get the tokens you want either via
var hours = matches[1];
var minutes = matches[2]
or
var hours = RegExp.$1;
var minutes = RegExp.$2
In either case though, RegExp gives you back strings, so you need to convert them to Numbers if you're doing quantitative stuff with them:
var hours = parseInt(matches[1]);
or more cryptically
var hours = +matches[1];

How to compare NSDate objects efficiently

I have to compare many managed objects and group them by date. I can't use NSDateComponents because it's too slow to compare two NSDate objects.
How can I compare them in a more efficient way, so that I can save processing time?
I am not sure what kind of comparison you are doing but you can do:
if ( [date1 timeIntervalSince1970] > [date2 timeIntervalSince1970])
{
NSLOG(#"Do something");
}
so if date1 is bigger (further away for 1970!) than date2 the if is true.
This will work quicker than NSDateComponents.

BIRT report cross tabs: How to calculate and display durations of time?

I have a BIRT report that displays some statistics of calls to a certain line on certain days. Now I have to add a new measeure called "call handling time". The data is collected from a MySQL DB:
TIME_FORMAT(SEC_TO_TIME(some calculations on the duration of calls in seconds),'%i:%s') AS "CHT"
I fail to display the duration in my crosstab in a "mm:ss"-format even when not converting to String. I can display the seconds by not converting them to a time/string but that's not very human readable.
Also I am supposed to add a "grand total" which calculates the average over all days. No problem when using seconds but I have no idea how to do that in a time format.
Which data types/functoins/expressions/settings do I have to use in the query, Data Cube definition and the cross tab cell to make it work?
Time format is not a duration measure, it cannot be summarized or used for an average. A solution is to keep "seconds" as measure in the datacube to compute aggregations, and create a derived measure for display.
In your datacube, select this "seconds" measure and click "add" to create a derived measure. I would use BIRT math functions to build this expression:
BirtMath.round(measure["seconds"]/60)+":"+BirtMath.mod(measure["seconds"],60)
Here are some things to watch out for: seconds are displayed as single digit values (if <10). The "seconds" values this is based on is not an integer, so I needed another round() for the seconds as well, which resulted in seconds sometimes being "60".
So I had to introduce some more JavaScript conditions to display the correct formatting, including not displaying at all if "0:00".
For the "totals" column I used the summary total of the seconds value and did the exact same thing as below.
This is the actual script I ended up using:
if (measure["seconds"] > 0)
{
var seconds = BirtMath.round(BirtMath.mod(measure["seconds"],60));
var minutes = BirtMath.round(measure["seconds"]/60);
if(seconds == 60)
{
seconds = 0;
}
if (seconds < 10)
{
minutes + ":0" + seconds;
}
else
{
minutes + ":" + seconds;
}
}

Resources