CLX C++ Libraries
Home >> ioftime

Declaration

template <
    class CharT,
    class Traits = std::char_traits<CharT>
>
class basic_iftime;

template <
    class CharT,
    class Traits = std::char_traits<CharT>
>
class basic_oftime;

typedef basic_iftime<char> iftime;
typedef basic_oftime<char> oftime;

Overview

与えられた書式にしたがって文字列を解析するクラス (iftime), および与えられた書式にしたがって時刻を出力するクラス (oftime) です. それぞれstrptime()strftime() 関数を用いて実現しています. 主な書式は以下の通りです.

%a
現在のロケールにおける曜日の省略名.
%A
現在のロケールにおける曜日の完全な名前.
%b
現在のロケールにおける月の省略名.
%B
現在のロケールにおける月の完全な名前.
%c
現在のロケールにおいて一般的な日付・時刻の表記.
%d
月内通算日(10 進数表記).
%e
%dと同様に月内通算日を 10 進数で表現するが,1 桁の場合 10 の位にゼロを置かずスペースを置く.
%F
%Y-%m-%d と等価.
%H
24 時間表記での時.
%I
12 時間表記での時.
%j
年の初めから通算の日数.
%k
24 時間表記での時.1 桁の場合には前にゼロでなくスペースが置かれる.
%l
12 時間表記での時.1 桁の場合には前にゼロでなくスペースが置かれる.
%m
月.
%M
分.
%p
現在のロケールにおける“午前”,“午後”に相当する文字列.
%S
秒.
%T
24 時間表記の時間(%H:%M:%S).
%u
週の何番目の日か.月曜日を 1 とする (1--7).
%U
年の初めからの通算の週数.その年の最初の日曜日を第 1 週の始まりとして計算する.
%w
週の何番目の日か.日曜日を 0 とする (0--6).
%W
年の初めからの通算の週数.その年の最初の月曜日を第 1 週の始まりとして計算する.
%x
現在のロケールで一般的な日付表記.時刻は含まない.
%X
現在のロケールで一般的な時刻表記.日付は含まない.
%y
西暦の下 2 桁.
%Y
世紀部分を含めた 4 桁の西暦年.

Example

// example_ioftime.cpp
#include <iostream>
#include <string>
#include "clx/ioftime.h"

int main(int argc, char* argv[]) {
    std::string str = "Sat 17 Mar 2007 19:56:19 JST";
    std::string infmt = "%a %d %b %Y %H:%M:%S JST";
    std::cout << "Input string: " << str << std::endl;
    std::cout << "Input format: " << infmt << std::endl;
    std::cout << std::endl;
    
    // 文字列strをinfmtにしたがって解析する.
    clx::iftime it;
    it.assign(str, infmt);
    
    /*
     * 時刻をoutfmtのフォーマットで出力する.
     * iftime::data()は,date_timeクラスの形で解析した時刻を返す.
     * oftimeには,C標準のstruct tm,time_tを引数に指定することも可能.
     */
    std::string outfmt = "%x %X";
    std::cout << "Output format: " << outfmt << std::endl;
    std::cout << "Result: " << clx::oftime(outfmt, it.data()) << std::endl;
    
    return 0;
}

Result
Input string: Sat 17 Mar 2007 19:56:19 JST
Input format: %a %d %b %Y %H:%M:%S JST

Output format: %x %X
Result: 03/17/07 19:56:19

Related Types

typedef CharT char_type;
typedef std::basic_string<CharT, Traits> string_type;
typedef date_time date_type;
typedef typename date_type::tm_type tm_type;

Constructions and Member Functions

basic_iftime

basic_iftime();
explicit basic_iftime(const char_type* s, const char_type* fmt);
explicit basic_iftime(const string_type& s, const string_type& fmt);
virtual ~basic_iftime();

const date_type& assign(const char_type* s, const char_type* fmt);
const date_type& assign(const string_type& s, const string_type& fmt);

const date_type& data() const;

basic_oftime

basic_oftime();
explicit basic_oftime(const char_type* fmt);
explicit basic_oftime(const string_type& fmt);

template <class Date>
explicit basic_oftime(const char_type* fmt, const Date& t);

template <class Date>
explicit basic_oftime(const string_type& fmt, const Date& t);

const string_type& assign(const char_type* fmt);
const string_type& assign(const string_type& fmt);

template <class Date>
const string_type& assign(const char_type* fmt, const Date& t);

template <class Date>
const string_type& assign(const string_type& fmt, const Date& t);

const string_type& str() const;

Operators

basic_oftime

const string_type& operator()(const char_type* fmt);
const string_type& operator()(const string_type& fmt);

template <class Date>
const string_type& operator()(const char_type* fmt, const Date& t);

template <class Date>
const string_type& operator()(const string_type& fmt, const Date& t);

template <class Ch, class Tr>
friend std::basic_ostream<Ch, Tr>& operator<<
    std::basic_ostream<Ch, Tr>& sout, const basic_oftime<Ch, Tr> f);

References

  1. Manpage of STRPTIME
  2. Manpage of STRFTIME