Botan 3.11.0
Crypto and TLS for C&
asn1_time.h
Go to the documentation of this file.
1/*
2* (C) 1999-2007,2018,2020,2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_ASN1_TIME_TYPE_H_
8#define BOTAN_ASN1_TIME_TYPE_H_
9
10#include <botan/asn1_obj.h>
11#include <chrono>
12
13namespace Botan {
14
15/**
16* Time (GeneralizedTime/UniversalTime)
17*/
18class BOTAN_PUBLIC_API(2, 0) ASN1_Time final : public ASN1_Object {
19 public:
20 /// DER encode a ASN1_Time
21 void encode_into(DER_Encoder& to) const override;
22
23 // Decode a BER encoded ASN1_Time
24 void decode_from(BER_Decoder& from) override;
25
26 /// Return an internal string representation of the time
27 std::string to_string() const;
28
29 /// Returns a human friendly string representation of no particular formatting
30 std::string readable_string() const;
31
32 /// Return if the time has been set somehow
33 bool time_is_set() const;
34
35 /// Compare this time against another
36 int32_t cmp(const ASN1_Time& other) const;
37
38 /// Create an invalid ASN1_Time
39 ASN1_Time() = default;
40
41 /// Create a ASN1_Time from a time point
42 explicit ASN1_Time(const std::chrono::system_clock::time_point& time);
43
44 /// Create an ASN1_Time from seconds since epoch
45 static ASN1_Time from_seconds_since_epoch(uint64_t seconds);
46
47 /// Create an ASN1_Time from string
48 BOTAN_FUTURE_EXPLICIT ASN1_Time(std::string_view t_spec);
49
50 /// Create an ASN1_Time from string and a specified tagging (Utc or Generalized)
51 ASN1_Time(std::string_view t_spec, ASN1_Type tag);
52
53 /// Returns a STL timepoint object
54 std::chrono::system_clock::time_point to_std_timepoint() const;
55
56 /// Return time since epoch
57 uint64_t time_since_epoch() const;
58
59 private:
60 void set_to(std::string_view t_spec, ASN1_Type type);
61 bool passes_sanity_check() const;
62
63 uint32_t m_year = 0;
64 uint32_t m_month = 0;
65 uint32_t m_day = 0;
66 uint32_t m_hour = 0;
67 uint32_t m_minute = 0;
68 uint32_t m_second = 0;
70};
71
72/*
73* Comparison Operations
74*/
75BOTAN_PUBLIC_API(2, 0) bool operator==(const ASN1_Time& x, const ASN1_Time& y);
76BOTAN_PUBLIC_API(2, 0) bool operator!=(const ASN1_Time& x, const ASN1_Time& y);
77BOTAN_PUBLIC_API(2, 0) bool operator<=(const ASN1_Time& x, const ASN1_Time& y);
78BOTAN_PUBLIC_API(2, 0) bool operator>=(const ASN1_Time& x, const ASN1_Time& y);
79BOTAN_PUBLIC_API(2, 0) bool operator<(const ASN1_Time& x, const ASN1_Time& y);
80BOTAN_PUBLIC_API(2, 0) bool operator>(const ASN1_Time& x, const ASN1_Time& y);
81
82} // namespace Botan
83
84#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
ASN1_Object()=default
uint64_t time_since_epoch() const
Return time since epoch.
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.
void decode_from(BER_Decoder &from) override
Definition asn1_time.cpp:59
static ASN1_Time from_seconds_since_epoch(uint64_t seconds)
Create an ASN1_Time from seconds since epoch.
Definition asn1_time.cpp:21
std::string to_string() const
Return an internal string representation of the time.
Definition asn1_time.cpp:65
ASN1_Time()=default
Create an invalid ASN1_Time.
bool time_is_set() const
Return if the time has been set somehow.
int32_t cmp(const ASN1_Time &other) const
Compare this time against another.
void encode_into(DER_Encoder &to) const override
DER encode a ASN1_Time.
Definition asn1_time.cpp:53
std::string readable_string() const
Returns a human friendly string representation of no particular formatting.
Definition asn1_time.cpp:98
ASN1_Type
Definition asn1_obj.h:43