LoginSignup
0
0

More than 5 years have passed since last update.

ログインしたタイミングが前回のログインから日をまたいでるかチェックする

Posted at
ログインチェック

#include <iostream>
#include <time.h>

using namespace std;

/**
 * now - 現在の日時
 * time - 前回のログイン日時
 * hours - 日付更新時刻
 * timezone - タイムゾーン
 */
bool loginCheck(int now, int time, int hours, int timezone)
{
    int tz = timezone * 3600;
    int offset = -hours * 3600;
    now += tz + offset;
    now -= now % 86400;
    time += tz + offset;
    return now > time;
}

//------- test >>

int dt(char *s)
{
    struct tm tm_;
    memset(&tm_, 0, sizeof(struct tm));
    strptime(s, "%Y-%m-%d %H:%M:%S", &tm_);
    return mktime(&tm_);
}

string toString(bool b)
{
  return b ? "true" : "false";
}

int main(int argc, char *argv[]) 
{
    // loginCheck(現在の日時, 前回のログイン日時, 日付更新時刻, タイムゾーン);
    cout << toString(loginCheck(dt("2013-01-01 03:59:59"), dt("2012-12-31 03:59:59"), 4, 9)) << "\n"; // true
    cout << toString(loginCheck(dt("2013-01-01 03:59:59"), dt("2012-12-31 04:00:00"), 4, 9)) << "\n"; // false
    cout << toString(loginCheck(dt("2013-01-01 04:00:00"), dt("2012-12-31 04:00:00"), 4, 9)) << "\n"; // true
    cout << toString(loginCheck(dt("2013-01-01 00:00:00"), dt("2012-12-31 23:59:59"), 4, 9)) << "\n"; // false
    cout << toString(loginCheck(dt("2013-01-01 04:00:00"), dt("2013-01-01 04:00:00"), 4, 9)) << "\n"; // false
    cout << toString(loginCheck(dt("2013-01-01 03:59:59"), dt("2013-01-01 00:00:00"), 4, 9)) << "\n"; // false
    cout << toString(loginCheck(dt("2013-01-01 04:00:00"), dt("2013-01-01 03:59:59"), 4, 9)) << "\n"; // true
    cout << toString(loginCheck(dt("2013-01-01 04:00:00"), dt("2013-01-01 04:00:00"), 4, 9)) << "\n"; // false
    cout << toString(loginCheck(dt("2013-01-01 04:00:01"), dt("2013-01-01 04:00:00"), 4, 9)) << "\n"; // false
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0