Bypass Origin-State-Id for regexp xml content in wait steps
[anna.git] / source / time / functions.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Local
10 #include <anna/time/functions.hpp>
11 #include <anna/time/internal/Timezone.hpp>
12
13 // Standard
14 #include <stdlib.h> // getenv
15 #include <time.h>
16 #include <sys/time.h>
17 #include <string>
18
19 #include <anna/core/functions.hpp>
20
21
22 namespace anna {
23 namespace time {
24 Timezone SystemTimezone;
25 unsigned long long int SecondsReference;
26 }
27 }
28
29
30 void anna::time::functions::initialize() throw() {
31   static bool cached = false;
32   if(!cached) {
33     SystemTimezone.set(getenv("TZ"));
34     SecondsReference = 0;
35     cached = true;
36   }
37 }
38
39 void anna::time::functions::setControlPoint(unsigned long long int secondsTimestamp) throw() {
40   SecondsReference = secondsTimestamp ? secondsTimestamp : (::time(NULL));
41 }
42
43 unsigned long long int anna::time::functions::getSecondsReference() throw() {
44   return SecondsReference;
45 }
46
47 const anna::time::Timezone & anna::time::functions::getSystemTimezone(void) throw() {
48   return (SystemTimezone);
49 }
50
51 bool anna::time::functions::initialized(void) throw() {
52   return (SystemTimezone.isInitialized());
53 }
54
55 unsigned long long int anna::time::functions::unixSeconds(void) throw() {
56   return (::time(NULL));
57 }
58
59 unsigned long long int anna::time::functions::unixMilliseconds() throw() {
60   struct timeval tv;
61   gettimeofday(&tv, NULL);
62   return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
63 }
64
65 unsigned long long int anna::time::functions::lapsedMilliseconds() throw() {
66   struct timeval tv;
67   gettimeofday(&tv, NULL);
68   return ((tv.tv_sec - SecondsReference) * 1000) + (tv.tv_usec / 1000);
69 }
70
71 unsigned long long int anna::time::functions::unixMicroseconds(void) throw() {
72   struct timeval tv;
73   gettimeofday(&tv, NULL);
74   unsigned long long int result(tv.tv_sec);
75   result *= 1000000;
76   return result += tv.tv_usec;
77 }
78
79 std::string anna::time::functions::currentTimeAsString(void) throw() {
80   struct timeval tv;
81   gettimeofday(&tv, NULL);
82   struct tm *tm;
83   tm = localtime(&tv.tv_sec);
84   return (anna::functions::asString("%02d:%02d:%02d %d usecs", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec));
85 }
86