6

C++11-获取当天是周期几

 3 years ago
source link: http://www.banbeichadexiaojiubei.com/index.php/2020/12/16/c11-%e8%8e%b7%e5%8f%96%e5%bd%93%e5%a4%a9%e6%98%af%e5%91%a8%e6%9c%9f%e5%87%a0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
2020年12月16日2020年12月16日 | by YoungTimes | No comments

C++11-获取当天是周期几

C/C++中时间相关的结果大多与tm数据结构相关。

struct tm {
int tm_sec; // seconds of minutes from 0 to 60
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 23
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday-[0, 6]
int tm_yday; // days since January 1st-[0, 365]
int tm_isdst; // hours of daylight savings time
struct tm {
  int tm_sec; // seconds of minutes from 0 to 60
  int tm_min; // minutes of hour from 0 to 59
  int tm_hour; // hours of day from 0 to 23
  int tm_mday; // day of month from 1 to 31
  int tm_mon; // month of year from 0 to 11
  int tm_year; // year since 1900
  int tm_wday; // days since sunday-[0, 6]
  int tm_yday; // days since January 1st-[0, 365]
  int tm_isdst; // hours of daylight savings time
 }

获取今天星期几的代码如下:

#include <iostream>
#include <ctime>
using namespace std;
int main(int argc, const char * argv[]) {
const string DAY[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t ttime = time(0);
tm* local_time = localtime(&ttime);
int weekday = local_time->tm_wday;
cout << "Today is: " << DAY[weekday] << endl;
return 0;
#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, const char * argv[]) {
    const string DAY[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    
    time_t ttime = time(0);
    tm* local_time = localtime(&ttime);

    int weekday = local_time->tm_wday;
     cout << "Today is: " << DAY[weekday] << endl;
     return 0;
}

程序编译和执行:

g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

执行结果如下:

Today is: Wed
Today is: Wed

除了星期几之外,我们还可以获取当前的年份、月份、一个月中第几天、当前的时分秒等信息。

代码如下:

#include <iostream>
#include <ctime>
using namespace std;
int main() {
time_t ttime = time(0);
cout << "Number of seconds elapsed since January 1, 1990:" << ttime << endl;
tm *local_time = localtime(&ttime);
cout << "Year: "<< 1900 + local_time->tm_year << endl;
cout << "Month: "<< 1 + local_time->tm_mon<< endl;
cout << "Day: "<< local_time->tm_mday << endl;
cout << "Week: "<< local_time->tm_wday << endl;
cout << "Time: "<< local_time->tm_hour << ":";
cout << local_time->tm_min << ":";
cout << local_time->tm_sec << endl;
#include <iostream>
#include <ctime>

using namespace std;
 
int main() {
  time_t ttime = time(0);
  cout << "Number of seconds elapsed since January 1, 1990:" << ttime << endl;
   tm *local_time = localtime(&ttime);
    
   cout << "Year: "<< 1900 + local_time->tm_year << endl;
   cout << "Month: "<< 1 + local_time->tm_mon<< endl;
   cout << "Day: "<< local_time->tm_mday << endl;
   cout << "Week: "<< local_time->tm_wday << endl;
   cout << "Time: "<< local_time->tm_hour << ":";
   cout << local_time->tm_min << ":";
   cout << local_time->tm_sec << endl;
 }

程序编译:

g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

程序输出:

Number of seconds elapsed since January 1, 1990:1608131812
Year: 2020
Month: 12
Day: 16
Week: 3
Time: 15:16:52
Number of seconds elapsed since January 1, 1990:1608131812
Year: 2020
Month: 12
Day: 16
Week: 3
Time: 15:16:52

http://www.banbeichadexiaojiubei.com/wp-admin/post.php?post=4465&action=edit

除非注明,否则均为[半杯茶的小酒杯]原创文章,转载必须以链接形式标明本文链接

本文链接:http://www.banbeichadexiaojiubei.com/index.php/2020/12/16/c11-%e8%8e%b7%e5%8f%96%e5%bd%93%e5%a4%a9%e6%98%af%e5%91%a8%e6%9c%9f%e5%87%a0/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK