Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SoftSensorPlugin / IndoorTrajectorySensor / src / SysTimer.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <cstdlib>
22 #include <sys/time.h>
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26
27 #include "SysTimer.h"
28
29 #include <stdint.h>
30 #include <stdio.h>
31
32 std::string SysTimer::MilliSecondTime()
33 {
34     std::stringstream ss;
35     struct timeval tv;
36     time_t              *p_time = NULL;
37     struct tm           *localT = NULL;
38     char                time_str[30] = {0,};
39
40     gettimeofday(&tv, NULL);
41     p_time = (time_t *) & (tv.tv_sec);
42     localT = localtime( p_time );
43
44     sprintf( time_str, "%04d-%02d-%02d %02d:%02d:%02d.%03ld",       \
45              localT->tm_year + 1900, localT->tm_mon + 1, localT->tm_mday,    \
46              localT->tm_hour,      localT->tm_min,   localT->tm_sec,     \
47              tv.tv_usec / 1000 );
48
49     ss << time_str;
50     std::string strTime = ss.str();
51
52     return strTime;
53 }
54
55 std::string SysTimer::MilliSecondAsString()
56 {
57     std::stringstream ss;
58     struct timeval tv;
59
60     gettimeofday(&tv, NULL);
61     long long val = tv.tv_sec * (long long) 1000 + tv.tv_usec / 1000;
62
63     ss << val;
64     std::string strTime = ss.str();
65
66     return strTime;
67 }
68
69 std::string SysTimer::UTCSecondAsString()
70 {
71     std::stringstream ss;
72     struct timeval tv;
73
74     gettimeofday(&tv, NULL);
75     unsigned long val = tv.tv_sec;
76
77     ss << val;
78     std::string strTime = ss.str();
79
80     return strTime;
81 }