1--- 2layout: default 3title: Date and Time Zone Examples 4nav_order: 4 5parent: Date/Time 6--- 7<!-- 8© 2020 and later: Unicode, Inc. and others. 9License & terms of use: http://www.unicode.org/copyright.html 10--> 11 12# Date and Time Zone Examples 13{: .no_toc } 14 15## Contents 16{: .no_toc .text-delta } 17 181. TOC 19{:toc} 20 21--- 22 23## C++ TimeZone example code 24 25This example code illustrates some time zone operations. 26 27```c++ 28 UErrorCode success = U_ZERO_ERROR; 29 UnicodeString dateReturned, curTZNameEn, curTZNameFr; 30 UDate curDate; 31 int32_t stdOffset,dstOffset; 32 33 // Create a Time Zone with America/Los_Angeles 34 TimeZone *tzWest = TimeZone::createTimeZone("America/Los_Angeles"); 35 36 37// Print out the Time Zone Name, GMT offset etc. 38 curTZNameEn = tzWest->getDisplayName(Locale::getEnglish(),curTZNameEn); 39 u_printf("%s\n","Current Time Zone Name in English:"); 40 u_printf("%S\n", curTZNameEn.getTerminatedBuffer()); 41 42 curTZNameFr = tzWest->getDisplayName(Locale::getCanadaFrench(),curTZNameFr); 43 u_printf("%s\n","Current Time Zone Name in French:"); 44 u_printf("%S\n", curTZNameFr.getTerminatedBuffer()); 45 46 47 // Create a Calendar to get current date 48 Calendar* calendar = Calendar::createInstance(success); 49 curDate = calendar->getNow(); 50 51 52 // Print out the Current Date/Time in the given time zone 53 DateFormat *dt = DateFormat::createDateInstance(); 54 dateReturned = dt->format(curDate,dateReturned,success); 55 u_printf("%s\n", "Current Time:"); 56 u_printf("%S\n", dateReturned.getTerminatedBuffer()); 57 58 59 // Use getOffset to get the stdOffset and dstOffset for the given time 60 tzWest->getOffset(curDate,true,stdOffset,dstOffset,success); 61 u_printf("%s\n%d\n","Current Time Zone STD offset:",stdOffset/(1000*60*60)); 62 u_printf("%s\n%d\n","Current Time Zone DST offset:",dstOffset/(1000*60*60)); 63 u_printf("%s\n", "Current date/time is in daylight savings time?"); 64 u_printf("%s\n", (calendar->inDaylightTime(success))?"Yes":"No"); 65 66 67 // Use createTimeZoneIDEnumeration to get the specific Time Zone IDs 68 // in United States with -5 hour standard offset from GMT 69 stdOffset = (-5)*U_MILLIS_PER_HOUR; // U_MILLIS_PER_HOUR = 60*60*1000; 70 StringEnumeration *ids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION,"US",&stdOffset,success); 71 for (int i=0; i<ids->count(success);i++) { 72 u_printf("%s\n",ids->next(NULL,success)); 73 } 74 75 76 // Use Calendar to get the hour of the day for different time zones 77 int32_t hour1,hour2; 78 TimeZone *tzEast = TimeZone::createTimeZone("America/New_York"); 79 Calendar * cal1 = Calendar::createInstance(tzWest,success); 80 Calendar * cal2 = Calendar::createInstance(tzEast,success); 81 hour1 = cal1->get(UCAL_HOUR_OF_DAY,success); 82 hour2 = cal2->get(UCAL_HOUR_OF_DAY,success); 83 u_printf("%s\n%d\n","Current hour of the day in North American West: ", hour1); 84 u_printf("%s\n%d\n","Current hour of the day in North American East: ", hour2); 85 86 delete cal1; 87 delete cal2; 88 delete ids; 89 delete calendar; 90 delete dt; 91 ``` 92