1 /* @source ajtime *************************************************************
2 **
3 ** AJAX time functions
4 **
5 ** @author Copyright (C) 1998 Ian Longden
6 ** @author Copyright (C) 2003 Jon Ison
7 ** @version $Revision: 1.56 $
8 ** @modified 2004-2011 Peter Rice
9 ** @modified $Date: 2012/03/28 21:11:23 $ by $Author: mks $
10 ** @@
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License as published by the Free Software Foundation; either
15 ** version 2.1 of the License, or (at your option) any later version.
16 **
17 ** This library is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** Lesser General Public License for more details.
21 **
22 ** You should have received a copy of the GNU Lesser General Public
23 ** License along with this library; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 ** MA 02110-1301, USA.
26 **
27 ******************************************************************************/
28
29
30 #include "ajlib.h"
31
32 #include "ajtime.h"
33 #include "ajnam.h"
34
35 #include <time.h>
36 #include <limits.h>
37
38 static clock_t timeClockSave = 0;
39
40 ajlong timeClockOverflow = 0L;
41 ajlong timeClockMax = 0L;
42
43
44
45
46 /* @datastatic TimePFormat ****************************************************
47 **
48 ** Internal structure for known Ajax time formats
49 **
50 ** @alias TimeSFormat
51 ** @alias TimeOFormat
52 **
53 ** @attr Name [const char*] format name
54 ** @attr Format [const char*] C run time library time format string
55 ** @attr Uppercase [AjBool] Convert to upper case on output
56 ** @attr Padding [ajint] Padding to alignment boundary
57 ** @@
58 ******************************************************************************/
59
60 typedef struct TimeSFormat
61 {
62 const char* Name;
63 const char* Format;
64 AjBool Uppercase;
65 ajint Padding;
66 } TimeOFormat;
67
68 #define TimePFormat TimeOFormat*
69
70
71 static AjPTime timeTodayData = NULL;
72 static AjPTime timeTodaySaved = NULL;
73
74
75 static TimeOFormat timeFormat[] = /* formats for strftime */
76 {
77 {"GFF", "%Y-%m-%d", AJFALSE, 0},
78 {"yyyy-mm-dd", "%Y-%m-%d", AJFALSE, 0},
79 {"dd Mon yyyy", "%d %b %Y", AJFALSE, 0},
80 {"day", "%d-%b-%Y", AJFALSE, 0},
81 {"time", "%H:%M:%S", AJFALSE, 0},
82 {"daytime", "%d-%b-%Y %H:%M", AJFALSE, 0},
83 {"log", "%a %b %d %H:%M:%S %Y", AJFALSE, 0},
84 #ifndef WIN32
85 {"report", "%a %e %b %Y %H:%M:%S", AJFALSE, 0},
86 #else /* WIN32 */
87 {"report", "%a %#d %b %Y %H:%M:%S", AJFALSE, 0},
88 #endif /* !WIN32 */
89 {"dbindex", "%d/%m/%y", AJFALSE, 0},
90 {"dtline", "%d-%b-%Y", AJTRUE, 0},
91 {"cachefile", "%Y-%m-%d %H:%M:%S", AJTRUE, 0},
92 { NULL, NULL, AJFALSE, 0}
93 };
94
95
96 static const char* TimeFormat(const char *timefmt, AjBool* makeupper);
97
98
99
100
101 /* @filesection ajtime *******************************************************
102 **
103 ** @nam1rule aj Function belongs to the AJAX library.
104 **
105 */
106
107
108
109
110 /* @datasection [AjPTime] Time object ****************************************
111 **
112 ** @nam2rule Time Function is for handling time and usually processes an
113 ** AjSTime object.
114 */
115
116
117
118
119 /* @section constructors ******************************************************
120 **
121 ** Functions for constructing time objects.
122 **
123 ** @fdata [AjPTime]
124 **
125 ** @nam3rule New Construct a new time object.
126 ** @nam4rule NewDay Construct with specified day.
127 ** @nam5rule NewDayFmt Construct with specified day in specified format.
128 ** @nam4rule NewToday Construct with current time in default format.
129 ** @nam5rule NewTodayFmt Construct with current time in specified format.
130 ** @nam4rule NewTime Copy constructor
131 **
132 ** @argrule Fmt timefmt [const char*] Known EMBOSS time format
133 ** @argrule Day mday [ajint] Day of the month (1-31)
134 ** @argrule Day mon [ajint] Day of the month (1-12)
135 ** @argrule Day year [ajint] Year as a 4 digit number
136 ** @argrule NewTime src [const AjPTime] Time object to be copied
137 **
138 ** @valrule * [AjPTime] New time object.
139 ** @fcategory new
140 **
141 ******************************************************************************/
142
143
144
145
146 /* @func ajTimeNew ************************************************************
147 **
148 ** Constructor for AjPTime object.
149 **
150 ** @return [AjPTime] An AjPTime object
151 **
152 ** @release 2.8.0
153 ** @@
154 ******************************************************************************/
155
ajTimeNew(void)156 AjPTime ajTimeNew(void)
157 {
158 AjPTime thys = NULL;
159
160 AJNEW0(thys);
161
162 return thys ;
163 }
164
165
166
167
168 /* @func ajTimeNewDayFmt ******************************************************
169 **
170 ** Constructor for user specification of an arbitrary AjPTime object.
171 ** Except for 'timefmt', the arguments are based upon the UNIX
172 ** 'tm' time structure defined in the time.h header file.
173 ** The range validity of numbers given are not checked.
174 **
175 ** @param [rN] timefmt [const char*] Time format to use
176 ** @param [rN] mday [ajint] Day of the month [1-31]
177 ** @param [rN] mon [ajint] Month [1-12]
178 ** @param [rN] year [ajint] Four digit year
179 ** @return [AjPTime] An AjPTime object
180 **
181 ** @release 5.0.0
182 ** @@
183 ******************************************************************************/
184
ajTimeNewDayFmt(const char * timefmt,ajint mday,ajint mon,ajint year)185 AjPTime ajTimeNewDayFmt(const char *timefmt,
186 ajint mday, ajint mon, ajint year)
187 {
188 AjPTime thys;
189
190 thys = ajTimeNewTodayFmt(timefmt) ;
191
192 thys->time.tm_mday = mday ;
193 thys->time.tm_mon = mon-1;
194
195 if(year > 1899)
196 year = year-1900;
197
198 thys->time.tm_year = year ;
199
200 mktime(&thys->time);
201
202 return thys ;
203 }
204
205
206
207
208 /* @func ajTimeNewTime ********************************************************
209 **
210 ** Constructor for AjPTime object, making a copy of an existing time object
211 **
212 ** @param [r] src [const AjPTime] Time object to be copied
213 ** @return [AjPTime] An AjPTime object
214 **
215 ** @release 5.0.0
216 ** @@
217 ******************************************************************************/
218
ajTimeNewTime(const AjPTime src)219 AjPTime ajTimeNewTime(const AjPTime src)
220 {
221 AjPTime thys = NULL;
222
223 AJNEW0(thys);
224
225 thys->time = src->time;
226 thys->format = src->format;
227 thys->uppercase = src->uppercase;
228
229 return thys ;
230 }
231
232
233
234
235 /* @func ajTimeNewToday *******************************************************
236 **
237 ** AJAX function to return today's time as an AjPTime object
238 ** @return [AjPTime] Pointer to time object containing today's date/time
239 **
240 ** @release 5.0.0
241 ** @@
242 ******************************************************************************/
243
ajTimeNewToday(void)244 AjPTime ajTimeNewToday(void)
245 {
246 AjPTime thys = NULL;
247 time_t tim;
248
249 tim = time(0);
250
251 AJNEW0(thys);
252
253 if(!ajTimeSetLocal(thys, tim))
254 return NULL;
255
256 thys->format = NULL;
257
258 return thys;
259 }
260
261
262
263
264 /* @func ajTimeNewTodayFmt ****************************************************
265 **
266 ** AJAX function to return today's time as an AjPTime object
267 ** with a specified output format
268 **
269 ** @param [r] timefmt [const char*] A controlled vocabulary of time formats
270 **
271 ** @return [] [AjPTime] Pointer to time object containing today's date/time
272 **
273 ** @release 5.0.0
274 ** @@
275 **
276 ******************************************************************************/
277
ajTimeNewTodayFmt(const char * timefmt)278 AjPTime ajTimeNewTodayFmt(const char* timefmt)
279 {
280 AjPTime thys = NULL;
281 time_t tim;
282
283 tim = time(0);
284
285 if(!thys)
286 AJNEW0(thys);
287
288 if(!ajTimeSetLocal(thys, tim))
289 return NULL;
290
291 thys->format = TimeFormat(timefmt, &thys->uppercase);
292
293 return thys;
294 }
295
296
297
298
299 /* @section destructors *******************************************************
300 **
301 ** Functions for destroying time objects.
302 **
303 ** @fdata [AjPTime]
304 **
305 ** @nam3rule Del Destroy a time object.
306 **
307 ** @argrule * Ptime [AjPTime*] Time object to be deleted
308 **
309 ** @valrule * [void]
310 **
311 ** @fcategory delete
312 **
313 ******************************************************************************/
314
315
316
317
318 /* @func ajTimeDel ************************************************************
319 **
320 ** Destructor for AjPTime object.
321 **
322 ** @param [w] Ptime [AjPTime*] Time object pointer
323 **
324 ** @return [void]
325 **
326 ** @release 2.8.0
327 ** @@
328 ******************************************************************************/
329
ajTimeDel(AjPTime * Ptime)330 void ajTimeDel(AjPTime *Ptime)
331 {
332 /* Check arg's */
333 if(Ptime==NULL)
334 return;
335
336 if(*Ptime==NULL)
337 return;
338
339 AJFREE(*Ptime);
340 *Ptime = NULL;
341
342 return;
343 }
344
345
346
347
348 /* @section get time **********************************************************
349 **
350 ** Functions for retrieving elements of a time object
351 **
352 ** @fdata [AjPTime]
353 **
354 ** @nam3rule Get Return elements from a time object.
355 ** @nam4rule GetTimetype Return time as a standard C time_t structure
356 **
357 ** @argrule Get thys [const AjPTime] Time object
358 **
359 ** @valrule * [time_t] Standard C time_t structure
360 ** @fcategory use
361 **
362 ******************************************************************************/
363
364
365
366
367 /* @func ajTimeGetTimetype ****************************************************
368 **
369 ** An AjPTime object version of the mktime function that returns
370 ** a standard time_t value
371 **
372 ** @param [r] thys [const AjPTime] Time object
373 ** @return [time_t] Standard time value
374 **
375 ** @release 5.0.0
376 ** @@
377 ******************************************************************************/
378
ajTimeGetTimetype(const AjPTime thys)379 time_t ajTimeGetTimetype(const AjPTime thys)
380 {
381 struct tm tm = thys->time; /* mktime resets wday and yday */
382
383 return mktime(&tm);
384 }
385
386
387
388
389 /* @section get reference to internals ****************************************
390 **
391 ** Functions for retrieving references to time internals
392 **
393 ** @fdata [AjPTime]
394 **
395 ** @nam3rule Ref Reference time object.
396 ** @nam4rule RefToday Reference time object with current time in
397 ** default format.
398 ** @nam5rule RefTodayFmt Reference time in defined format.
399 **
400 ** @argrule Fmt timefmt [const char*] A controlled vocabulary of time formats
401 **
402 ** @valrule * [const AjPTime] Reference to Time object.
403 ** @fcategory misc
404 **
405 ******************************************************************************/
406
407
408
409
410 /* @func ajTimeRefToday *******************************************************
411 **
412 ** AJAX function to return today's time as an AjPTime object reference
413 ** @return [const AjPTime] Pointer to static time object containing
414 ** today's date/time
415 **
416 ** @release 5.0.0
417 ** @@
418 ******************************************************************************/
419
ajTimeRefToday(void)420 const AjPTime ajTimeRefToday(void)
421 {
422 time_t tim;
423
424 if(!timeTodaySaved)
425 {
426 AJNEW0(timeTodaySaved);
427
428 tim = time(0);
429
430 if(!ajTimeSetLocal(timeTodaySaved, tim))
431 return NULL;
432
433 timeTodaySaved->format = NULL;
434 }
435
436 return timeTodaySaved;
437 }
438
439
440
441
442 /* @func ajTimeRefTodayFmt ****************************************************
443 **
444 ** AJAX function to return today's time as a static AjPTime object
445 ** with a specified output format
446 **
447 ** @param [r] timefmt [const char*] A controlled vocabulary of time formats
448 **
449 ** @return [] [const AjPTime] Pointer to static time object containing
450 ** today's date/time
451 **
452 ** @release 5.0.0
453 ** @@
454 **
455 ******************************************************************************/
456
ajTimeRefTodayFmt(const char * timefmt)457 const AjPTime ajTimeRefTodayFmt(const char* timefmt)
458 {
459 time_t tim;
460
461 tim = time(0);
462
463 if(!timeTodayData)
464 AJNEW0(timeTodayData);
465
466 if(!ajTimeSetLocal(timeTodayData, tim))
467 return NULL;
468
469 timeTodayData->format = TimeFormat(timefmt, &timeTodayData->uppercase);
470
471 return timeTodayData;
472 }
473
474
475
476
477 /* @section set time **********************************************************
478 **
479 ** Functions for setting the time.
480 **
481 ** @fdata [AjPTime]
482 **
483 ** @nam3rule Set Set the time.
484 ** @nam4rule SetLocal A localtime()/localtime_r() replacement for AjPTime
485 ** objects
486 ** @suffix C Char* argument
487 ** @suffix S AjPStr argument
488 **
489 ** @argrule * thys [AjPTime] Time object to set.
490 ** @argrule Local timer [const time_t] Populated standard C time structure
491 ** @argrule C timestr [const char*] Time as a string
492 ** @argrule S timestr [const AjPStr] Time as a string
493 ** RefF
494 ** @valrule * [AjBool] True if time was set.
495 **
496 ** @fcategory modify
497 **
498 ******************************************************************************/
499
500
501
502
503 /* @func ajTimeSetC ***********************************************************
504 **
505 ** Constructor for user specification of an AjPTime object.
506 ** using the time set as a string in format
507 ** yyyy-mm-dd hh:mm:ss
508 **
509 ** used so that graphs which include the date can remain constant for
510 ** documentation and testing.
511 **
512 ** @param [w] thys [AjPTime] Time object
513 ** @param [r] timestr [const char*] Time in format yyyy-mm-dd hh:mm:ss
514 ** @return [AjBool] ajTrue on success
515 **
516 ** @release 5.0.0
517 ** @@
518 ******************************************************************************/
519
ajTimeSetC(AjPTime thys,const char * timestr)520 AjBool ajTimeSetC(AjPTime thys, const char* timestr)
521 {
522 ajint year = 0;
523 ajint mon = 0;
524 ajint mday = 0;
525 ajint hour = 0;
526 ajint min = 0;
527 ajint sec = 0;
528
529 if(!thys)
530 return ajFalse;
531
532 if(!timestr)
533 return ajFalse;
534
535 if(!ajFmtScanC(timestr, "%4d-%2d-%2d %2d:%2d:%2d",
536 &year, &mon, &mday, &hour, &min, &sec))
537 return ajFalse;
538
539 if(year > 1899)
540 year = year-1900;
541
542 thys->time.tm_year = year ;
543 thys->time.tm_mon = mon-1;
544 thys->time.tm_mday = mday ;
545 thys->time.tm_hour = hour;
546 thys->time.tm_min = min;
547 thys->time.tm_sec = sec;
548 thys->time.tm_isdst = -1;
549
550 mktime(&thys->time);
551
552 return ajTrue;
553 }
554
555
556
557
558 /* @func ajTimeSetS ***********************************************************
559 **
560 ** Constructor for user specification of an AjPTime object.
561 ** using the time set as a string in format
562 ** yyyy-mm-dd hh:mm:ss
563 **
564 ** used so that graphs which include the date can remain constant for
565 ** documentation and testing.
566 **
567 ** @param [w] thys [AjPTime] Time object
568 ** @param [r] timestr [const AjPStr] Time in format yyyy-mm-dd hh:mm:ss
569 ** @return [AjBool] ajTrue on success
570 **
571 ** @release 3.0.0
572 ** @@
573 ******************************************************************************/
574
ajTimeSetS(AjPTime thys,const AjPStr timestr)575 AjBool ajTimeSetS(AjPTime thys, const AjPStr timestr)
576 {
577 ajint year = 0;
578 ajint mon = 0;
579 ajint mday = 0;
580 ajint hour = 0;
581 ajint min = 0;
582 ajint sec = 0;
583
584 if(!thys)
585 return ajFalse;
586
587 if(!ajStrGetLen(timestr))
588 return ajFalse;
589
590 if(!ajFmtScanS(timestr, "%4d-%2d-%2d %2d:%2d:%2d",
591 &year, &mon, &mday, &hour, &min, &sec))
592 return ajFalse;
593
594 if(year > 1899)
595 year = year-1900;
596
597 thys->time.tm_year = year ;
598 thys->time.tm_mon = mon-1;
599 thys->time.tm_mday = mday ;
600 thys->time.tm_hour = hour;
601 thys->time.tm_min = min;
602 thys->time.tm_sec = sec;
603 thys->time.tm_isdst = -1;
604
605 mktime(&thys->time);
606
607 return ajTrue;
608 }
609
610
611
612
613 /* @func ajTimeSetLocal *******************************************************
614 **
615 ** A localtime()/localtime_r() replacement for AjPTime objects
616 **
617 ** @param [w] thys [AjPTime] Time object
618 ** @param [r] timer [const time_t] Populated standard C time structure
619 **
620 ** @return [AjBool] true if successful
621 **
622 ** @release 5.0.0
623 ** @@
624 ******************************************************************************/
625
ajTimeSetLocal(AjPTime thys,const time_t timer)626 AjBool ajTimeSetLocal(AjPTime thys, const time_t timer)
627 {
628 struct tm *result;
629 AjPStr timestr = NULL;
630
631 if(ajNamGetValueC("timetoday", ×tr))
632 {
633 if(ajTimeSetS(thys, timestr))
634 {
635 ajStrDel(×tr);
636
637 return ajTrue;
638 }
639
640 ajStrDel(×tr);
641 }
642
643 #if defined(__ppc__) || defined(WIN32)
644 result = localtime(&timer);
645 if(!result)
646 return ajFalse;
647
648 thys->time.tm_sec = result->tm_sec;
649 thys->time.tm_min = result->tm_min;
650 thys->time.tm_mday = result->tm_mday;
651 thys->time.tm_wday = result->tm_wday;
652 thys->time.tm_hour = result->tm_hour;
653 thys->time.tm_mon = result->tm_mon;
654 thys->time.tm_year = result->tm_year;
655 #else /* !(__ppc__ || WIN32) */
656 result = (struct tm *)localtime_r(&timer,&thys->time);
657
658 if(!result)
659 return ajFalse;
660 #endif /* __ppc__ || WIN32 */
661
662 return ajTrue;
663 }
664
665
666
667
668 /* @section comparison ********************************************************
669 **
670 ** Functions for comparing time objects.
671 **
672 ** @fdata [AjPTime]
673 **
674 ** @nam3rule Diff Return time difference.
675 **
676 ** @argrule * thys [const AjPTime] Time object.
677 ** @argrule * newtime [const AjPTime] Later time object.
678 **
679 ** @valrule Diff [double] Time difference in seconds
680 ** @fcategory use
681 **
682 ******************************************************************************/
683
684
685
686
687 /* @func ajTimeDiff ***********************************************************
688 **
689 ** Difference between two time objects
690 **
691 ** @param [r] thys [const AjPTime] Original time object
692 ** @param [r] newtime [const AjPTime] Later time object
693 ** @return [double] Difference in seconds
694 **
695 ** @release 6.0.0
696 ******************************************************************************/
697
ajTimeDiff(const AjPTime thys,const AjPTime newtime)698 double ajTimeDiff(const AjPTime thys, const AjPTime newtime)
699 {
700 double ret = 0.0;
701 struct tm oldt;
702 struct tm newt;
703 time_t oldtm;
704 time_t newtm;
705
706 oldt = thys->time;
707 newt = newtime->time;
708 oldtm = mktime(&oldt);
709 newtm = mktime(&newt);
710
711 ret = difftime(newtm,oldtm);
712
713 return ret;
714 }
715
716
717
718
719 /* @funcstatic TimeFormat *****************************************************
720 **
721 ** AJAX function to return the ANSI C format for an AJAX time string
722 **
723 ** @param [r] timefmt [const char*] AJAX time format
724 ** @param [w] makeupper [AjBool*] If true, convert time to upper case
725 ** @return [const char*] ANSI C time format, or NULL if none found
726 **
727 ** @release 1.0.0
728 ** @@
729 ******************************************************************************/
730
TimeFormat(const char * timefmt,AjBool * makeupper)731 static const char* TimeFormat(const char *timefmt, AjBool* makeupper)
732 {
733 ajint i;
734 AjBool ok = ajFalse;
735 const char *format = NULL ;
736
737 for(i=0; timeFormat[i].Name; i++)
738 if(ajCharMatchCaseC(timefmt, timeFormat[i].Name))
739 {
740 ok = ajTrue;
741 break;
742 }
743
744 if(ok)
745 {
746 format = timeFormat[i].Format;
747 *makeupper = timeFormat[i].Uppercase;
748 }
749 else
750 {
751 *makeupper = ajFalse;
752 ajWarn("Unknown date/time format %s", timefmt);
753 }
754
755 return format;
756 }
757
758
759
760
761 /* @section debug *************************************************************
762 **
763 ** Functions for debugging time objects.
764 **
765 ** @fdata [AjPTime]
766 **
767 ** @nam3rule Trace Write information on contents of AjPTime object to
768 ** debug file.
769 **
770 ** @argrule * thys [const AjPTime] Time object to debug.
771 **
772 ** @valrule * [void]
773 ** @fcategory use
774 **
775 ******************************************************************************/
776
777
778
779
780 /* @func ajTimeTrace **********************************************************
781 **
782 ** Debug report on the contents of an AjPTime object
783 **
784 ** @param [r] thys [const AjPTime] Time object
785 ** @return [void]
786 **
787 ** @release 1.0.0
788 ** @@
789 ******************************************************************************/
790
ajTimeTrace(const AjPTime thys)791 void ajTimeTrace(const AjPTime thys)
792 {
793 ajDebug("Time value trace '%D'\n", thys);
794 ajDebug("format: '%s'\n", thys->format);
795
796 return;
797 }
798
799
800
801
802 /* @section exit
803 **
804 ** Functions called on exit from the program by ajExit to do
805 ** any necessary cleanup and to report internal statistics to the debug file
806 **
807 ** @fdata [AjPTime]
808 ** @fnote general exit functions, no arguments
809 **
810 ** @nam3rule Exit Cleanup and report on exit
811 **
812 ** @valrule * [void]
813 **
814 ** @fcategory misc
815 */
816
817
818
819
820 /* @func ajTimeExit ***********************************************************
821 **
822 ** Cleans up time processing internal memory
823 **
824 ** @return [void]
825 **
826 ** @release 4.0.0
827 ** @@
828 ******************************************************************************/
829
ajTimeExit(void)830 void ajTimeExit(void)
831 {
832 ajTimeDel(&timeTodayData);
833 ajTimeDel(&timeTodaySaved);
834
835 return;
836 }
837
838
839
840
841 /* @datasection [none] time internals ***********************************
842 **
843 ** Function is for processing time internals.
844 **
845 ** @nam2rule Time Time processing
846 **
847 */
848
849
850
851
852 /* @section reset **************************************************************
853 **
854 ** Functions for memory cleanup
855 **
856 ** @fdata [none]
857 **
858 ** @nam3rule Reset Reset internals
859 **
860 ** @valrule * [void] No return value
861 **
862 ** @fcategory misc
863 **
864 ******************************************************************************/
865
866
867
868
869 /* @func ajTimeReset **********************************************************
870 **
871 ** Resets the clock time to zero
872 **
873 ** @return [void]
874 **
875 ** @release 6.1.0
876 ******************************************************************************/
877
ajTimeReset(void)878 void ajTimeReset(void)
879 {
880 time_t tim;
881
882 if(!timeTodaySaved)
883 AJNEW0(timeTodaySaved);
884
885 tim = time(0);
886
887 if(ajTimeSetLocal(timeTodaySaved, tim))
888 timeTodaySaved->format = NULL;
889
890 return;
891 }
892
893
894
895
896 /* @datasection [none] Cpu clock object ****************************************
897 **
898 ** @nam2rule Clock Function is for handling CPU clock time
899 **
900 ******************************************************************************/
901
902
903
904
905 /* @section get cpu time *******************************************************
906 **
907 ** Functions for retrieving CPU time
908 **
909 ** @fdata [none]
910 **
911 ** @nam3rule Diff Return time difference in seconds as a double
912 ** @nam3rule Now Return current cpu clock ticks as a long integer
913 ** @nam3rule Seconds Return current cpu clock time in seconds as a long integer
914 **
915 ** @argrule Diff starttime [ajlong] start time
916 ** @argrule Diff nowtime [ajlong] current time
917 ** @valrule Diff [double] CPU time in seconds
918 ** @valrule Now [ajlong] CPU ticks
919 ** @valrule Seconds [double] CPU time in seconds
920 **
921 ** @fcategory use
922 **
923 ******************************************************************************/
924
925
926
927
928 /* @func ajClockDiff **********************************************************
929 **
930 ** Returns the cpu time in seconds between two clock values
931 **
932 ** @param [r] starttime [ajlong] start time
933 ** @param [r] nowtime [ajlong] current time
934 ** @return [double] Total cpu clock time in seconds
935 **
936 **
937 ** @release 6.1.0
938 ******************************************************************************/
939
ajClockDiff(ajlong starttime,ajlong nowtime)940 double ajClockDiff(ajlong starttime, ajlong nowtime)
941 {
942 double x;
943
944 x = (double) (nowtime - starttime);
945
946 return x/(double)CLOCKS_PER_SEC;
947 }
948
949
950
951
952 /* @func ajClockNow ***********************************************************
953 **
954 ** Returns the clock time as a long even for systems where the clock_t type
955 ** is 4 bytes
956 **
957 ** @return [ajlong] Total clock ticks
958 **
959 ** @release 6.1.0
960 ******************************************************************************/
961
ajClockNow(void)962 ajlong ajClockNow(void)
963 {
964 clock_t now;
965
966 now = clock();
967
968 if(now < timeClockSave)
969 {
970 /* ajUser("ajClockNow overflow now:%Ld "
971 "timeClockSave:%Ld timeClockOverflow:%Ld",
972 (ajlong)now, (ajlong)timeClockSave, timeClockOverflow); */
973 if(!timeClockMax)
974 {
975 if(sizeof(now) == 4)
976 timeClockMax = UINT_MAX;
977 else
978 timeClockMax = UINT_MAX;
979 }
980
981
982 timeClockOverflow += timeClockMax;
983 /* ajUser("timeClockOverflow:%Ld timeClockMax:%Ld",
984 timeClockOverflow, timeClockMax); */
985 }
986
987 timeClockSave = now;
988
989 if(timeClockOverflow)
990 return (timeClockOverflow + now);
991
992 return now;
993 }
994
995
996
997
998 /* @func ajClockSeconds *******************************************************
999 **
1000 ** Returns the cpu time in seconds since the start
1001 **
1002 ** @return [double] Total cpu clock time in seconds
1003 **
1004 **
1005 ** @release 6.1.0
1006 ******************************************************************************/
1007
ajClockSeconds(void)1008 double ajClockSeconds(void)
1009 {
1010 double x;
1011 ajlong lv = ajClockNow();
1012
1013 x = (double) lv;
1014
1015 return x/(double)CLOCKS_PER_SEC;
1016 }
1017
1018
1019
1020
1021 /* @datasection [none] cpu time internals ***********************************
1022 **
1023 ** Function is for processing cpu time internals.
1024 **
1025 ** @nam2rule Clock Cpu time processing
1026 **
1027 */
1028
1029
1030
1031
1032 /* @section reset **************************************************************
1033 **
1034 ** Functions for CPU time memory cleanup
1035 **
1036 ** @fdata [none]
1037 **
1038 ** @nam3rule Reset Reset internals
1039 **
1040 ** @valrule * [void] No return value
1041 **
1042 ** @fcategory misc
1043 **
1044 ******************************************************************************/
1045
1046
1047
1048
1049 /* @func ajClockReset *********************************************************
1050 **
1051 ** Resets the clock time to zero
1052 **
1053 ** @return [void]
1054 **
1055 ** @release 6.1.0
1056 ******************************************************************************/
1057
ajClockReset(void)1058 void ajClockReset(void)
1059 {
1060 timeClockSave = clock();
1061 timeClockOverflow = 0;
1062
1063 return;
1064 }
1065
1066
1067
1068
1069 #ifdef AJ_COMPILE_DEPRECATED_BOOK
1070 #endif /* AJ_COMPILE_DEPRECATED_BOOK */
1071
1072
1073
1074
1075 #ifdef AJ_COMPILE_DEPRECATED
1076 /* @obsolete ajTimeSet
1077 ** @rename ajTimeNewDayFmt
1078 */
1079
ajTimeSet(const char * timefmt,ajint mday,ajint mon,ajint year)1080 __deprecated AjPTime ajTimeSet(const char *timefmt,
1081 ajint mday, ajint mon, ajint year)
1082 {
1083 return ajTimeNewDayFmt(timefmt, mday, mon, year);
1084 }
1085
1086
1087
1088
1089 /* @obsolete ajTimeToday
1090 ** @rename ajTimeNewToday
1091 */
1092
ajTimeToday(void)1093 __deprecated AjPTime ajTimeToday(void)
1094 {
1095 return ajTimeNewToday();
1096 }
1097
1098
1099
1100
1101 /* @obsolete ajTimeTodayF
1102 ** @rename ajTimeNewTodayFmt
1103 */
1104
ajTimeTodayF(const char * timefmt)1105 __deprecated AjPTime ajTimeTodayF(const char* timefmt)
1106 {
1107 return ajTimeNewTodayFmt(timefmt);
1108 }
1109
1110
1111
1112
1113 /* @obsolete ajTimeMake
1114 ** @rename ajTimeGetTimetype
1115 */
ajTimeMake(const AjPTime thys)1116 __deprecated time_t ajTimeMake(const AjPTime thys)
1117 {
1118 return ajTimeGetTimetype(thys);
1119 }
1120
1121
1122
1123
1124 /* @obsolete ajTimeTodayRef
1125 ** @rename ajTimeRefToday
1126 */
1127
ajTimeTodayRef(void)1128 __deprecated const AjPTime ajTimeTodayRef(void)
1129 {
1130 return ajTimeRefToday();
1131 }
1132
1133
1134
1135
1136 /* @obsolete ajTimeTodayRefF
1137 ** @rename ajTimeRefTodayFmt
1138 */
1139
ajTimeTodayRefF(const char * timefmt)1140 __deprecated const AjPTime ajTimeTodayRefF(const char* timefmt)
1141 {
1142 return ajTimeRefTodayFmt(timefmt);
1143 }
1144
1145
1146
1147
1148 /* @obsolete ajTimeLocal
1149 ** @replace ajTimeSetLocal (1,2/2,1)
1150 */
1151
ajTimeLocal(const time_t timer,AjPTime thys)1152 __deprecated AjBool ajTimeLocal(const time_t timer, AjPTime thys)
1153 {
1154 return ajTimeSetLocal(thys, timer);
1155 }
1156
1157 #endif /* AJ_COMPILE_DEPRECATED */
1158