国外优秀网站,天津房地产集团网站建设,战略咨询,网站空间托管【C】郭老二博文之#xff1a;C目录
1、Poco::Random 随机数
1.1 说明
POCO包括一个伪随机数生成器(PRNG)#xff0c;使用非线性加性反馈算法#xff0c;具有256位状态信息和长达269的周期。 PRNG可以生成31位的伪随机数。
它可以生成UInt32, char, bool, float和double…【C】郭老二博文之C目录
1、Poco::Random 随机数
1.1 说明
POCO包括一个伪随机数生成器(PRNG)使用非线性加性反馈算法具有256位状态信息和长达269的周期。 PRNG可以生成31位的伪随机数。
它可以生成UInt32, char, bool, float和double随机值。 还可以提供随机字节流(使用/dev/random或Windows加密api)。
1.2 用法
Poco:::Random实现了一个伪随机数生成器(PRNG)。 头文件#include “Poco/Random.h”
void seed(Poco::UInt32 seed)使用给定的种子为PRNG播种。void seed()使用随机数据(来自Poco::RandomInputStream)播种PRNG
如果不使用seed()POCO:::Random的构造函数只使用当前日期和时间生成伪随机数。 建议显式调用seed()方法可以生成更理想的随机数。
UInt32 next()返回范围为[0,231]的伪随机数UInt32 next(UInt32 n)返回范围为[0,n]的伪随机数。char nextChar()返回一个伪随机字符bool nextBool()返回一个伪随机布尔值float nextFloat()返回范围为[0,1]的伪随机浮点值double nextDouble()返回范围为[0,1]的伪随机浮点值
Poco::RandomInputStream是一个istream它产生一个无限的随机字节序列。 随机字节取自/dev/random或Windows加密API(如果两者都不可用Poco::RandomInputStream创建自己的随机数据)
1.3 示例
#include Poco/Random.h
#include Poco/RandomStream.h
#include iostream
using Poco::Random;
using Poco::RandomInputStream;
int main(int argc, char** argv)
{Random rnd;rnd.seed();std::cout Random integer: rnd.next() std::endl;std::cout Random digit: rnd.next(10) std::endl;std::cout Random char: rnd.nextChar() std::endl;std::cout Random bool: rnd.nextBool() std::endl;std::cout Random double: rnd.nextDouble() std::endl;RandomInputStream ri;std::string rs;ri rs;return 0;
}2、Poco::DigestEngine 加密
2.1 说明
POCO提供了一些广泛使用的加密散列函数的实现包括如下加密
MD2、MD4、 MD5、SHA1、HMACHMAC消息验证码算法(RFC 2104)HMAC是密钥相关的哈希运算消息认证码Hash-based Message Authentication Code的缩写。是一种基于Hash函数和密钥进行消息认证的方法。
所有哈希函数和HMAC的实现都是DigestEngine类的子类。 如果想实现自己的哈希函数建议从DigestEngine派生。
2.2 用法
Poco::DigestEngine 定义了所有消息摘要算法实现的公共接口。 头文件#include “Poco/DigestEngine.h”
加密后的输出数据的长度取决于算法。在POCO中输出数据的类型是Digest摘要Digest其实就是std::vectorunsigned char。 通过反复调用Poco::DigestEngine的update()方法将数据传入算法中当所有数据都传递给Poco::DigestEngine后调用digest()方法来获取加密后的数据。
void update(const void * data, unsigned length)使用数据块更新Digest摘要void update(char data)用一个字节的数据更新Digest摘要void update(const std::string data)使用数据字符串更新Digest摘要const digest digest()完成Digest计算并返回对Digest摘要的引用
常用加密算法它们都是Poco::DigestEngine的子类
Poco::MD2Engine (#include Poco/MD2Engine.h)Poco::MD4Engine (#include Poco/MD4Engine.h)Poco::MD5Engine (#include Poco/MD5Engine.h)Poco::SHA1Engine (#include Poco/SHA1Engine.h)Poco::HMACEngine (#include Poco/HMACEngine.h)
2.3 示例
#include Poco/HMACEngine.h
#include Poco/SHA1Engine.h
using Poco::DigestEngine;
using Poco::HMACEngine;
using Poco::SHA1Engine;
int main(int argc, char** argv)
{std::string message1(This is a top-secret message.);std::string message2(Dont tell anyone!);std::string passphrase(s3cr3t); // HMAC需要一个密码短语HMACEngineSHA1Engine hmac(passphrase); // 计算一个HMAC-SHA1hmac.update(message1);hmac.update(message2);const DigestEngine::Digest digest hmac.digest();// 完成HMAC计算得到摘要 digeststd::string digestString(DigestEngine::digestToHex(digest));// 转换为十六进制数字符串return 0;
}3、流加密
3.1 说明
Poco::DigestInputStream和Poco::DigestOutputStream允许对写入输出流或从输入流读取的所有数据进行摘要计算。
Poco::DigestEngine必须传递给流的构造函数。然后流将所有经过它们的数据传递给用于摘要计算的Poco::DigestEngine。 在写入Poco::DigestOutputStream后始终刷新flush()流以确保所有数据都被传递到摘要引擎。
3.2 示例
#include Poco/DigestStream.h
#include Poco/MD5Engine.h
using Poco::DigestOutputStream;
using Poco::DigestEngine;
using Poco::MD5Engine;
int main(int argc, char** argv)
{MD5Engine md5;DigestOutputStream ostr(md5);ostr This is some text;ostr.flush(); // 确保所有内容都被传递到摘要引擎const DigestEngine::Digest digest md5.digest(); // 获取结果std::string result DigestEngine::digestToHex(digest);return 0;
}4、Poco::Timestamp 时间戳
4.1 说明
Poco::Timestamp是Poco中时间戳。 头文件 #include “Poco/Timestamp.h” Poco::Timestamp它存储一个基于UTC的64位时间值(高达)微秒分辨率。实际的分辨率取决于操作系统。 由于Poco::Timestamp是基于UTC的所以它独立于时区(并对其进行更改)。
Poco::Timestamp支持值语义、比较和简单算术。
Poco::Timestamp定义了一些公共类型:
TimeVal是一个64位带符号的整数以微秒的分辨率保存UTC时间UtcTimeVal是一个64位带符号的整数保存着100纳秒分辨率的UTC时间(实际分辨率仍然 1 μs)TimeDiff一个64位带符号的整数保存两者之间的差值时间戳以微秒为单位
在Unix中纪元epoch时间是从1970年1月1日午夜开始以秒为单位测量的时间。 UTC(协调世界时)是从1582年10月15日午夜开始以100纳秒为间隔测量的时间。
4.2 用法
Poco::Timestamp的默认构造函数用当前时间初始化时间戳Timestamp。
Timestamp fromEpochTime(time_t time)静态函数获取从纪元epoch到现在的时间戳time_tTimestamp fromUtcTime(UtcTimeVal val)获取UTC时间time_t epochTime() const返回time_t (epoch time)中表示的时间UtcTimeVal utcTime() const返回以UTC表示的时间分辨率为100纳秒TimeVal epochMicroseconds() const返回自Unix纪元以来以微秒表示的时间时间戳void update()用当前时间更新时间戳timemediff elapsed() const返回经过的微秒数时间戳bool isElapsed(TimeDiff interval) const如果从时间戳中存储的时间开始至少经过了interval微秒则返回true
4.3 示例
#include Poco/Timestamp.h
#include ctime
using Poco::Timestamp;
int main(int argc, char** argv)
{Timestamp now; // 当前日期和时间std::time_t t1 now.epochTime(); // Timestamp 转变为 time_t ...Timestamp ts1(Timestamp::fromEpochTime(t1)); // ... 转回Timestamp for (int i 0; i 100000; i) ; // 稍等一下Timestamp::TimeDiff diff now.elapsed(); // 花了多长时间?Timestamp start(now); // 记录启动时间now.update(); // 更新当前时间 diff now - start;return 0;
}5、Poco::DateTime 日期和时间
5.1 说明
Poco::DateTime用于处理基于公历的日历日期和时间。 头文件 #include “Poco/DateTime.h” Poco::DateTime常用于日期计算如果要存储日期和时间时间戳Poco::Timestamp类更合适。
Poco::DateTime在内部以两种格式维护日期和时间: UTC和(年、月、日、时、分、秒、毫秒、微秒)。
公历也叫格里高里历几乎在世界各地都在使用。它的年份是根据耶稣基督的传统出生年份来编号的这被标记为“纪元”时代。 它将天作为时间的基本单位将它们分成365天或366天的年。一年分为12个月长短不一。 并非所有国家都在同一时间采用公历(例如德国在1582年英国在1752年)。
儒略历(JDN)是从公元前4713年1月1日星期一开始的天数。这一天被记为朱利安零日。 因此7的倍数是星期一。负值也可以使用。 儒略历日期(JD)是指从格林威治标准时间中午12点开始经过的天数(带有小数部分)
5.2 用法
5.2.1 常用成员函数
int year() const返回年份int month()返回月份(1 - 12)int week(int firstDayOfWeek DateTime::MONDAY) const根据ISO 8601返回一年内的周数 (第1周为1月4日所在的一周); firstDayOfWeek应该是DateTime::MONDAY或DateTime::SUNDAY。int day() const返回一个月内的日期(1 - 31)int dayOfWeek() const返回一周内的日期 (0 DateTime::SUNDAY, 1 DateTime::MONDAY… 6 DateTime::SATURDAY)int dayOfYear() const返回一年中的日期(1 - 366)Int hour() const返回小时(0 - 23)int hourAMPM() const返回小时(0 - 12)bool isAM() const返回true如果hour() 12否则返回falsebool isPM() const返回true如果hour() 12否则返回falseint minute() const返回分钟数(0 - 59)int second() const返回秒(0 - 59)int millisecond() const返回毫秒数(0 - 999)int microsecond() const返回微秒数(0 - 999)Timestamp timestamp() const返回以时间戳形式表示的日期和时间Timestamp::UtcTimeVal utcTime() const返回以UTC时间表示的日期和时间Poco::DateTime支持所有关系操作符( ! )Poco::DateTime支持算法 ( - -)
5.2.2 常用静态函数
bool isLeapYear(int year)如果给定的年份是闰年则返回true否则返回falseint daysOfMonth(int year, int month)返回给定年、月的天数bool isValid(int year, int month, int day,int hour, int minute, int second, int millisecond, int microsecond) 如果给定的日期和时间有效(所有参数都在适当的范围内)返回true否则返回false(考虑闰年)
5.2.3 月、星期的枚举
Poco::DateTime具有月份和星期名称的枚举。这些可以用来代替数值: 1月Poco::DateTime::Months
JANUARYFEBRUARYMARCHAPRILMAYJUNEJULYAUGUSTSEPTEMBEROCTOBERNOVEMBERDECEMBER
2星期Poco::DateTime::DaysOfWeek
SUNDAYMONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAY
5.3 示例
#include Poco/DateTime.h
using Poco::DateTime;
int main(int argc, char** argv)
{DateTime now; // the current date and time in UTCint year now.year();int month now.month();int day now.day();int dow now.dayOfWeek();int doy now.dayOfYear();int hour now.hour();int hour12 now.hourAMPM();int min now.minute();int sec now.second();int ms now.millisecond();int us now.microsecond();double jd now.julianDay();Poco::Timestamp ts now.timestamp();DateTime xmas(2006, 12, 25); // 2006-12-25 00:00:00Poco::Timespan timeToXmas xmas - now;DateTime dt(1973, 9, 12, 2, 30, 45); // 1973-09-12 02:30:45dt.assign(2006, 10, 13, 13, 45, 12, 345); // 2006-10-13 12:45:12.345bool isAM dt.isAM(); // falsebool isPM dt.isPM(); // truebool isLeap DateTime::isLeapYear(2006); // falseint days DateTime::daysOfMonth(2006, 2); // 28bool isValid DateTime::isValid(2006, 02, 29); // falsedt.assign(2006, DateTime::OCTOBER, 22); // 2006-10-22 00:00:00return 0;
}6、Poco::LocalDateTime 本地时间
6.1 说明
Poco::LocalDateTime类似于Poco::DateTime除了它存储本地时间(与UTC相反)和时区差异。 头文件#include “Poco/LocalDateTime.h” 时区差是指UTC和本地时间之差(UTC 本地时间-时区之差)。
Poco::LocalDateTime支持Poco::DateTime所有功能。
所有关系运算符在执行比较之前都归一化为UTC。int tzd() const返回时区差(秒)Poco::DateTime utc() const将本地时间转换为utc时间
6.2 示例
#include Poco/LocalDateTime.h
using Poco::LocalDateTime;
int main(int argc, char** argv)
{LocalDateTime now; // 当前日期和本地时间int year now.year();int month now.month();int day now.day();int dow now.dayOfWeek();int doy now.dayOfYear();int hour now.hour();int hour12 now.hourAMPM();int min now.minute();int sec now.second();int ms now.millisecond();int us now.microsecond();int tzd now.tzd();double jd now.julianDay();Poco::Timestamp ts now.timestamp();LocalDateTime dt1(1973, 9, 12, 2, 30, 45); // 1973-09-12 02:30:45dt1.assign(2006, 10, 13, 13, 45, 12, 345); // 2006-10-13 12:45:12.345LocalDateTime dt2(3600, 1973, 9, 12, 2, 30, 45, 0, 0); // UTC 1 hourdt2.assign(3600, 2006, 10, 13, 13, 45, 12, 345, 0);Poco::Timestamp nowTS;LocalDateTime dt3(3600, nowTS); // 从时间戳构造return 0;
}7、Poco::Timespan 时间跨度
7.1 说明
Poco::Timespan表示微秒级分辨率的时间跨度。 头文件#include “Poco/Timespan.h” 在内部Poco::Timespan使用64位整数来存储时间跨度。时间跨度可以用天、小时、分钟、秒、毫秒和微秒表示。
Poco::时间跨度定义了以下缩放因子:
MILLISECONDS毫秒中的微秒数SECONDS一秒内的微秒数MINUTES一分钟内的微秒数HOURS一小时内的微秒数DAYS一天中的微秒数
7.2 用法
int days() const返回天数int hours() const返回一天中的小时数(0 - 23)int totalHours() const返回总小时数int minutes() const返回小时内的分钟数(0 - 59)int totalMinutes() const返回总分钟数Int seconds() const返回分钟内的秒数(0 - 60)int totalSeconds() const返回总秒数int milliseconds() const返回秒内的毫秒数(0 - 999)int totalMilliseconds() const返回总毫秒数Int microseconds() const返回毫秒内的微秒数(0 -999)int totalMicroseconds() const返回总微秒数
7.3 示例
#include Poco/Timespan.h
using Poco::Timespan;
int main(int argc, char** argv)
{Timespan ts1(1, 11, 45, 22, 123433); // 1d 11h 45m 22.123433sTimespan ts2(33*Timespan::SECONDS); // 33sTimespan ts3(2*Timespan::DAYS 33*Timespan::HOURS); // 3d 33hint days ts1.days(); // 1int hours ts1.hours(); // 11int totalHours ts1.totalHours(); // 35int minutes ts1.minutes(); // 45int totalMins ts1.totalMinutes(); // 2145int seconds ts1.seconds(); // 22int totalSecs ts1.totalSeconds(); // 128722return 0;
}7.4 时间运算
#include Poco/DateTime.h
#include Poco/Timespan.h
using Poco::DateTime;
using Poco::Timespan;
int main(int argc, char** argv)
{DateTime birthdate(1973, 9, 12, 2, 30); // 1973-09-12 02:30:00DateTime now;Timespan age now - birthdate;int days age.days(); // in daysint hours age.totalHours(); // in hoursint secs age.totalSeconds(); // in secondsTimespan span(10000*Timespan::DAYS);DateTime dt birthdate span;return 0;
}8、Poco::Timezone 系统时区
8.1 说明
Poco::Timezone 提供了获取系统时区信息的静态方法 头文件#include “Poco/Timezone.h”
int utcOffset()返回本地时间到UTC的偏移量以秒为单位不包括夏令时DST (local time UTC utcOffset())int DST()返回夏令时偏移量以秒为单位(通常为3600)夏令时生效否则为0。bool isDst(const timestamp timestamp)返回true如果DST在给定时间有效int tzd()返回当前时区的时区差异(tzd utcOffset() dst())string name()返回当前生效的时区名称std::string standardName()返回时区的名称如果DST没有生效std::string dstName()返回夏令时生效时的时区名称报告的名称依赖于操作系统不能跨系统移植。
8.2 示例
#include Poco/Timezone.h
#include Poco/Timestamp.h
using Poco::Timezone;
using Poco::Timestamp;
int main(int argc, char** argv)
{int utcOffset Timezone::utcOffset();int dst Timezone::dst();bool isDst Timezone::isDst(Timestamp());int tzd Timezone::tzd();std::string name Timezone::name();std::string stdName Timezone::standardName();std::string dstName Timezone::dstName();return 0;
}9、Poco::DateTimeFormatter 格式化日期和时间
9.1 说明
Poco::DateTimeFormatter 可用于格式化日期和时间 (Timestamp, DateTime, LocalDateTime和Timespan)作为字符串。 头文件 #include “Poco/DateTimeFormat.h” Poco::DateTimeFormatter使用类似于strftime()的格式字符串。
9.2 用法
Poco::DateTimeFormatter的所有函数都是静态的。
std::string format(const timestamp timestamp, const std::string fmt, int tzd UTC) 根据给定的格式字符串fmt格式化给定的时间戳。时区差值(tzd)是可选的。std::string format(const dateTime dateTime, const std::string fmt, int tzd UTC) 类似于前一个函数不同之处是这个函数接受DateTimestd::string format(const LocalDateTime dateTime, const std::string fmt) 接受一个LocalDateTime(包含时区差异)并根据格式字符串fmt对其进行格式化std::string format(const timespan timespan, const std::string fmt) 根据格式字符串fmt格式化给定的时间跨度
几种常用的已定义好的格式
ISO8601_FORMAT (2005-01-01T12:00:0001:00)RFC1123_FORMAT (Sat, 1 Jan 2005 12:00:00 0100)SORTABLE_FORMAT (2005-01-01 12:00:00)
9.3 示例
#include Poco/DateTime.h
#include Poco/Timestamp.h
#include Poco/Timespan.h
#include Poco/DateTimeFormatter.h
#include Poco/DateTimeFormat.h
using Poco::DateTimeFormatter;
using Poco::DateTimeFormat;
int main(int argc, char** argv)
{Poco::DateTime dt(2006, 10, 22, 15, 22, 34);std::string s(DateTimeFormatter::format(dt, %e %b %Y %H:%M)); // 22 Oct 2006 15:22Poco::Timestamp now;s DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT);// 2006-10-30 09:27:44Poco::Timespan span(5, 11, 33, 0, 0);s DateTimeFormatter::format(span, %d days, %H hours, %M minutes);// 5 days, 11 hours, 33 minutesreturn 0;
}10、Poco::DateTimeParser 从字符串中解析日期和时间
10.1 说明
可以使用Poco::DateTimeParser从字符串中解析日期和时间。 头文件 #include “Poco/DateTimeParser.h” Poco::DateTimeParser总是返回一个Poco::DateTime和一个时区差值。然后可以将Poco::DateTime转换为Poco::Timestamp或Poco::LocalDateTime。 Poco::DateTimeParser的所有函数都是静态的。 Poco::DateTimeParser用法和Poco::DateTimeFormatter类似。
10.2 用法
void parse(const std::string fmt, const std::string str, DateTime dateTime, int tzd) 从字符串 str 中以 fmt 给出的格式解析日期和时间。在dateTime中存储日期和时间在tzd中存储时区差异。如果无法解析字符串则抛出Poco::SyntaxException。DateTime parse(const std::string fmt, const std::string str, int tzd) 与上面类似但返回DateTimebool tryParse(const std::string fmt, const std::string str, DateTime dateTime, int tzd) 尝试从字符串str中以fmt给出的格式解析日期和时间。如果成功将日期和时间存储在dateTime中并将时区差存储在tzd中。如果成功返回true否则返回false。void parse(const std::string str, DateTime dateTime, int tzd) 解析给定字符串 str 中的日期和时间识别Poco::DateTimeFormat定义的所有标准日期/时间格式。 如果无法识别格式或者字符串与预期格式不匹配则抛出Poco::SyntaxExceptionDateTime parse(const std::string str, int tzd) 同上bool tryParse(const std::string str, DateTime dateTime, int tzd)同上
10.3 示例
#include Poco/DateTimeParser.h
#include Poco/DateTime.h
#include Poco/DateTimeFormat.h
#include Poco/LocalDateTime.h
#include Poco/Timestamp.h
using Poco::DateTimeParser;
using Poco::DateTimeFormat;
using Poco::DateTime;
int main(int argc, char** argv)
{std::string s(Sat, 1 Jan 2005 12:00:00 GMT);int tzd;DateTime dt;DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, s, dt, tzd);Poco::Timestamp ts dt.timestamp();Poco::LocalDateTime ldt(tzd, dt);bool ok DateTimeParser::tryParse(2006-10-22, dt, tzd);ok DateTimeParser::tryParse(%e.%n.%Y, 22.10.2006, dt, tzd);return 0;
}