Sandglass (Hourglass)--a free Python time processing library

Source: Internet
Author: User

In the process of game development, it is often necessary to deal with time-related processing. And Python built a lot of time processing library, Datetime/date/time/calendar/timedelta, and so on, the details are slightly confusing. Very many things are not convenient direct.
Consider the following scenarios:
    • 2014 Children's Day What is the time stamp on children's Day?
    • 2014 Children's Day What is the day of the week?
    • DateTime objects are often converted to strings with decimal points, or microseconds. And I don't need a microsecond to save it to a database.
    • Read from the configuration, the transmission in the network, the time information obtained by the database store is a string, how to turn into an object that can be used for the operation?
    • Be aware of the recent 7 days of retention. How to get the date of the recent 7 days?
    • Testing often needs to change the system time, but the system time to assume that the time to do other operations will be affected, such as changing the file will cause the time of the file is affected, commit code time is also a mess. How to solve?
    • How many days are there this month?
    • Last year's Day? At this time of yesterday? 2 hours later?
    • ...

In these scenarios, the Python built-in function basically checks the official documents, or Google or StackOverflow. The conversion between datetime, date, timestamp, and time string can be a headache, and the '%y-%m-%d%h:%m:%s ' is full of screens.


In this case, the Sandglass library was born. Freed, the main function is 2. Ben () and Tslice (). The core of the library is the Sandglass object . The meaning of Sandglass is " Hourglass ". Ben is " Big Ben ". Python's basics know that Python slices are very flexible. Tslice is something like this that is used to produce a time series.
Through Ben (), Sandglass objects can be obtained at high speed through various time information, and through Sandglass objects. Access to a variety of information and operations.
The Sandglass object is equivalent to a broker . Makes all kinds of conversions easier.

Such a concept is somewhat similar to transcoding. Various codes->unicode-> various codes. Unicode is a transit point.
Well, here's a demonstration of how to solve those requirements with the Sandglass library.

>>>from Sandglass Import Ben,tslice,sandglass
#2014年六一儿童节那天的时间戳是多少?
>>>Ben (' 2014-06-01 '). Timestamp
#2014年六一儿童节那天是星期几?
>>>Ben (' 2014-06-01 '). Isoweekday
#datetime对象转成字符串后面往往带小数点. That's microseconds, and I don't need a microsecond to save it to a database.
>>>Now = Ben ()
>>>Now.sql
The time information obtained by the #从配置中读取, the transmission in the network, and the database store is a string. How do you turn it into an object that can be used for arithmetic?
>>>Ben (1399387043.898), Ben (' 2014-02-03 '), Ben (' 2014,2,3 '), Ben (' 2014/2/3 '), Ben (' 19:20:00 ' )
#要知道近期7天的留存. How to get the date of the recent 7 days?
>>>tslice (' Day ', start=none,end=none,step=-1,count=7)
#測试中常常须要改系统时间. But to change the system time this period of time to assume that other operations will be affected, such as changing the file will cause the file time is affected, the time to commit the code is also confused, how to solve?
>>>Sandglass.mock (Day=1)#把当前日期改成明天, all the time that Ben () or Sandglass.now () gets is affected
#这个月有几天?
>>>Ben (). Days_in_month
#去年的今天? At this time of yesterday? 2 hours later?
>>>Ben ( ). Shift (Year=-1), Ben (). Shift (Day=-1), Ben (). Shift (hour=2)

For many other details, please click on the project address.

Sandglass (Hourglass)--a free Python time processing library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.