Thursday, October 28, 2010


Problem Links:



Time Zones
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 2050
Accepted: 562
Prior to the late nineteenth century, time keeping was a purely local phenomenon. Each town would set their clocks to noon when the sun reached its zenith each day. A clockmaker or town clock would be the "official" time and the citizens would set their pocket watches and clocks to the time of the town - enterprising citizens would offer their services as mobile clock setters, carrying a watch with the accurate time to adjust the clocks in customer's homes on a weekly basis. Travel between cities meant having to change one's pocket watch upon arrival.
However, once railroads began to operate and move people rapidly across great distances, time became much more critical. In the early years of the railroads, the schedules were very confusing because each stop was based on a different local time. The standardization of time was essential to efficient operation of railroads.

In 1878, Canadian Sir Sanford Fleming proposed the system of worldwide time zones that we use today. He recommended that the world be divided into twenty-four time zones, each spaced 15 degrees of longitude apart. Since the earth rotates once every 24 hours and there are 360 degrees of longitude, each hour the earth rotates one-twenty-fourth of a circle or 15° of longitude. Sir Fleming's time zones were heralded as a brilliant solution to a chaotic problem worldwide.

United States railroad companies began utilizing Fleming's standard time zones on November 18, 1883. In 1884 an International Prime Meridian Conference was held in Washington D.C. to standardize time and select the Prime Meridian. The conference selected the longitude of Greenwich, England as zero degrees longitude and established the 24 time zones based on the Prime Meridian. Although the time zones had been established, not all countries switched immediately. Though most U.S. states began to adhere to the Pacific, Mountain, Central, and Eastern time zones by 1895, Congress didn't make the use of these time zones mandatory until the Standard Time Act of 1918.

Today, many countries operate on variations of the time zones proposed by Sir Fleming. All of China (which should span five time zones) uses a single time zone - eight hours ahead of Coordinated Universal Time (known by the abbreviation UTC - based on the time zone running through Greenwich at 0° longitude). Russia adheres to its designated time zones although the entire country is on permanent Daylight Saving Time and is an hour ahead of their actual zones. Australia uses three time zones - its central time zone is a half-hour ahead of its designated time zone. Several countries in the Middle East and South Asia also utilize half-hour time zones.

Since time zones are based on segments of longitude and lines of longitude narrow at the poles, scientists working at the North and South Poles simply use UTC time. Otherwise, Antarctica would be divided into 24 very thin time zones!

Time zones have recently been given standard capital-letter abbreviations as follows:

UTC Coordinated Universal Time
GMT Greenwich Mean Time, defined as UTC
BST British Summer Time, defined as UTC+1 hour
IST Irish Summer Time, defined as UTC+1 hour
WET Western Europe Time, defined as UTC
WEST Western Europe Summer Time, defined as UTC+1 hour
CET Central Europe Time, defined as UTC+1
CEST Central Europe Summer Time, defined as UTC+2
EET Eastern Europe Time, defined as UTC+2
EEST Eastern Europe Summer Time, defined as UTC+3
MSK Moscow Time, defined as UTC+3
MSD Moscow Summer Time, defined as UTC+4
AST Atlantic Standard Time, defined as UTC-4 hours
ADT Atlantic Daylight Time, defined as UTC-3 hours
NST Newfoundland Standard Time, defined as UTC-3.5 hours
NDT Newfoundland Daylight Time, defined as UTC-2.5 hours
EST Eastern Standard Time, defined as UTC-5 hours
EDT Eastern Daylight Saving Time, defined as UTC-4 hours
CST Central Standard Time, defined as UTC-6 hours
CDT Central Daylight Saving Time, defined as UTC-5 hours
MST Mountain Standard Time, defined as UTC-7 hours
MDT Mountain Daylight Saving Time, defined as UTC-6 hours
PST Pacific Standard Time, defined as UTC-8 hours
PDT Pacific Daylight Saving Time, defined as UTC-7 hours
HST Hawaiian Standard Time, defined as UTC-10 hours
AKST Alaska Standard Time, defined as UTC-9 hours
AKDT Alaska Standard Daylight Saving Time, defined as UTC-8 hours
AEST Australian Eastern Standard Time, defined as UTC+10 hours
AEDT Australian Eastern Daylight Time, defined as UTC+11 hours
ACST Australian Central Standard Time, defined as UTC+9.5 hours
ACDT Australian Central Daylight Time, defined as UTC+10.5 hours
AWST Australian Western Standard Time, defined as UTC+8 hours

Given the current time in one time zone, you are to compute what time it is in another time zone.
The first line of input contains N, the number of test cases. For each case a line is given with a time, and 2 time zone abbreviations. Time is given in standard a.m./p.m. format with midnight denoted "midnight" and noon denoted "noon" (12:00 a.m. and 12:00 p.m. are oxymorons).
For each case, assuming the given time is the current time in the first time zone, give the current time in the second time zone.
Sample Input
noon HST CEST 
11:29 a.m. EST GMT
6:01 p.m. CST UTC
12:40 p.m. ADT MSK
Sample Output
4:29 p.m.
12:01 a.m.
6:40 p.m.
Waterloo local 2002.09.28


Hard to read, but easy to implement.

Source Code:

//Fri May 21 16:08:01 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

char Zone[32][5] =
{ "UTC", "GMT", "BST", "IST", "WET", "WEST", "CET", "CEST", "EET", "EEST",
        "MSK", "MSD", "AST", "ADT", "NST", "NDT", "EST", "EDT", "CST", "CDT",
        "MST", "MDT", "PST", "PDT", "HST", "AKST", "AKDT", "AEST", "AEDT",
        "ACST", "ACDT", "AWST" };

float Diff[32] =
{ 0, 0, 1, 1, 0, 1, 1, 2, 2, 3, 3, 4, -4, -3, -3.5, -2.5, -5, -4, -6, -5, -7,
        -6, -8, -7, -10, -9, -8, 10, 11, 9.5, 10.5, 8 };

int main(int argc, const char* argv[])
//  freopen("", "r", stdin);
//  freopen("output.out", "w", stdout);
    int t, h, m, i, j, min;
    char time[9], tmp, zone[5];
    cin >> t;
    while (t--)
        cin >> time;
        if (time[0] == 'n')
            h = 12, m = 0;
        else if (time[0] == 'm')
            h = 24, m = 0;
            istringstream is(time);
            is >> h, is >> tmp, is >> m;
            h %= 12;
            cin >> time;
            if (time[0] == 'p')
                h += 12;
        i = j = 0;
        cin >> zone;
        while (strcmp(zone, Zone[i]))
        cin >> zone;
        while (strcmp(zone, Zone[j]))
        min = 60 * h + m + int(60 * (Diff[j] - Diff[i]));
        if (min < 0)
            min += 1440;
        min %= 1440;
        if (!min)
            cout << "midnight\n";
        else if (min == 720)
            cout << "noon\n";
            m = min % 60, h = min / 60;
            if (!h)
                cout << "12:" << setw(2) << setfill('0') << m << " a.m.\n";
            else if (h < 12)
                cout << h << ':' << setw(2) << setfill('0') << m << " a.m.\n";
            else if (h == 12)
                cout << h << ':' << setw(2) << setfill('0') << m << " p.m.\n";
                cout << h - 12 << ':' << setw(2) << setfill('0') << m
                        << " p.m.\n";
//  fclose(stdin);
//  fclose(stdout);
    return 0;

No comments :