]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_htimmstime.c
cc79e4237507acfa724a6e8a2de407e57f66b24a
[ulut.git] / ulut / ul_htimmstime.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_htimmstime.c   - standard hierarchical timer for microsecond
5                       time resolution
6
7   (C) Copyright 2003 by Pavel Pisa - Originator
8
9   The uLan utilities library can be used, copied and modified under
10   next licenses
11     - GPL - GNU General Public License
12     - LGPL - GNU Lesser General Public License
13     - MPL - Mozilla Public License
14     - and other licenses added by project originators
15   Code can be modified and re-distributed under any combination
16   of the above listed licenses. If contributor does not agree with
17   some of the licenses, he/she can delete appropriate line.
18   Warning, if you delete all lines, you are not allowed to
19   distribute source code and/or binaries utilizing code.
20   
21   See files COPYING and README for details.
22
23  *******************************************************************/
24
25 #include <string.h>
26 #include "ul_gavl.h"
27 #include "ul_gavlflesint.h"
28 #include "ul_htimer.h"
29
30 #ifdef UL_HTIMER_WITH_MSTIME
31
32 #include <time.h>
33 #include <stdio.h>
34
35 ul_htimer_queue_t ul_root_htimer;
36
37 ul_mstime_t ul_mstime_last;
38 ul_mstime_t ul_mstime_next;
39
40 static ul_mstime_t ul_mstime_base_offs;
41
42 #ifndef _WIN32
43
44 #include <sys/time.h>
45
46 void ul_mstime_now(ul_mstime_t *mstm)
47 {
48   struct timeval tv_actual;
49   ul_mstime_t ms_actual;
50   long int sec;
51   gettimeofday(&tv_actual,NULL);
52   sec=tv_actual.tv_sec;
53   ms_actual=sec*1000+tv_actual.tv_usec/1000+ul_mstime_base_offs;
54   *mstm=ms_actual;
55 }
56 #else /* _WIN32 */
57
58 #include <sys/timeb.h>
59
60 void ul_mstime_now(ul_mstime_t *mstm)
61 {
62   struct timeb tb_actual;
63   ul_mstime_t ms_actual;
64   long int sec;
65   ftime(&tb_actual);
66   sec=tb_actual.time;
67   ms_actual=sec*1000+tb_actual.millitm+ul_mstime_base_offs;
68   *mstm=ms_actual;
69 }
70 #endif /* _WIN32 */
71
72 void ul_mstime_update(void)
73 {
74     ul_mstime_now(&ul_mstime_last);
75 }
76
77 void ul_mstime_init(void)
78 {
79   ul_mstime_update();
80   ul_mstime_base_offs=-ul_mstime_last;
81   ul_mstime_update();
82 }
83
84 void
85 ul_get_log_time_str(char str[30])
86 {
87   time_t log_time;
88   struct tm *log_tm;
89   time(&log_time);
90   
91   log_tm=localtime(&log_time);
92   sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d",
93           (int)log_tm->tm_year+1900,(int)log_tm->tm_mon+1,
94           (int)log_tm->tm_mday,(int)log_tm->tm_hour,
95           (int)log_tm->tm_min,(int)log_tm->tm_sec);
96 }
97
98 void ul_compute_mstime_next(void)
99 {
100   if(!ul_htimer_next_expire(&ul_root_htimer,&ul_mstime_next))
101     ul_mstime_next=ul_mstime_last+0x10000000;
102 }
103   
104
105 #endif /*UL_HTIMER_WITH_MSTIME*/