Merge branch 'master' of ssh://repo.or.cz/srv/git/ail
[ail.git] / source / time.cpp
blob869a0e53caae380387843fdb0548435a3d19fe76
1 #include <ail/time.hpp>
3 #include <ctime>
4 #include <sstream>
6 #include <ail/environment.hpp>
8 #ifdef AIL_WINDOWS
9 #include <ail/windows.hpp>
10 #else
11 #include <sys/time.h>
12 #include <sys/times.h>
13 #endif
16 #include <iostream>
18 namespace ail
20 ulong time()
22 return static_cast<ulong>(std::time(0));
25 ullong milliseconds()
27 #ifdef AIL_WINDOWS
28 ::SYSTEMTIME system_time;
29 ::GetLocalTime(&system_time);
30 ullong output = time() * 1000ull + system_time.wMilliseconds;
31 #else
32 ullong output = time() * 1000ull;
33 ::timeval time_value;
34 ::gettimeofday(&time_value, 0);
35 ::tm * time_data = ::localtime(&time_value.tv_sec);
36 output += static_cast<ullong>(time_value.tv_usec / 1000);
37 #endif
38 return output;
41 std::string timestamp()
43 std::time_t current_time;
44 std::time(&current_time);
45 std::tm * pointer = std::localtime(&current_time);
46 std::stringstream output;
47 output.fill('0');
48 output << (pointer->tm_year + 1900) << ".";
49 output.width(2);
50 output << (pointer->tm_mon + 1) << ".";
51 output.width(2);
52 output << pointer->tm_mday << " ";
53 output.width(2);
54 output << pointer->tm_hour << ":";
55 output.width(2);
56 output << pointer->tm_min << ":";
57 output.width(2);
58 output << pointer->tm_sec;
59 return output.str();
63 ullong boot_time()
65 #ifdef AIL_WINDOWS
66 #if (_WIN32_WINNT >= 0x0600)
67 return static_cast<ullong>(::GetTickCount64());
68 #else
69 return static_cast<ullong>(::GetTickCount());
70 #endif
71 #else
72 tms tm;
73 return static_cast<ullong>(::times(&tm));
74 #endif