⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpptime.h

📁 Thinking in C++ 2nd edition source code which are all the cores of the book Thinking in C++ second e
💻 H
字号:
//: C09:Cpptime.h
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// A simple time class
#ifndef CPPTIME_H_
#define CPPTIME_H_
#include <ctime>
#include <cstring>

class Time {
  std::time_t t;
  std::tm local;
  char Ascii[26];
  unsigned char lflag, aflag;
  void updateLocal() {
    if(!lflag) {
      local = *std::localtime(&t);
      lflag++;
    }
  }
  void updateAscii() {
    if(!aflag) {
      updateLocal();
      strcpy(Ascii, std::asctime(&local));
      aflag++;
    }
  }
public:
  Time() { mark(); }
  void mark() {
    lflag = aflag = 0;
    std::time(&t);
  }
  const char* ascii() {
    updateAscii();
    return Ascii;
  }
  // Difference in seconds:
  int delta(Time* dt) const {
    return std::difftime(t, dt->t);
  }
  int DaylightSavings() {
    updateLocal();
    return local.tm_isdst;
  }
  int DayOfYear() { // Since January 1
    updateLocal();
    return local.tm_yday;
  }
  int DayOfWeek() { // Since Sunday
    updateLocal();
    return local.tm_wday;
  }
  int Since1900() { // Years since 1900
    updateLocal();
    return local.tm_year;
  }
  int Month() { // Since January
    updateLocal();
    return local.tm_mon;
  }
  int DayOfMonth() {
    updateLocal();
    return local.tm_mday;
  }
  int Hour() { // Since midight, 24-hour clock
    updateLocal();
    return local.tm_hour;
  }
  int Minute() {
    updateLocal();
    return local.tm_min;
  }
  int Second() {
    updateLocal();
    return local.tm_sec;
  }
};
#endif // CPPTIME_H_ ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -