1# Time Management<a name="EN-US_TOPIC_0000001123753363"></a> 2 3- [Basic Concepts](#section12903185785119) 4- [Development Guidelines](#section430981720522) 5 - [Available APIs](#section1040142705214) 6 - [How to Develop](#section1381224710522) 7 - [Development Example](#section1344610245416) 8 9 10## Basic Concepts<a name="section12903185785119"></a> 11 12Time management provides all time-related services for applications based on the system clock. The system clock is generated by the interrupts triggered by the output pulse of a timer or counter. The system clock is generally defined as an integer or a long integer. The period of an output pulse is a "clock tick". The system clock is also called time scale or tick. The duration of a tick can be configured statically. People use second or millisecond as the time unit, while the operating system uses tick. When operations such as suspending a task or delaying a task are performed, the time management module converts time between ticks and seconds or milliseconds. 13 14The mapping between ticks and seconds can be configured. 15 16- **Cycle** 17 18 Cycle is the minimum time unit in the system. The cycle duration is determined by the system clock frequency, that is, the number of cycles per second. 19 20 21- **Tick** 22 23 Tick is the basic time unit of the operating system and is determined by the number of ticks per second configured by the user. 24 25 26The OpenHarmony time management module provides time conversion, statistics, and delay functions to meet users' time requirements. 27 28## Development Guidelines<a name="section430981720522"></a> 29 30The time management module provides APIs to implement conversion between the system running time, ticks, and seconds/milliseconds. 31 32### Available APIs<a name="section1040142705214"></a> 33 34The following table describes APIs available for the OpenHarmony LiteOS-A time management. For more details about the APIs, see the API reference. 35 36**Table 1** APIs of the time management module 37 38<a name="table1316220185211"></a> 39<table><thead align="left"><tr id="row191622182021"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="p13162121815218"><a name="p13162121815218"></a><a name="p13162121815218"></a>Category</p> 40</th> 41<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="p12162618623"><a name="p12162618623"></a><a name="p12162618623"></a>API</p> 42</th> 43<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="p16162118427"><a name="p16162118427"></a><a name="p16162118427"></a>Description</p> 44</th> 45</tr> 46</thead> 47<tbody><tr id="row04981218910"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p6462616696"><a name="p6462616696"></a><a name="p6462616696"></a>Time conversion</p> 48</td> 49<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p164931214913"><a name="p164931214913"></a><a name="p164931214913"></a>LOS_MS2Tick</p> 50</td> 51<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p8504121996"><a name="p8504121996"></a><a name="p8504121996"></a>Converts milliseconds into ticks.</p> 52</td> 53</tr> 54<tr id="row7162101814216"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p816311185217"><a name="p816311185217"></a><a name="p816311185217"></a>LOS_Tick2MS</p> 55</td> 56<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p161632181721"><a name="p161632181721"></a><a name="p161632181721"></a>Converts ticks into milliseconds.</p> 57</td> 58</tr> 59<tr id="row1516317181227"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p1077619231696"><a name="p1077619231696"></a><a name="p1077619231696"></a>Time statistics</p> 60</td> 61<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p181638181921"><a name="p181638181921"></a><a name="p181638181921"></a>LOS_TickCountGet</p> 62</td> 63<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p615864811116"><a name="p615864811116"></a><a name="p615864811116"></a>Obtains the current number of ticks.</p> 64</td> 65</tr> 66<tr id="row101631818620"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p71633181125"><a name="p71633181125"></a><a name="p71633181125"></a>LOS_CyclePerTickGet</p> 67</td> 68<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p151631718124"><a name="p151631718124"></a><a name="p151631718124"></a>Obtains the number of cycles per tick.</p> 69</td> 70</tr> 71</tbody> 72</table> 73 74### How to Develop<a name="section1381224710522"></a> 75 761. Call APIs to convert time. 772. Call APIs to perform time statistics. 78 79> **NOTE:** 80>- The system tick count can be obtained only after the system clock is enabled. 81>- The time management module depends on **OS\_SYS\_CLOCK** and **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND** in **los\_config.h**. 82>- The number of system ticks is not counted when the interrupt feature is disabled. Therefore, the number of ticks cannot be used as the accurate time. 83 84### Development Example<a name="section1344610245416"></a> 85 86Prerequisites: 87 88- **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND**, that is, the number of ticks per second in the system is configured. 89- **OS\_SYS\_CLOCK**, that is, system clock \(in Hz\), is configured. 90 91**Sample Code** 92 93Time conversion: 94 95``` 96VOID Example_TransformTime(VOID) 97{ 98 UINT32 uwMs; 99 UINT32 uwTick; 100 uwTick = LOS_MS2Tick(10000);// Convert 10000 ms into ticks. 101 PRINTK("uwTick = %d \n",uwTick); 102 uwMs= LOS_Tick2MS(100); // Convert 100 ticks into ms. 103 PRINTK("uwMs = %d \n",uwMs); 104} 105``` 106 107Time statistics and delay: 108 109``` 110VOID Example_GetTime(VOID) 111{ 112 UINT32 uwcyclePerTick; 113 UINT64 uwTickCount; 114 115 uwcyclePerTick = LOS_CyclePerTickGet(); // Obtain the number of cycles per tick. 116 if(0 != uwcyclePerTick) 117 { 118 PRINTK("LOS_CyclePerTickGet = %d \n", uwcyclePerTick); 119 } 120 121 uwTickCount = LOS_TickCountGet(); // Obtain the number of ticks. 122 if(0 != uwTickCount) 123 { 124 PRINTK("LOS_TickCountGet = %d \n", (UINT32)uwTickCount); 125 } 126 LOS_TaskDelay(200);// Delay 200 ticks. 127 uwTickCount = LOS_TickCountGet(); 128 if(0 != uwTickCount) 129 { 130 PRINTK("LOS_TickCountGet after delay = %d \n", (UINT32)uwTickCount); 131 } 132} 133``` 134 135**Verification** 136 137The result is as follows: 138 139Time conversion: 140 141``` 142uwTick = 10000 143uwMs = 100 144``` 145 146Time statistics and delay: 147 148``` 149LOS_CyclePerTickGet = 49500 150LOS_TickCountGet = 5042 151LOS_TickCountGet after delay = 5242 152``` 153 154