fmtlib 格式化 基本用法 11—— chrono基本语法

fmtlib提供了用于时间处理的格式化规范,包括日期、时间、时区等,如%Y-%m-%d%H:%M:%S。这些规范适用于std::tm和时间点,对std::chrono::duration有特定限制。例如,a和A代表缩写和全名的星期几,b和B代表缩写和全名的月份。转换修饰符如E和O可以改变输出格式。测试代码展示了如何使用这些规范来格式化时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fmtlib支持时间相关操作的基本语法:

Chrono Format Specifications:
    chrono_format_spec ::=  [[fill]align][width]["." precision][chrono_specs]
    chrono_specs       ::=  [chrono_specs] conversion_spec | chrono_specs literal_char
    conversion_spec    ::=  "%" [modifier] chrono_type
    literal_char       ::=  <a character other than '{', '}' or '%'>
    modifier           ::=  "E" | "O"
    chrono_type        ::=  "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "F" |
                            "g" | "G" | "h" | "H" | "I" | "j" | "m" | "M" | "n" | "p" |
                            "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | "U" | "V" |
                            "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | "%"
   
   精度仅对具有浮点表示类型的std::chrono::duration类型有效
   具有日历组件(如“d”(月的哪一天)的规范仅对std::tm和时间点有效,但对持续时间无效。

   chrono_type:
        Type        Meaning
        'a'     The abbreviated weekday name, e.g. “Sat”. If the value does not contain a valid weekday, an exception of type format_error is thrown.
        'A'     The full weekday name, e.g. “Saturday”. If the value does not contain a valid weekday, an exception of type format_error is thrown.
        'b'     The abbreviated month name, e.g. “Nov”. If the value does not contain a valid month, an exception of type format_error is thrown.
        'B'     The full month name, e.g. “November”. If the value does not contain a valid month, an exception of type format_error is thrown.
        'c'     The date and time representation, e.g. “Sat Nov 12 22:04:00 1955”. The modified command %Ec produces the locale’s alternate date and time representation.
        'C'     The year divided by 100 using floored division, e.g. “55”. If the result is a single decimal digit, it is prefixed with 0. The modified command %EC produces the locale’s alternative representation of the century.
        'd'     The day of month as a decimal number. If the result is a single decimal digit, it is prefixed with 0. The modified command %Od produces the locale’s alternative representation.
        'D'     Equivalent to %m/%d/%y, e.g. “11/12/55”.
        'e'     The day of month as a decimal number. If the result is a single decimal digit, it is prefixed with a space. The modified command %Oe produces the locale’s alternative representation.
        'F'     Equivalent to %Y-%m-%d, e.g. “1955-11-12”.
        'g'     The last two decimal digits of the ISO week-based year. If the result is a single digit it is prefixed by 0.
        'G'     The ISO week-based year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits.
        'h'     Equivalent to %b, e.g. “Nov”.
        'H'     The hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command %OH produces the locale’s alternative representation.
        'I'     The hour (12-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command %OI produces the locale’s alternative representation.
        'j'     If the type being formatted is a specialization of duration, the decimal number of days without padding. Otherwise, the day of the year as a decimal number. Jan 1 is 001. If the result is less than three digits, it is left-padded with 0 to three digits.
        'm'     The month as a decimal number. Jan is 01. If the result is a single digit, it is prefixed with 0. The modified command %Om produces the locale’s alternative representation.
        'M'     The minute as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command %OM produces the locale’s alternative representation.
        'n'     A new-line character.
        'p'     The AM/PM designations associated with a 12-hour clock.
        'q'     The duration’s unit suffix.
        'Q'     The duration’s numeric value (as if extracted via .count()).
        'r'     The 12-hour clock time, e.g. “10:04:00 PM”.
        'R'     Equivalent to %H:%M, e.g. “22:04”.
        'S'     Seconds as a decimal number. If the number of seconds is less than 10, the result is prefixed with 0. If the precision of the input cannot be exactly represented with seconds, then the format is a decimal floating-point number with a fixed format and a precision matching that of the precision of the input (or to a microseconds precision if the conversion to floating-point decimal seconds cannot be made within 18 fractional digits). The character for the decimal point is localized according to the locale. The modified command %OS produces the locale’s alternative representation.
        't'     A horizontal-tab character.
        'T'     Equivalent to %H:%M:%S.
        'u'     The ISO weekday as a decimal number (1-7), where Monday is 1. The modified command %Ou produces the locale’s alternative representation.
        'U'     The week number of the year as a decimal number. The first Sunday of the year is the first day of week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0. The modified command %OU produces the locale’s alternative representation.
        'V'     The ISO week-based week number as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command %OV produces the locale’s alternative representation.
        'w'     The weekday as a decimal number (0-6), where Sunday is 0. The modified command %Ow produces the locale’s alternative representation.
        'W'     The week number of the year as a decimal number. The first Monday of the year is the first day of week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0. The modified command %OW produces the locale’s alternative representation.
        'x'     The date representation, e.g. “11/12/55”. The modified command %Ex produces the locale’s alternate date representation.
        'X'     The time representation, e.g. “10:04:00”. The modified command %EX produces the locale’s alternate time representation.
        'y'     The last two decimal digits of the year. If the result is a single digit it is prefixed by 0. The modified command %Oy produces the locale’s alternative representation. The modified command %Ey produces the locale’s alternative representation of offset from %EC (year only).
        'Y'     The year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits. The modified command %EY produces the locale’s alternative full year representation.
        'z'     The offset from UTC in the ISO 8601:2004 format. For example -0430 refers to 4 hours 30 minutes behind UTC. If the offset is zero, +0000 is used. The modified commands %Ez and %Oz insert a : between the hours and minutes: -04:30. If the offset information is not available, an exception of type format_error is thrown.
        'Z'     The time zone abbreviation. If the time zone abbreviation is not available, an exception of type format_error is thrown.
        '%'     A % character.

测试代码:

int main(int argc, char** argv)
{
    auto t = tm();
    t.tm_year = 2010 - 1900;
    t.tm_mon = 7;
    t.tm_mday = 4;
    t.tm_hour = 12;
    t.tm_min = 15;
    t.tm_sec = 58;
    fmt::print("{:%Y-%m-%d %H:%M:%S}", t);
    // Prints: 2010-08-04 12:15:58


    return 0;
}

运行结果:

### Spring Framework ApplicationEventPublisher Example and Usage In the context of the Spring framework, `ApplicationEventPublisher` is an interface that allows beans to publish events to the application context. This mechanism facilitates a loosely coupled architecture where components can notify each other about significant occurrences without being directly dependent on one another. The core classes involved in this event-driven model include: - **ApplicationEvent**: A class extending from which all custom events should derive. - **ApplicationListener<E extends ApplicationEvent>**: An interface implemented by any bean wishing to listen for specific types of events. - **ApplicationEventMulticaster**: The component responsible for broadcasting events to registered listeners within the ApplicationContext[^1]. To demonstrate how these pieces work together using `ApplicationEventPublisher`, consider the following code snippets illustrating both publishing and listening capabilities. #### Publishing Events with ApplicationEventPublisher A service or repository layer might want to inform others when certain actions occur. For instance, after saving data into storage, it could broadcast such activity as shown below: ```java @Service public class MyService { private final ApplicationEventPublisher publisher; @Autowired public MyService(ApplicationEventPublisher publisher) { this.publisher = publisher; } void performAction() { // Action logic here... CustomEvent event = new CustomEvent(this); publisher.publishEvent(event); // Publishes the event through the context } } ``` Here, upon executing some action inside `performAction()`, a new `CustomEvent` gets created and published via injection of `ApplicationEventPublisher`. #### Listening for Specific Events Using ApplicationListener On the receiving end, interested parties implement `ApplicationListener<SpecificEventType>` to react accordingly whenever their targeted type occurs: ```java @Component public class EventConsumer implements ApplicationListener<MyCustomEvent> { @Override public void onApplicationEvent(MyCustomEvent event) { System.out.println("Received my custom event : " + event.getMessage()); } } ``` This listener will automatically receive notifications every time a matching event (`MyCustomEvent`) happens anywhere across different parts of your application[^2]. Additionally, annotations like `@EventListener` provide even more concise syntax while offering flexibility regarding method signatures and parameters used during handling processes. By leveraging these constructs effectively, developers gain powerful tools enabling robust communication patterns throughout complex systems built atop Spring's foundation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值