Safir SDK Core
Loading...
Searching...
No Matches
TimeProvider.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2006-2013 (http://safirsdkcore.com)
4*
5* Created by: Erik Adolfsson / sterad
6*
7*******************************************************************************
8*
9* This file is part of Safir SDK Core.
10*
11* Safir SDK Core is free software: you can redistribute it and/or modify
12* it under the terms of version 3 of the GNU General Public License as
13* published by the Free Software Foundation.
14*
15* Safir SDK Core is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22*
23******************************************************************************/
24#pragma once
25
26#include <Safir/Utilities/Internal/VisibilityHelpers.h>
27
28#ifdef douf_time_cpp_EXPORTS
29# define DOUF_TIME_CPP_API SAFIR_HELPER_DLL_EXPORT
30#else
31# define DOUF_TIME_CPP_API SAFIR_HELPER_DLL_IMPORT
32# define SAFIR_LIBRARY_NAME "douf_time_cpp"
33# include <Safir/Utilities/Internal/AutoLink.h>
34#endif
35#define DOUF_TIME_CPP_LOCAL SAFIR_HELPER_DLL_LOCAL
36
38
39//disable warnings in boost
40#if defined _MSC_VER
41 #pragma warning (push)
42 #pragma warning (disable : 4127)
43#endif
44
45#include <boost/date_time/posix_time/posix_time.hpp>
46#include <boost/date_time/gregorian/greg_date.hpp>
47#include <boost/date_time/time_duration.hpp>
48
49//and enable the warnings again
50#if defined _MSC_VER
51 #pragma warning (pop)
52#endif
53
54namespace Safir
55{
56namespace Time
57{
62 {
63 public:
64
71
78 static boost::posix_time::ptime ToLocalTime(const Safir::Dob::Typesystem::Si64::Second utcTime)
79 {
80 return ToPtime(utcTime + GetLocalTimeOffset());
81 }
82
83
90 static Safir::Dob::Typesystem::Si64::Second ToUtcTime(const boost::posix_time::ptime & localTime)
91 {
92 boost::posix_time::time_duration duration = localTime - Epoch();
93 Dob::Typesystem::Int64 seconds = duration.total_seconds();
94 Dob::Typesystem::Float64 fraction = duration.fractional_seconds() / pow(10.0,duration.num_fractional_digits());
95
96 return seconds + fraction - GetLocalTimeOffset();
97 }
98
105 static boost::posix_time::ptime ToPtime(const Safir::Dob::Typesystem::Si64::Second utcTime)
106 {
109
110 sec = (Dob::Typesystem::Int64)utcTime;
111 fraction = utcTime - sec;
112
113 long long hour = sec/3600;
114 long long minutes = (sec - hour*3600)/60;
115 long long seconds = sec - hour*3600 - minutes*60;
116
117 // Return the duration since 1970-Jan-01
118 boost::posix_time::time_duration duration((long)hour, (long)minutes, (long)seconds,
119 Dob::Typesystem::Int64(fraction * pow(10.0,boost::posix_time::time_duration::num_fractional_digits()) + 0.5));
120
121 return Epoch() + duration;
122 }
123
130 static Safir::Dob::Typesystem::Si64::Second ToDouble(const boost::posix_time::ptime & utcTime)
131 {
132 boost::posix_time::time_duration d = utcTime - Epoch();
133 return(double)d.ticks() / d.ticks_per_second();
134 }
135
136 private:
143 static inline boost::posix_time::ptime Epoch()
144 {
145 return boost::posix_time::ptime(boost::gregorian::date(1970,boost::date_time::Jan,1));
146 }
147
148 static DOUF_TIME_CPP_API Safir::Dob::Typesystem::Int32 GetLocalTimeOffset();
149 };
150
151} // namespace Time
152} // namespace Safir
153
154
#define DOUF_TIME_CPP_API
Definition TimeProvider.h:31
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
DotsC_Int32 Int32
32 bit integer type.
Definition Defs.h:66
DotsC_Int64 Int64
64 bit integer type.
Definition Defs.h:69
DotsC_Float64 Float64
64 bit floating point type.
Definition Defs.h:75
Float64 Second
64 bit representation of Second.
Definition Defs.h:197
The Time class contains functions to operate on time.
Definition TimeProvider.h:62
static boost::posix_time::ptime ToPtime(const Safir::Dob::Typesystem::Si64::Second utcTime)
Get specified UTC time in boost::posix_time::ptime representation.
Definition TimeProvider.h:105
static Safir::Dob::Typesystem::Si64::Second ToUtcTime(const boost::posix_time::ptime &localTime)
Convert local time to UTC time.
Definition TimeProvider.h:90
static boost::posix_time::ptime ToLocalTime(const Safir::Dob::Typesystem::Si64::Second utcTime)
Convert from UTC to local time.
Definition TimeProvider.h:78
static Safir::Dob::Typesystem::Si64::Second ToDouble(const boost::posix_time::ptime &utcTime)
Convert specified UTC time to a Double.
Definition TimeProvider.h:130
static SAFIR_HELPER_DLL_IMPORT Safir::Dob::Typesystem::Si64::Second GetUtcTime()
Get current UTC time.