mastodon-cpp  0.105.1
Public Member Functions | Public Attributes | Friends | List of all members
Mastodon::Easy::time Struct Reference

Type for time. Converts to time_point and string. More...

#include <types_easy.hpp>

Public Member Functions

 operator const system_clock::time_point () const
 
 operator const string () const
 Returns local time as string in ISO 8601 format (FTTz). More...
 
const string strtime (const string &format, const bool &local=true) const
 Converts time to a string. More...
 

Public Attributes

system_clock::time_point timepoint = system_clock::time_point()
 

Friends

std::ostream & operator<< (std::ostream &out, const Easy::time &t)
 Returns local time as string in ISO 8601 format (FTTz). More...
 

Detailed Description

Type for time. Converts to time_point and string.

Since
0.100.0

Member Function Documentation

◆ operator const string()

Mastodon::Easy::time::operator const string ( ) const

Returns local time as string in ISO 8601 format (FTTz).

Since
0.100.0
29  {
30  return strtime("%FT%T%z", true);
31  }
const string strtime(const string &format, const bool &local=true) const
Converts time to a string.
Definition: types_easy.cpp:33

◆ strtime()

const string Mastodon::Easy::time::strtime ( const string &  format,
const bool &  local = true 
) const

Converts time to a string.

The return value can not exceed 1023 chars.

Parameters
formatThe format of the string, same as with strftime.
localUse local time (default) or UTC.

Example:

Mastodon::Easy::time timepoint = status.created_at();
std::cout << timepoint.strtime("%F, %T UTC", false) << '\n';
Returns
The time as string.
Since
0.100.0
Examples:
example02_stream.cpp.
34  {
35  constexpr std::uint16_t bufsize = 1024;
36  std::time_t time = system_clock::to_time_t(timepoint);
37  std::tm *tm;
38  if (local)
39  {
40  tm = std::localtime(&time);
41  }
42  else
43  {
44  tm = std::gmtime(&time);
45  }
46 
47  char buffer[bufsize];
48  std::strftime(buffer, bufsize, format.c_str(), tm);
49 
50  return static_cast<const string>(buffer);
51  }

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const Easy::time t 
)
friend

Returns local time as string in ISO 8601 format (FTTz).

Since
0.100.0
55  {
56  const string s = t; // Converts using operator const string().
57  out << s;
58  return out;
59  }

The documentation for this struct was generated from the following files: