MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / duration_example1

Function duration_example1

examples/src/bson_examples.cpp:175–200  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

173}
174
175void duration_example1()
176{
177 auto duration = std::chrono::system_clock::now().time_since_epoch();
178 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
179
180 json j;
181 j.try_emplace("time", time);
182
183 auto milliseconds = j["time"].as<std::chrono::milliseconds>();
184 std::cout << "Time since epoch (milliseconds): " << milliseconds.count() << "\n\n";
185 auto seconds = j["time"].as<std::chrono::seconds>();
186 std::cout << "Time since epoch (seconds): " << seconds.count() << "\n\n";
187
188 std::vector<uint8_t> data;
189 bson::encode_bson(j, data);
190
191 std::cout << "BSON bytes:\n" << jsoncons::byte_string_view(data) << "\n\n";
192
193/*
194 13,00,00,00, // document has 19 bytes
195 09, // UTC datetime
196 74,69,6d,65,00, // "time"
197 ea,14,7f,96,73,01,00,00, // 1595957777642
198 00 // terminating null
199*/
200}
201
202void binary_example1()
203{

Callers 1

mainFunction · 0.70

Calls 4

encode_bsonFunction · 0.85
byte_string_viewClass · 0.85
try_emplaceMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected