数据开发_Python中时间处理和Hive以及SparkSQL中时间

时间相关的内容

日期和时间
年历和月历 
转换日期格式
    时间戳、格式化时间字符串和结构化时间
	  时间戳指的是从1970年1月1日00:00:00开始按秒计算的偏移量
tzinfo (时区) 时区转换
时间数据
    数据分为时期数和时点数。
	    时期 数 是反映现象在一段时间内发生的总量
		时点 数 是表明事物总体在某一时点上的数量状态
频率  偏移量 移动

SparkSQL和HiveSQL中的时间

Spark中的时间
 01.类型
  TimestampType 
  DateType 
 02.
  to_unix_timestamp(timeExp[, format])
  weekday(date)
  current_timestamp()
  weekofyear(string date)

Hive中时间
 类型
 01.Date and time types
      TIMESTAMP                       — A date and time without a timezone ("LocalDateTime" semantics)
      TIMESTAMP WITH LOCAL TIME ZONE  — A point in time measured down to nanoseconds ("Instant" semantics)
      DATE                            — a date
 02.函数 输入 函数 返回值  功能描述 示例
      string  from_unixtime(int unixtime)   
      string  to_date(string timestamp)
      int    year(string date)
      int    month(string date)
      int	day(string date)
     unix_timestamp()
     weekofyear(string date)

Python的时间

数据类型
   datatime模块重新封装了time模块,提供更多接口,提供的类有:
     time,date,datetime,timedelta,tzinfo
	    两个date或datetime对象相减就可以返回一个 timedelta 对象
		datetime相当于date和time结合起来。其属性有 year, month, day, hour , minute , second , microsecond , tzinfo
	pandas模块
	   DatatimeIndex  TimedeltaIndex

数据操作
   p表示 parse, 表示 分析    strptime 是给定一个时间 字符串 和 分析模式,返回一个时间 对象。
   f表示 format,表示 格式化,strptime 是给定一个时间 对象   和 输出格式,返回一个时间 字符串
 时间戳和日期
    转换为 时间戳  .timestamp()
   datetime.now().timestamp()
    时间戳转换位时间 fromtimestamp

Python时间代码

from datetime import datetime, timedelta,time

if __name__ == '__main__':
    now_time = datetime.now()
    print(now_time)
    now_2 = datetime(2020, 11, 22)
    # 返回  datetime.timedelta
    diff_time = now_time - now_2
    print(diff_time)
    print(type(diff_time))
    print(diff_time.days)
    # 进行加减
    print(now_2 + timedelta(days=3))

    # 格式转换  int  str  datetime
    # str转时间格式: strptime  strftime
    # p表示 parse, 表示 分析    strptime 是给定一个时间 字符串 和 分析模式,返回一个时间 对象。
    dd = '2020-11-22 17:40:54'
    dd = datetime.strptime(dd, "%Y-%m-%d %H:%M:%S")
    print(dd, type(dd))

    # from 时间格式转str:
    # f表示 format,表示 格式化,strptime 是给定一个时间 对象   和 输出格式,返回一个时间 字符串
    dc = dd.strftime("%Y-%m-%d %H:%M:%S")
    print(dc,type(dc))

    # 时间戳和datetime之间的转换  utcfromtimestamp
    unix_ts = datetime.now().timestamp()
    time1_str = datetime.fromtimestamp(unix_ts)
    print(time1_str)
    time1_str = datetime.utcfromtimestamp(unix_ts)
    print(time1_str)
    print(time1_str, type(time1_str))
    print(time1_str.timestamp())

参考:

 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
 http://spark.apache.org/docs/latest/sql-ref-functions-builtin.html#date-and-timestamp-functions
posted @ 2020-11-26 18:11  辰令  阅读(369)  评论(0编辑  收藏  举报