C++ 获取时间戳:下周一、月初、月中、月末
在一次开发过程中需要利用当前时间获取下周一、这个个月月中、这个月月末和下个月月初的时间戳,在此做一个总结。
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <time.h>
#define ONE_DAY (86400)
using namespace std;
int GetWeek(long now_timestamp) {
struct tm gmtm;
gmtime_r(&now_timestamp, &gmtm);
int day_of_week = gmtm.tm_wday;
return day_of_week == 0?7:day_of_week;
}
int GetDay(long now_timestamp) {
struct tm gmtm;
gmtime_r(&now_timestamp, &gmtm);
int day_of_month = gmtm.tm_mday;
return day_of_month;
}
int GetYears(long now_timestamp) {
struct tm gmtm;
gmtime_r(&now_timestamp, &gmtm);
int years = gmtm.tm_year;
return years;
}
int GetMonth(long now_timestamp) {
struct tm gmtm;
gmtime_r(&now_timestamp, &gmtm);
int month = gmtm.tm_mon;
return month;
}
int GetDaysInMonth(long now_timestamp)
{
int now_years = GetYears(now_timestamp