代码如下:
#include<iostream>
#include<ctime>
using namespace std;
//以12小时制显示当前时间
int main ()
{
cout << "Enter the time zone offset to GMT : ";
float offset;
cin >> offset;
//Obtain the total seconds since the midnight ,Jan 1 , 1970
int totalSeconds = time(0) + 60*60*offset;
int currentSecond = totalSeconds % 60;
int totalMinutes = totalSeconds / 60;
int currentMinute = totalMinutes % 60;
int totalHours = totalMinutes / 60;
int currentHour = totalHours % 24 ;
if (currentHour < 12)
cout << "The current time is "<< currentHour
<<" : "<<currentMinute<<" : "<< currentSecond<<" AM"<<endl;
else
cout << "The current time is "<< currentHour-12
<<" : "<<currentMinute<<" : "<< currentSecond<<" PM"<<endl;
return 0;
}
运行结果: