00001 // $Id: cctime.cpp 1320 2006-06-14 14:39:32Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 // implementation file for cctime class 00099 00100 /* 00101 */ 00102 00103 #include "camtypes.h" 00104 //#include "basestr.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00105 //#include "convert.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00106 #include "cctime.h" 00107 #include <time.h> 00108 00109 CC_IMPLEMENT_DYNCREATE( CCTimeBase, CCObject ) 00110 CC_IMPLEMENT_DYNCREATE( CCTime, CCTimeBase ) 00111 CC_IMPLEMENT_DYNCREATE( CCGmtTime, CCTimeBase ) 00112 00113 #define new CAM_DEBUG_NEW 00114 00115 /********************************************************************************************* 00116 00117 > CCTime::CCTimeBase() 00118 00119 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00120 Created: 21/08/96 00121 Purpose: CCTimeBase constructor, doesn't do much for the moment 00122 00123 **********************************************************************************************/ 00124 00125 CCTimeBase::CCTimeBase() 00126 { 00127 TheTime.tm_sec = 0; // seconds after the minute - [0,59] 00128 TheTime.tm_min = 0; // minutes after the hour - [0,59] 00129 TheTime.tm_hour = 0; // hours since midnight - [0,23] 00130 TheTime.tm_mday = 0; // day of the month - [1,31] 00131 TheTime.tm_mon = 0; // months since January - [0,11] 00132 TheTime.tm_year = 0; // years since 1900 00133 TheTime.tm_wday = 0; // days since Sunday - [0,6] 00134 TheTime.tm_yday = 0; // days since January 1 - [0,365] 00135 TheTime.tm_isdst = 0; // daylight savings time flag 00136 } 00137 00138 00139 /********************************************************************************************* 00140 00141 > CCTimeBase::~CCTimeBase() 00142 00143 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00144 Created: 21/08/96 00145 Purpose: Destructor for a CCTimeBase object 00146 00147 **********************************************************************************************/ 00148 00149 CCTimeBase::~CCTimeBase() 00150 { 00151 } 00152 00153 /********************************************************************************************* 00154 00155 > cctime* CCTimeBase::GetTimeStruct() 00156 00157 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00158 Created: 21/08/96 00159 Purpose: Return a pointer to our internal time structure for derived classes to use. 00160 They should really use the access functions for all but the initialisation 00161 of this structure. 00162 00163 **********************************************************************************************/ 00164 00165 cctime* CCTimeBase::GetTimeStruct() 00166 { 00167 return &TheTime; 00168 } 00169 00170 00171 00172 /********************************************************************************************* 00173 00174 > INT32 CCTimeBase::GetDay() 00175 00176 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00177 Created: 21/08/96 00178 Purpose: returns the Day of the month, ranges between 1 and 31 00179 00180 **********************************************************************************************/ 00181 00182 INT32 CCTimeBase::GetDay() 00183 { 00184 return TheTime.tm_mday; 00185 } 00186 00187 00188 /********************************************************************************************* 00189 00190 > INT32 CCTimeBase::GetDayOfWeek() 00191 00192 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00193 Created: 21/08/96 00194 Purpose: return the day of the week, 1=Sunday, 2=Monday etc 00195 00196 **********************************************************************************************/ 00197 00198 INT32 CCTimeBase::GetDayOfWeek() 00199 { 00200 return TheTime.tm_wday; 00201 } 00202 00203 00204 /********************************************************************************************* 00205 00206 > INT32 CCTimeBase::Get24Hour() 00207 00208 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00209 Created: 21/08/96 00210 Purpose: returns the hour, based on local time, in the range 0 through 23 00211 00212 **********************************************************************************************/ 00213 00214 INT32 CCTimeBase::GetHour() 00215 { 00216 return TheTime.tm_hour; 00217 } 00218 00219 INT32 CCTimeBase::Get12Hour() 00220 { 00221 INT32 t = GetHour(); 00222 if (t>12) t-=12; 00223 if (t==0) t+=12; // adjust for midnight 0 time 00224 return t; 00225 } 00226 00227 INT32 CCTimeBase::Get24Hour() 00228 { 00229 return GetHour(); 00230 } 00231 00232 00233 /********************************************************************************************* 00234 00235 > INT32 CCTimeBase::GetMinute() 00236 00237 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00238 Created: 21/08/96 00239 Purpose: Returns the minute based on local time, in the range 0 through 59. 00240 00241 **********************************************************************************************/ 00242 00243 INT32 CCTimeBase::GetMinute() 00244 { 00245 return TheTime.tm_min; 00246 } 00247 00248 00249 /********************************************************************************************* 00250 00251 > INT32 CCTimeBase::GetMonth() 00252 00253 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00254 Created: 21/08/96 00255 Purpose: Returns the month based on local time, in the range 1 through 12, (1=January) 00256 00257 **********************************************************************************************/ 00258 00259 INT32 CCTimeBase::GetMonth() 00260 { 00261 return TheTime.tm_mon; 00262 } 00263 00264 00265 /********************************************************************************************* 00266 00267 > INT32 CCTimeBase::GetSecond() 00268 00269 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00270 Created: 21/08/96 00271 Purpose: Returns the second based on local time, in the range 0 through 59, 00272 00273 **********************************************************************************************/ 00274 00275 INT32 CCTimeBase::GetSecond() 00276 { 00277 return TheTime.tm_sec; 00278 } 00279 00280 00281 /********************************************************************************************* 00282 00283 > INT32 CCTimeBase::GetYear() 00284 00285 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00286 Created: 21/08/96 00287 Purpose: Returns the year based on local time, in the range 1970 through 2038, 00288 00289 **********************************************************************************************/ 00290 00291 INT32 CCTimeBase::GetYear() 00292 { 00293 return TheTime.tm_year; 00294 } 00295 00296 00297 /********************************************************************************************* 00298 00299 > CCTimeBase& CCTimeBase::operator=(const CCTimeBase& timeSrc) 00300 00301 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00302 Created: 21/08/96 00303 Purpose: Assign one CCTimeBase object to another 00304 00305 **********************************************************************************************/ 00306 00307 const CCTimeBase& CCTimeBase::operator=(const CCTimeBase& timeSrc) 00308 { 00309 TheTime = timeSrc.TheTime; 00310 return (*this); 00311 } 00312 00313 00314 /********************************************************************************************* 00315 00316 > void CCTimeBase::ConvertStandardDateAndTime(const String_64 *pFormat, String_256 *pOutput) 00317 00318 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00319 Created: 21/08/96 00320 Inputs: pFormat = a pointer to a format string 00321 pOutput = a pointer to a string to receive the formated date and time 00322 Outputs pOutput = contains the formated date and time 00323 Returns: - 00324 Purpose: Build a date and time string using the format string provided. 00325 Allowable format fields are 00326 %yr = year 00327 %mt = month 00328 %dy = day 00329 %hr = hours (24 hour clock) 00330 %mn = minutes 00331 %sc = seconds 00332 %th = hours (12 hour clock) 00333 %pm = am/pm characters 00334 %% = % 00335 These fields should be embedded in the format string along with other 00336 characters eg 00337 "%hr/%mn/%sc" = 19/22/33 00338 "%dy/%mt/%yr" = 21/12/1996 00339 "%dy/%mt/%yr - %hr/%mn/%sc" = 21/12/1996 - 19/22/33" 00340 "%dy/%mt/%yr - %th.%mn %pm" = 21/12/1996 - 6.45 am 00341 00342 **********************************************************************************************/ 00343 00344 void CCTimeBase::ConvertStandardDateAndTime(const String_64 *pFormat, String_256 *pOutput) 00345 { 00346 if (pFormat==NULL || pOutput==NULL) 00347 return; 00348 00349 String_64 Format = (*pFormat); 00350 00351 const TCHAR percent = TEXT('%'); 00352 00353 // find the number of characters in this string 00354 INT32 ilen = Format.Length(); 00355 INT32 ipos = 0; 00356 String_32 tempstr; 00357 INT32 num,az; 00358 TCHAR ch0,ch1,ch2; 00359 00360 while (ipos<ilen) 00361 { 00362 ch0 = Format[ipos]; 00363 if (ch0==percent) 00364 { 00365 if ((ipos+2) < ilen) 00366 { 00367 ch1 = Format[ipos+1]; 00368 ch2 = Format[ipos+2]; 00369 00370 if (ch1==percent) 00371 { 00372 (*pOutput) += ch1; 00373 ipos+=1; 00374 } 00375 else 00376 { 00377 if (ch1==TEXT('p') && ch2==TEXT('m')) 00378 { 00379 (*pOutput) += ((GetHour()>12) ? TEXT("pm") : TEXT("am")); 00380 ipos+=2; 00381 } 00382 else 00383 { 00384 num = -1; 00385 az = 0; 00386 00387 if (ch1==TEXT('y') && ch2==TEXT('r')) { num = (INT32)GetYear() % 100; 00388 az=1; } 00389 else if (ch1==TEXT('m') && ch2==TEXT('t')) { num = (INT32)GetMonth(); az=0; } 00390 else if (ch1==TEXT('d') && ch2==TEXT('y')) { num = (INT32)GetDay(); az=0; } 00391 else if (ch1==TEXT('h') && ch2==TEXT('r')) { num = (INT32)GetHour(); az=1; } 00392 else if (ch1==TEXT('m') && ch2==TEXT('n')) { num = (INT32)GetMinute();az=1; } 00393 else if (ch1==TEXT('s') && ch2==TEXT('c')) { num = (INT32)GetSecond();az=1; } 00394 else if (ch1==TEXT('t') && ch2==TEXT('h')) { num = (INT32)Get12Hour();az=0; } 00395 00396 if (num>-1) 00397 { 00398 Convert::LongToString(num, &tempstr); 00399 if (az==1 && num<10) 00400 (*pOutput)+=TEXT('0'); 00401 (*pOutput) += tempstr; 00402 ipos+=2; 00403 } 00404 } 00405 } 00406 } 00407 else 00408 (*pOutput) += ch0; 00409 } 00410 else 00411 (*pOutput) += ch0; 00412 00413 ipos++; 00414 } 00415 } 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 /********************************************************************************************* 00429 00430 > CCTime::CCTime() 00431 00432 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00433 Created: 21/08/96 00434 Purpose: Constructor for CCTime object. We get the current local time ready for 00435 use. 00436 00437 **********************************************************************************************/ 00438 00439 CCTime::CCTime() 00440 { 00441 // Force the time to be up-to-the-second 00442 GetCurrentTime(); 00443 } 00444 00445 /********************************************************************************************* 00446 00447 > void CCTime::GetCurrentTime() 00448 00449 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00450 Created: 21/08/96 00451 Purpose: Updates this CCTime object with the date and time now. 00452 00453 **********************************************************************************************/ 00454 00455 void CCTime::GetCurrentTime() 00456 { 00457 struct tm *newtime; 00458 time_t long_time; 00459 00460 // Set time zone from TZ environment variable. If TZ is not set, 00461 // operating system default is used, otherwise PST8PDT is used 00462 // (Pacific standard time, daylight savings). 00463 // _tzset(); 00464 00465 time( &long_time ); // Get time as long integer. 00466 newtime = localtime( &long_time ); // Convert to local time. 00467 00468 cctime *p_time = GetTimeStruct(); 00469 00470 p_time->tm_sec = newtime->tm_sec; 00471 p_time->tm_min = newtime->tm_min; 00472 p_time->tm_hour = newtime->tm_hour; 00473 p_time->tm_mday = newtime->tm_mday; 00474 p_time->tm_mon = newtime->tm_mon; 00475 p_time->tm_year = newtime->tm_year; 00476 p_time->tm_wday = newtime->tm_wday; 00477 p_time->tm_yday = newtime->tm_yday; 00478 p_time->tm_isdst= newtime->tm_isdst; 00479 } 00480 00481 /********************************************************************************************* 00482 00483 > INT32 CCTime::GetMonth() 00484 00485 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00486 Created: 21/08/96 00487 Purpose: Returns the month based on local time, in the range 1 through 12, (1=January) 00488 00489 **********************************************************************************************/ 00490 00491 INT32 CCTime::GetMonth() 00492 { 00493 return CCTimeBase::GetMonth() + 1; 00494 } 00495 00496 00497 00498 00499 00500 00501 00502 00503 /********************************************************************************************* 00504 00505 > CCGmtTime::CCGmtTime() 00506 00507 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00508 Created: 21/08/96 00509 Purpose: Costructor for CCGmtTime.... 00510 00511 **********************************************************************************************/ 00512 00513 CCGmtTime::CCGmtTime() 00514 { 00515 // Force the time to be up-to-the-second 00516 GetCurrentTime(); 00517 } 00518 00519 00520 /********************************************************************************************* 00521 00522 > void CCGmtTime::GetCurrentTime() 00523 00524 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00525 Created: 21/08/96 00526 Purpose: Updates this CCGMTTime object with the date and time now. 00527 00528 **********************************************************************************************/ 00529 00530 void CCGmtTime::GetCurrentTime() 00531 { 00532 time_t osBinaryTime; 00533 time(&osBinaryTime); 00534 struct tm *p_tm; 00535 00536 cctime *p_time = GetTimeStruct(); 00537 #ifdef __WXMSW__ 00538 CTime t(osBinaryTime); 00539 p_tm = t.GetGmtTm(NULL); 00540 #else 00541 p_tm=gmtime( &osBinaryTime ); // Convert to local time. 00542 #endif 00543 00544 p_time->tm_sec = p_tm->tm_sec; 00545 p_time->tm_min = p_tm->tm_min; 00546 p_time->tm_hour = p_tm->tm_hour; 00547 p_time->tm_mday = p_tm->tm_mday; 00548 p_time->tm_mon = p_tm->tm_mon; 00549 p_time->tm_year = p_tm->tm_year; 00550 p_time->tm_wday = p_tm->tm_wday; 00551 p_time->tm_yday = p_tm->tm_yday; 00552 p_time->tm_isdst= p_tm->tm_isdst; 00553 } 00554 00555 00556 INT32 CCGmtTime::GetMonth() 00557 { 00558 return CCTimeBase::GetMonth() + 1; 00559 } 00560 00561 INT32 CCGmtTime::GetYear() 00562 { 00563 return CCTimeBase::GetYear() + 1900; 00564 } 00565 00566 00567 00568 00569 00570 00571 00572 00573 /* 00574 #include <time.h> 00575 #include <stdio.h> 00576 #include <sys/types.h> 00577 #include <sys/timeb.h> 00578 #include <string.h> 00579 00580 void main() 00581 { 00582 char tmpbuf[128], ampm[] = "AM"; 00583 time_t ltime; 00584 struct _timeb tstruct; 00585 struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 }; 00586 00587 // Set time zone from TZ environment variable. If TZ is not set, 00588 // operating system default is used, otherwise PST8PDT is used 00589 // (Pacific standard time, daylight savings). 00590 00591 _tzset(); 00592 00593 // Display operating system-style date and time. 00594 _strtime( tmpbuf ); 00595 printf( "OS time:\t\t\t\t%s\n", tmpbuf ); 00596 _strdate( tmpbuf ); 00597 printf( "OS date:\t\t\t\t%s\n", tmpbuf ); 00598 00599 // Get UNIX-style time and display as number and string. 00600 time( <ime ); 00601 printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime ); 00602 printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) ); 00603 00604 // Display UTC. 00605 gmt = gmtime( <ime ); 00606 printf( "Coordinated universal time:\t\t%s", asctime( gmt ) ); 00607 00608 // Convert to time structure and adjust for PM if necessary. 00609 today = localtime( <ime ); 00610 if( today->tm_hour > 12 ) 00611 { 00612 strcpy( ampm, "PM" ); 00613 today->tm_hour -= 12; 00614 } 00615 if( today->tm_hour == 0 ) // Adjust if midnight hour. 00616 today->tm_hour = 12; 00617 00618 // Note how pointer addition is used to skip the first 11 00619 // characters and printf is used to trim off terminating 00620 // characters. 00621 00622 printf( "12-hour time:\t\t\t\t%.8s %s\n", 00623 asctime( today ) + 11, ampm ); 00624 00625 // Print additional time information. 00626 _ftime( &tstruct ); 00627 printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm ); 00628 printf( "Zone difference in seconds from UTC:\t%u\n", 00629 tstruct.timezone ); 00630 printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); 00631 printf( "Daylight savings:\t\t\t%s\n", 00632 tstruct.dstflag ? "YES" : "NO" ); 00633 00634 // Make time for noon on Christmas, 1993. 00635 if( mktime( &xmas ) != (time_t)-1 ) 00636 printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) ); 00637 00638 // Use time structure to build a customized time string. 00639 today = localtime( <ime ); 00640 00641 // Use strftime to build a customized time string. 00642 strftime( tmpbuf, 128, 00643 "Today is %A, day %d of %B in the year %Y.\n", today ); 00644 printf( tmpbuf ); 00645 } 00646 00647 00648 Output 00649 00650 OS time: 21:51:03 00651 OS date: 05/03/94 00652 Time in seconds since UTC 1/1/70: 768027063 00653 UNIX time and date: Tue May 03 21:51:03 1994 00654 Coordinated universal time: Wed May 04 04:51:03 1994 00655 12-hour time: 09:51:03 PM 00656 Plus milliseconds: 279 00657 Zone difference in seconds from UTC: 480 00658 Time zone name: 00659 Daylight savings: YES 00660 Christmas Sat Dec 25 12:00:00 1993 00661 00662 Today is Tuesday, day 03 of May in the year 1994. 00663 */ 00664