日付や時刻を取り扱う Poco::DateTime 、タイムゾーンを扱う Poco::Timezone、さらに日付や時刻の書式を扱う Poco::DateTimeFormatter の3つを紹介します。
DateTimeTest.cpp
・まず Poco::DateTime をインスタンス化して内容を表示した後、ローカル時刻に変換して再度内容を
表示し、最後に Poco::DateTimeFormatter::format で、Linux の date コマンド相当の出力フォーマット
に整形。
・タイムゾーンと GMT からのオフセットを入手するのに Poco::Timezone を利用。
#include <Poco/DateTime.h> #include <Poco/Timezone.h> #include <Poco/Format.h> #include <Poco/DateTimeFormatter.h> #include <string> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" void DisplayDateTime(const Poco::DateTime& dateTime, ScopedLogMessage& msg) { msg.Message(Poco::format(" year = %d", dateTime.year())); msg.Message(Poco::format(" month = %d\t(1 to 12)", dateTime.month())); msg.Message(Poco::format(" day = %d\t(1 to 31)", dateTime.day())); msg.Message(Poco::format(" hour = %d\t(0 to 23)", dateTime.hour())); msg.Message(Poco::format(" minute = %d\t(0 to 59)", dateTime.minute())); msg.Message(Poco::format(" second = %d\t(0 to 59)", dateTime.second())); msg.Message(Poco::format(" millisecond = %d\t(0 to 999)", dateTime.millisecond())); msg.Message(Poco::format(" microsecond = %d\t(0 to 999)", dateTime.microsecond())); msg.Message(Poco::format(" isAM = %s\t(true or false)", std::string(dateTime.isAM() ? "true":"false"))); msg.Message(Poco::format(" isPM = %s\t(true or false)", std::string(dateTime.isPM() ? "true":"false"))); msg.Message(Poco::format(" isLeapYear = %s\t(true or false)", std::string(Poco::DateTime::isLeapYear(dateTime.year()) ? "true":"false"))); msg.Message(Poco::format(" hourAMPM = %d\t(0 to 12)", dateTime.hourAMPM())); msg.Message(Poco::format(" dayOfWeek = %d\t(0 to 6, 0: Sunday)", dateTime.dayOfWeek())); msg.Message(Poco::format(" dayOfYear = %d\t(1 to 366, 1: January 1)", dateTime.dayOfYear())); msg.Message(Poco::format(" daysOfMonth = %d\t(1 to 366, 1: January 1)", Poco::DateTime::daysOfMonth(dateTime.year(), dateTime.month()))); msg.Message(Poco::format(" week = %d\t(0 to 53, 1: the week containing January 4)", dateTime.week())); msg.Message(""); } int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("DateTimeTest ", "start", "end"); Poco::DateTime dateTime; msg.Message(" Current DateTime (UTC)"); DisplayDateTime(dateTime, msg); msg.Message(Poco::format(" Current DateTime (Locat Time: %s [GMT%+d])", Poco::Timezone::name(), Poco::Timezone::tzd()/(60*60))); dateTime.makeLocal(Poco::Timezone::tzd()); DisplayDateTime(dateTime, msg); msg.Message(Poco::format(Poco::DateTimeFormatter::format(dateTime, " DateTimeFormatter: %w %b %e %H:%M:%S %%s %Y") , Poco::Timezone::name())); return 0; } |
Results of execution
・On Linux
[0] DateTimeTest start [0] Current DateTime (UTC) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 14 (1 to 31) [0] hour = 20 (0 to 23) [0] minute = 3 (0 to 59) [0] second = 51 (0 to 59) [0] millisecond = 834 (0 to 999) [0] microsecond = 438 (0 to 999) [0] isAM = false (true or false) [0] isPM = true (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 8 (0 to 12) [0] dayOfWeek = 5 (0 to 6, 0: Sunday) [0] dayOfYear = 134 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] Current DateTime (Locat Time: JST [GMT+9]) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 15 (1 to 31) [0] hour = 5 (0 to 23) [0] minute = 3 (0 to 59) [0] second = 51 (0 to 59) [0] millisecond = 834 (0 to 999) [0] microsecond = 438 (0 to 999) [0] isAM = true (true or false) [0] isPM = false (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 5 (0 to 12) [0] dayOfWeek = 6 (0 to 6, 0: Sunday) [0] dayOfYear = 135 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] DateTimeFormatter: Sat May 15 05:03:51 JST 2010 [0] DateTimeTest end |
・On Macintosh OSX 10.6.3
[0] DateTimeTest start [0] Current DateTime (UTC) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 14 (1 to 31) [0] hour = 19 (0 to 23) [0] minute = 24 (0 to 59) [0] second = 40 (0 to 59) [0] millisecond = 231 (0 to 999) [0] microsecond = 910 (0 to 999) [0] isAM = false (true or false) [0] isPM = true (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 7 (0 to 12) [0] dayOfWeek = 5 (0 to 6, 0: Sunday) [0] dayOfYear = 134 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] Current DateTime (Locat Time: JST [GMT+9]) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 15 (1 to 31) [0] hour = 4 (0 to 23) [0] minute = 24 (0 to 59) [0] second = 40 (0 to 59) [0] millisecond = 231 (0 to 999) [0] microsecond = 910 (0 to 999) [0] isAM = true (true or false) [0] isPM = false (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 4 (0 to 12) [0] dayOfWeek = 6 (0 to 6, 0: Sunday) [0] dayOfYear = 135 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] DateTimeFormatter: Sat May 15 04:24:40 JST 2010 [0] DateTimeTest end |
・On Windows XP sp3
[0] DateTimeTest start [0] Current DateTime (UTC) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 14 (1 to 31) [0] hour = 19 (0 to 23) [0] minute = 43 (0 to 59) [0] second = 23 (0 to 59) [0] millisecond = 127 (0 to 999) [0] microsecond = 500 (0 to 999) [0] isAM = false (true or false) [0] isPM = true (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 7 (0 to 12) [0] dayOfWeek = 5 (0 to 6, 0: Sunday) [0] dayOfYear = 134 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] Current DateTime (Locat Time: 東京 (標準時) [GMT+9]) [0] year = 2010 [0] month = 5 (1 to 12) [0] day = 15 (1 to 31) [0] hour = 4 (0 to 23) [0] minute = 43 (0 to 59) [0] second = 23 (0 to 59) [0] millisecond = 127 (0 to 999) [0] microsecond = 500 (0 to 999) [0] isAM = true (true or false) [0] isPM = false (true or false) [0] isLeapYear = false (true or false) [0] hourAMPM = 4 (0 to 12) [0] dayOfWeek = 6 (0 to 6, 0: Sunday) [0] dayOfYear = 135 (1 to 366, 1: January 1) [0] daysOfMonth = 31 (1 to 366, 1: January 1) [0] week = 19 (0 to 53, 1: the week containing January 4) [0] [0] DateTimeFormatter: Sat May 15 04:43:23 東京 (標準時) 2010 [0] DateTimeTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年5月15日からのダウンロード数:1175
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・http://pocoproject.org にある DateAndTime のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
0 Comments.