Skip to content

Instantly share code, notes, and snippets.

@csuzw
Last active November 13, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csuzw/63e7e85d1e60471b8c1101fef0ae1f6e to your computer and use it in GitHub Desktop.
Save csuzw/63e7e85d1e60471b8c1101fef0ae1f6e to your computer and use it in GitHub Desktop.
Gets the number of days between 2 dates (at least if they fall between 01/01/0001 and 29/02/4096 (I think!)). Note that this could be even faster if I used such exotic things as conditionals to weed out the easy cases but where would the fun be in that!?!
public int DateDiff(int y1, int m1, int d1, int y2, int m2, int d2)
{
return GetDays(y2, m2, d2) - GetDays(y1, m1, d1);
}
private int GetDays(int y, int m, int d)
{
var y1 = y - 1;
var m1 = m - 1;
var yleap = y - ((18 - m) >> 4);
var d4 = yleap >> 2;
var d16 = yleap >> 4;
return (y1 << 8) + (y1 << 6) + (y1 << 5) + (y1 << 3) + (y1 << 2) + y1 + ((m1 << 4) + (m1 << 3) + (m1 << 2) + (m1 << 1)) + ((m1 >> 1) - 1) - (((((m1 << 2) + m1) >> 5) - 1) & (m & 1)) + ((1 >> (m1 - 1)) << 1) + ((16 - m1) >> 4 << 1) + d4 - (((d4 << 5) + (d4 << 3) + d4) >> 10) + (((d16 << 5) + (d16 << 3) + d16) >> 10) + d;
}
@csuzw
Copy link
Author

csuzw commented Nov 13, 2017

int a(int aͣ, int aͣͣ, int aͣͣͣ, int aͣͣͣͣ, int aͣͣͣͣͣ, int aͣͣͣͣͣͣ) => (((aͣͣͣͣ - 1) << 8) + ((aͣͣͣͣ - 1) << 6) + ((aͣͣͣͣ - 1) << 5) + ((aͣͣͣͣ - 1) << 3) + ((aͣͣͣͣ - 1) << 2) + (aͣͣͣͣ - 1) + (((aͣͣͣͣͣ - 1) << 4) + ((aͣͣͣͣͣ - 1) << 3) + ((aͣͣͣͣͣ - 1) << 2) + ((aͣͣͣͣͣ - 1) << 1)) + (((aͣͣͣͣͣ - 1) >> 1) - 1) - ((((((aͣͣͣͣͣ - 1) << 2) + (aͣͣͣͣͣ - 1)) >> 5) - 1) & (aͣͣͣͣͣ & 1)) + ((1 >> ((aͣͣͣͣͣ - 1) - 1)) << 1) + ((16 - (aͣͣͣͣͣ - 1)) >> 4 << 1) + ((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 2) - (((((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 2) << 5) + (((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 2) << 3) + ((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 2)) >> 10) + (((((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 4) << 5) + (((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 4) << 3) + ((aͣͣͣͣ - ((18 - aͣͣͣͣͣ) >> 4)) >> 4)) >> 10) + aͣͣͣͣͣͣ) - (((aͣ - 1) << 8) + ((aͣ - 1) << 6) + ((aͣ - 1) << 5) + ((aͣ - 1) << 3) + ((aͣ - 1) << 2) + (aͣ - 1) + (((aͣͣ - 1) << 4) + ((aͣͣ - 1) << 3) + ((aͣͣ - 1) << 2) + ((aͣͣ - 1) << 1)) + (((aͣͣ - 1) >> 1) - 1) - ((((((aͣͣ - 1) << 2) + (aͣͣ - 1)) >> 5) - 1) & (aͣͣ & 1)) + ((1 >> ((aͣͣ - 1) - 1)) << 1) + ((16 - (aͣͣ - 1)) >> 4 << 1) + ((aͣ - ((18 - aͣͣ) >> 4)) >> 2) - (((((aͣ - ((18 - aͣͣ) >> 4)) >> 2) << 5) + (((aͣ - ((18 - aͣͣ) >> 4)) >> 2) << 3) + ((aͣ - ((18 - aͣͣ) >> 4)) >> 2)) >> 10) + (((((aͣ - ((18 - aͣͣ) >> 4)) >> 4) << 5) + (((aͣ - ((18 - aͣͣ) >> 4)) >> 4) << 3) + ((aͣ - ((18 - aͣͣ) >> 4)) >> 4)) >> 10) + aͣͣͣ);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment