Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Last active December 27, 2015 12:59
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 cathandnya/7329457 to your computer and use it in GitHub Desktop.
Save cathandnya/7329457 to your computer and use it in GitHub Desktop.
//
// EndlessSummer.c
// Tumbltail
//
// Created by nya on 2013/11/05.
//
//
#import "EndlessSummer.h"
typedef struct {
double a1;
int a2;
double b1;
int b2;
long long upperBound;
} ESParamBase;
#define PARAM_COUNT (10)
#define COEF_A(base) (base.a1 * pow(10, base.a2))
#define COEF_B(base) (base.b1 * pow(10, base.b2))
static const ESParamBase baseParams[PARAM_COUNT] = {
{2.374, -268, 5.342, -10, 1182114958836},
{2.292, -63, 1.349, -10, 1193135035175},
{1.214, -11, 3.509, -11, 1241622616806},
{1.259, -22, 5.546, -11, 1283088065116},
{3.805, -29, 6.716, -11, 1291075200000},
{5.579, -28, 6.521, -11, 1292241600000},
{4.482, -28, 6.541, -11, 1307576237375},
{2.424, -28, 6.588, -11, 1313586050187},
{3.923, -13, 3.922, -11, 1340572542928},
{2.157, -3, 2.249, -11, LLONG_MAX},
};
long long GetPostIDFromUnixTime(long long utime) {
if (utime < 1149465600000) {
return -1;
} else if (utime == LLONG_MAX) {
return LLONG_MAX;
}
for (int i = 0; i < PARAM_COUNT; i++) {
if (utime <= baseParams[i].upperBound) {
return ceil(COEF_A(baseParams[i]) * exp(COEF_B(baseParams[i]) * utime));
}
}
return -1;
}
long long GetUnixTimeFromPostID(long long postID) {
if (postID < 1) { return -1; }
for (int i = 0; i < PARAM_COUNT; i++) {
if (postID <= GetPostIDFromUnixTime(baseParams[i].upperBound)) {
return floor(log(postID / COEF_A(baseParams[i])) / COEF_B(baseParams[i])) / 1000;
}
}
return -1;
}
long long GetRandomPostID() {
struct tm tm = {
0, 0, 0, // 秒, 分, 時 (0~)
1, // 日 (1~31)
0, // 月 (0~11)
108, // 年 (1900 年からの年数)
};
long long epoch = mktime(&tm); // 2008/1/1
long long now = time(NULL) - 60 * 60;
return GetPostIDFromUnixTime(1000 * (epoch + (now - epoch) * rand() / RAND_MAX));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment