1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 #include <anna/time/functions.hpp>
11 #include <anna/time/internal/Timezone.hpp>
14 #include <stdlib.h> // getenv
19 #include <anna/core/functions.hpp>
24 Timezone SystemTimezone;
25 unsigned long long int SecondsReference;
30 void anna::time::functions::initialize() throw() {
31 static bool cached = false;
33 SystemTimezone.set(getenv("TZ"));
39 void anna::time::functions::setControlPoint(unsigned long long int secondsTimestamp) throw() {
40 SecondsReference = secondsTimestamp ? secondsTimestamp : (::time(NULL));
43 unsigned long long int anna::time::functions::getSecondsReference() throw() {
44 return SecondsReference;
47 const anna::time::Timezone & anna::time::functions::getSystemTimezone(void) throw() {
48 return (SystemTimezone);
51 bool anna::time::functions::initialized(void) throw() {
52 return (SystemTimezone.isInitialized());
55 unsigned long long int anna::time::functions::unixSeconds(void) throw() {
56 return (::time(NULL));
59 unsigned long long int anna::time::functions::unixMilliseconds() throw() {
61 gettimeofday(&tv, NULL);
62 return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
65 unsigned long long int anna::time::functions::lapsedMilliseconds() throw() {
67 gettimeofday(&tv, NULL);
68 return ((tv.tv_sec - SecondsReference) * 1000) + (tv.tv_usec / 1000);
71 unsigned long long int anna::time::functions::unixMicroseconds(void) throw() {
73 gettimeofday(&tv, NULL);
74 unsigned long long int result(tv.tv_sec);
76 return result += tv.tv_usec;
79 std::string anna::time::functions::currentTimeAsString(void) throw() {
81 gettimeofday(&tv, NULL);
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));