'''
本文源自:https://www.cnblogs.com/renpingsheng/p/6965044.html
1.时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日开始按秒计算的偏移量(time.gmtime(0))
此模块中的函数无法处理1970纪元年以前的时间或太遥远的未来(处理极限取决于C函数库,对于32位系统而言,是2038年)
2.UTC(Coordinated Universal Time,世界协调时)也叫格林威治天文时间,是世界标准时间.在我国为UTC+8
3.DST(Daylight Saving Time)即夏令时
4.一些实时函数的计算精度可能不同
时间元祖(time.struct_time)
gmtime(),localtime()和strptime()以时间元祖(struct_time)的形式返回
索引值(index) 属性(Attribute) 值(Values)
0 tm_year(年) (例如:2015)
1 tm_mon(月) 1-12
2 tm_mday(日) 1-31
3 tm_hour(时) 0-23
4 tm_min(分) 0-59
5 tm_sec(秒) 0-61(60代表闰秒,61是基于历史原因保留)
6 tm_wday(星期几) 0-6(0表示星期一)
7 tm_yday(一年中的第几天) 1-366
8 tm_isdst(是否为夏令时) 0,1,-1(-1代表夏令时)
time.altzone
返回格林威治西部的夏令时地区的偏移秒数,如果该地区在格林威治东部会返回负值(如西欧,包括英国),对夏令时启用地区才能使用
time.asctime([t])
接受时间元组并返回一个可读的形式"Tue May 30 17:17:30 2017"(2017年5月30日周二17时17分30秒)的24个字符的字符串
time.clock()
用以浮点数计算的秒数返回当前的CPU时间,用来衡量不同程序的耗时,比time.time()更有用
python3.3以后不被推荐使用,该方法依赖操作系统,建议使用per_counter(返回系统运行时间)或process_time(返回进程运行时间)代替
time.ctime([secs])
作用相当于asctime(localtime(secs)),未给参数相当于asctime()
time.gmtime([secs])
接收时间辍(1970纪元年后经过的浮点秒数)并返回格林威治天文时间下的时间元组t(t.tm_isdst始终为0)
time.daylight
如果夏令时被定义,则该值为非零
time.localtime([secs])
接收时间辍(1970纪元年后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取为0或1,取决于当地当时是不是夏令时)
time.mktime(t)
接受时间元组并返回时间辍(1970纪元年后经过的浮点秒数)
time.perf_counter()
返回计时器的精准时间(系统的运行时间),包含整个系统的睡眠时间.由于返回值的基准点是未定义的,所以,只有连续调用的结果之间的差才是有效的
time.process_time()
返回当前进程执行CPU的时间总和,不包含睡眠时间.由于返回值的基准点是未定义的,所以只有连续调用的结果之间的差才是有效的
time.sleep(secs)
推迟调用线程的运行,secs的单位是秒
time.strftime(format[,t])
把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串.如果t未指定,将传入time.localtime(),如果元组中任命一个元素越界,将会抛出ValueError异常
format格式如下:
%a 本地(local)简化星期名称
%A 本地完整星期名称
%b 本地简化月份名称
%B 本地完整月份名称
%c 本地相应的日期和时间表示
%d 一个月中的第几天(01-31)
%H 一天中的第几个小时(24小时制,00-23)
%l 一天中的第几个小时(12小时制,01-12)
%j 一年中的第几天(01-366)
%m 月份(01-12)
%M 分钟数(00-59)
%p 本地am或者pm的相应符
%S 秒(01-61)
%U 一年中的星期数(00-53,星期天是一个星期的开始,第一个星期天之前的所有天数都放在第0周)
%w 一个星期中的第几天(0-6,0是星期天)
%W 和%U基本相同,不同的是%W以星期一为一个星期的开始
%x 本地相应日期
%X 本地相应时间
%y 去掉世纪的年份(00-99)
%Y 完整的年份
%z 用+HHMM或者-HHMM表示距离格林威治的时区偏移(H代表十进制的小时数,M代表十进制的分钟数)
%Z 时区的名字(如果不存在为空字符)
%% %号本身
%p只有与%I配合使用才有效果
当使用strptime()函数时,只有当在这年中的周数和天数被确定的时候%U和%W才会被计算
time.strptime(string[,format])
把一个格式化时间字符串转化为struct_time,实际上它和strftie()是逆操作
time.time()
返回当前时间的时间戳(1970元年后的浮点秒数)
time.timezone()
是当地时区(未启动夏令时)距离格林威治的偏移秒数(美洲>0,欧洲大部分,亚洲,非洲<=0)
time.tzname
包含两个字符串的元组,第一是当地夏令时区的名称,第二是当地的DST时区的名称
'''
import time
print(time.localtime()) #localtime 当地时间
print(time.gmtime()) #gmtime 英国时间
#print(time.strptime(time.localtime()))
#时间元组和字符串时间格式的转换
#"%Y-%m-%d %H:%M:%S格式
#2018-07-16 15:26:18 时间字符串
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
print(time.strptime("2018-07-16 15:25:04","%Y-%m-%d %H:%M:%S"))
print(time.asctime(time.localtime())) #获取当前系统时间,并以字符串形式显示
print(time.asctime(time.gmtime())) #获去当前系统对应的英国时间以字符串形式显示出来
print(time.process_time()) #进程运行时间
print(time.ctime(time.time())) #和asctime的功能相似蚕食是时间戳,
print(time.daylight) #夏列营时间 系统默认定义为0
print(time.mktime(time.localtime())) #将元组形式的时间转换成时间戳1531726617.0
print(time.ctime(1531726617.0))
print(time.perf_counter()) #返回计时器的精准时间
print(time.process_time()) #返回进程的运行时间
print(time.sleep(3)) #推迟线程的运行时间
print(time.time()) #返回当前时间的时间戳
print(time.timezone) #返回当地时区
#print(time.tzname) #(夏列营时间,dst时区名称)
adad dajkdaj dajdjas dajdasjd dajdjasd ajdlasjdjasldjlasjdjasldjlasjdljasldjaljdljaldjaljdlajdajldjaljdajdljaldjlajddjaldja dgdgg aaaa dddd
1qqeqe
package com.sam.project.kafka;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.common.security.JaasUtils;
import kafka.admin.AdminUtils;
import kafka.admin.RackAwareMode;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.consumer.Whitelist;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;
import kafka.server.ConfigType;
import kafka.utils.ZkUtils;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import kafka.serializer.StringEncoder;
/**
* Test kafka
*/
public class App {
private final static String URL = "192.168.1.114:2181";
private final static String NAME = "test_topic";
// 创建主题
private static void createTopic() {
ZkUtils zkUtils = ZkUtils.apply(URL, 30000, 30000, JaasUtils.isZkSecurityEnabled());
// 创建一个单分区单副本名为t1的topic
AdminUtils.createTopic(zkUtils, NAME, 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
zkUtils.close();
System.out.println("创建成功!");
}
// 删除主题(未彻底删除)
private static void deleteTopic() {
ZkUtils zkUtils = ZkUtils.apply(URL, 30000, 30000, JaasUtils.isZkSecurityEnabled());
// 删除topic 't1'
AdminUtils.deleteTopic(zkUtils, NAME);
zkUtils.close();
System.out.println("删除成功!");
}
// 修改主题
private static void editTopic() {
ZkUtils zkUtils = ZkUtils.apply(URL, 30000, 30000, JaasUtils.isZkSecurityEnabled());
Properties props = AdminUtils.fetchEntityConfig(zkUtils, ConfigType.Topic(), NAME);
// 增加topic级别属性
props.put("min.cleanable.dirty.ratio", "0.3");
// 删除topic级别属性
props.remove("max.message.bytes");
// 修改topic 'test'的属性
AdminUtils.changeTopicConfig(zkUtils, NAME, props);
zkUtils.close();
}
// 主题读取
private static void queryTopic() {
ZkUtils zkUtils = ZkUtils.apply(URL, 30000, 30000, JaasUtils.isZkSecurityEnabled());
// 获取topic 'test'的topic属性属性
Properties props = AdminUtils.fetchEntityConfig(zkUtils, ConfigType.Topic(), NAME);
// 查询topic-level属性
Iterator it = props.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + " = " + value);
}
zkUtils.close();
}
/**
* @Description: 生产者
*/
private static void producer() {
Properties properties = new Properties();
properties.put("zookeeper.connect", "192.168.1.114:2181");//声明zk
properties.put("serializer.class", StringEncoder.class.getName());
properties.put("metadata.broker.list", "192.168.1.114:9092");// 声明kafka broker
Producer producer = new Producer<Integer, String>(new ProducerConfig(properties));
int i=0;
while(true){
producer.send(new KeyedMessage<Integer, String>(NAME, "message: " + i++));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @Description: 消费者
*/
private static void customer() {
try {
Properties properties = new Properties();
properties.put("zookeeper.connect", URL);
properties.put("auto.commit.enable", "true");
properties.put("auto.commit.interval.ms", "60000");
properties.put("group.id", "test_topic");
ConsumerConfig consumerConfig = new ConsumerConfig(properties);
ConsumerConnector javaConsumerConnector = Consumer.createJavaConsumerConnector(consumerConfig);
// topic的过滤器
Whitelist whitelist = new Whitelist("test_topic");
List<KafkaStream<byte[], byte[]>> partitions = javaConsumerConnector
.createMessageStreamsByFilter(whitelist);
if (partitions == null) {
System.out.println("empty!");
TimeUnit.SECONDS.sleep(1);
}
System.out.println("partitions:"+partitions.size());
// 消费消息
for (KafkaStream<byte[], byte[]> partition : partitions) {
ConsumerIterator<byte[], byte[]> iterator = partition.iterator();
while (iterator.hasNext()) {
MessageAndMetadata<byte[], byte[]> next = iterator.next();
System.out.println("partiton:" + next.partition());
System.out.println("offset:" + next.offset());
System.out.println("接收到message:" + new String(next.message(), "utf-8"));
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
createTopic();
// deleteTopic();
// editTopic();
// queryTopic();
//producer();
// customer();
}
}
111111111