0
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

Organization

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

ログインチェック

#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
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?