C++:编写函数,显示当前时间
来源:互联网 发布:阿里妈妈淘宝联盟规则 编辑:程序博客网 时间:2024/04/24 03:13
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
/** Determine if it is a leap year */
bool isLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Get the total number of days from Jan 1, 1970 to the specified year */
int getTotalDaysInYears(int year)
{
int total = 0;
// Get the total days from 1970 to the specified year
for (int i = 1970; i <= year; i++)
{
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
}
return total;
}
int getNumOfDaysInMonth(int year, int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0; // If month is incorrect.
}
/** Get the total number of days from Jan 1 to the month in the year */
int getTotalDaysInMonths(int year, int month)
{
int total = 0;
// Add days from Jan to the month
for (int i = 1; i <= month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
int main()
{
// Obtain the total seconds since the midnight, Jan 1, 1970
long totalSeconds = time(0);
// Compute the current second in the minute in the hour
int currentSecond = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
int currentMinute = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
int currentHour = (int)(totalHours % 24);
// Compute the total days
int totalDays = (int)(totalHours / 24);
if (currentHour > 0) totalDays++;
// Obtain Year
int currentYear = 2000;
do
{
currentYear++;
}
while (getTotalDaysInYears(currentYear) < totalDays);
// Obtain Month
int totalNumOfDaysInTheYear = totalDays - getTotalDaysInYears(currentYear - 1);
int currentMonth = 0;
do
{
currentMonth++;
}
while (getTotalDaysInMonths(currentYear, currentMonth) < totalNumOfDaysInTheYear);
// Obtain Day
int currentDay = totalNumOfDaysInTheYear - getTotalDaysInMonths(currentYear, currentMonth - 1);
// Display results
cout << "Current date and time is " << currentMonth << "/" << currentDay <<
"/" << currentYear << " " << currentHour+8 << ":"
<< currentMinute << ":" << currentSecond << " GMT" << endl;
return 0;
}
#include <iomanip>
#include <ctime>
using namespace std;
/** Determine if it is a leap year */
bool isLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Get the total number of days from Jan 1, 1970 to the specified year */
int getTotalDaysInYears(int year)
{
int total = 0;
// Get the total days from 1970 to the specified year
for (int i = 1970; i <= year; i++)
{
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
}
return total;
}
int getNumOfDaysInMonth(int year, int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0; // If month is incorrect.
}
/** Get the total number of days from Jan 1 to the month in the year */
int getTotalDaysInMonths(int year, int month)
{
int total = 0;
// Add days from Jan to the month
for (int i = 1; i <= month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
int main()
{
// Obtain the total seconds since the midnight, Jan 1, 1970
long totalSeconds = time(0);
// Compute the current second in the minute in the hour
int currentSecond = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
int currentMinute = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
int currentHour = (int)(totalHours % 24);
// Compute the total days
int totalDays = (int)(totalHours / 24);
if (currentHour > 0) totalDays++;
// Obtain Year
int currentYear = 2000;
do
{
currentYear++;
}
while (getTotalDaysInYears(currentYear) < totalDays);
// Obtain Month
int totalNumOfDaysInTheYear = totalDays - getTotalDaysInYears(currentYear - 1);
int currentMonth = 0;
do
{
currentMonth++;
}
while (getTotalDaysInMonths(currentYear, currentMonth) < totalNumOfDaysInTheYear);
// Obtain Day
int currentDay = totalNumOfDaysInTheYear - getTotalDaysInMonths(currentYear, currentMonth - 1);
// Display results
cout << "Current date and time is " << currentMonth << "/" << currentDay <<
"/" << currentYear << " " << currentHour+8 << ":"
<< currentMinute << ":" << currentSecond << " GMT" << endl;
return 0;
}
- C++:编写函数,显示当前时间
- 【C语言】编写一个程序显示当前时间。
- c语言显示当前时间
- C实例---显示当前时间
- 显示当前系统时间(Linux C++)
- <C++>获取并显示当前时间
- 编写安卓app程序显示当前系统时间
- C语言gettimeofday()函数:获取当前时间
- 当前时间显示
- 显示当前时间
- JavaScript显示当前时间
- 显示当前系统时间
- 完整显示当前时间
- 显示当前时间
- 网页显示当前时间
- 显示当前时间
- js显示当前时间
- ios 显示当前时间
- 原:用C/C++扩展PHP
- 嵌入式linux下u盘自动挂载的实现
- secureCRT和Xshell登录ubuntu
- 设计模式-Factory,Abstract Factory,SingleTon,Build模式
- 算法导论-4-6 VLSI芯片测试
- C++:编写函数,显示当前时间
- IE6Bug(WEB前端开发 http://www.css88.com/)
- JavaScript跨域总结与解决办法
- 标准模版库(STL)
- Opencv。。
- 学了两天java,第一次无参照实现的链表,纪念一下
- 能被17和37整除的数
- 关于拦截器和过滤器以及实现国际化和session超时验证
- C++:显示每年的各月份第一天是星期几