FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/DateTime.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
DateTime.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
157 lines (119 loc) · 3.98 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
DateTime.cpp
Copy path
File metadata and controls
157 lines (119 loc) · 3.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* DateTime.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/DateTime.hpp
>
#
include
"
boost/date_time/c_local_time_adjustor.hpp
"
#
include
<
boost/date_time/local_time/local_time.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
date_time
{
double
secondsSinceEpoch
()
{
return
millisecondsSinceEpoch
() /
1000
;
}
double
secondsSinceEpoch
(
const
boost::posix_time::ptime& time)
{
return
millisecondsSinceEpoch
(time) /
1000
;
}
double
secondsSinceEpoch
(std::
time_t
time)
{
return
millisecondsSinceEpoch
(time) /
1000
;
}
double
millisecondsSinceEpoch
()
{
return
millisecondsSinceEpoch
(
boost::posix_time::microsec_clock::universal_time
());
}
double
millisecondsSinceEpoch
(
const
boost::posix_time::ptime& time)
{
using
namespace
boost
::gregorian
;
using
namespace
boost
::posix_time
;
ptime
time_t_epoch
(
date
(
1970
,
1
,
1
));
time_duration diff = time - time_t_epoch;
return
static_cast
<
double
>(diff.
total_milliseconds
());
}
double
millisecondsSinceEpoch
(std::
time_t
time)
{
return
std::difftime
(time,
0
) *
1000
;
}
boost::posix_time::ptime
timeFromSecondsSinceEpoch
(
double
sec)
{
using
namespace
boost
::gregorian
;
using
namespace
boost
::posix_time
;
ptime
time_t_epoch
(
date
(
1970
,
1
,
1
));
return
time_t_epoch +
seconds
(
static_cast
<
long
>(sec));
}
boost::posix_time::ptime
timeFromMillisecondsSinceEpoch
(
int64_t
ms)
{
using
namespace
boost
::gregorian
;
using
namespace
boost
::posix_time
;
ptime
time_t_epoch
(
date
(
1970
,
1
,
1
));
return
time_t_epoch +
milliseconds
(ms);
}
std::string
millisecondsSinceEpochAsString
(
double
ms)
{
boost::posix_time::ptime time =
date_time::timeFromMillisecondsSinceEpoch
(
static_cast
<
int64_t
>(ms));
return
date_time::format
(time,
"
%d %b %Y %H:%M:%S
"
);
}
bool
parseUtcTimeFromIsoString
(
const
std::string& timeStr,
boost::posix_time::ptime *pOutTime)
{
return
parseUtcTimeFromFormatString
(timeStr,
"
%Y-%m-%d %H:%M:%S %ZP
"
,
pOutTime);
}
const
std::string
kIso8601Format
{
"
%Y-%m-%dT%H:%M:%S%FZ
"
};
bool
parseUtcTimeFromIso8601String
(
const
std::string& timeStr,
boost::posix_time::ptime *pOutTime)
{
return
parseUtcTimeFromFormatString
(timeStr,
kIso8601Format
, pOutTime);
}
bool
parseUtcTimeFromFormatString
(
const
std::string& timeStr,
const
std::string& formatStr,
boost::posix_time::ptime *pOutTime)
{
using
namespace
boost
::local_time
;
std::stringstream
ss
(timeStr);
local_time_input_facet* ifc =
new
local_time_input_facet
(formatStr);
ss.
imbue
(
std::locale
(ss.
getloc
(), ifc));
local_date_time
ldt
(not_a_date_time);
if
(ss >> ldt)
{
*pOutTime = ldt.
utc_time
();
return
true
;
}
return
false
;
}
boost::posix_time::time_duration
getUtcOffset
()
{
using
namespace
boost
::posix_time
;
typedef
boost::date_time::c_local_adjustor<ptime> local_adj;
const
ptime utc_now =
second_clock::universal_time
();
const
ptime now =
local_adj::utc_to_local
(utc_now);
return
now - utc_now;
}
std::string
getUtcOffsetString
()
{
using
namespace
boost
::posix_time
;
std::stringstream out;
time_facet* tf =
new
time_facet
();
tf->
time_duration_format
(
"
%+%H:%M
"
);
out.
imbue
(
std::locale
(out.
getloc
(), tf));
out <<
getUtcOffset
();
return
out.
str
();
}
}
//
namespace date_time
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL