Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python time processing Library sandglass

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to use the python time processing library sandglass. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

In the process of game development, time-related processing is frequently needed, while python has built-in many time processing libraries, datetime/date/time/calendar/timedelta, etc., with a wide range of details and slightly confusing. A lot of things are not convenient and direct.

Consider the following scenarios:

What is the time stamp of International Children's Day on June 1, 2014?

What day was International Children's Day on June 1, 2014?

Datetime objects are often converted to a string with a decimal point, that is, microseconds, but I do not need microseconds to save to the database, how to do?

The time information read from the configuration, transmitted in the network, and stored in the database is a string, how can it be converted into an object that can be used for operation?

If you want to know the retention of the last 7 days, how do you get the date of the last 7 days?

During the test, you often need to change the system time, but if you do other operations during this period of time, for example, modifying the file will affect the time of the file, and the time for submitting the code is also messy, how to solve it?

How many days are there this month?

On this day last year? At this time yesterday? Two hours later?

...

When you encounter these scenarios, you should basically check the official documents, or google or stackoverflow, if you use the python built-in function. The conversion between datetime, date, timestamp, and time strings can be a headache, with the screen floating all over the screen like'% Y-%m-%d% HRV% MRO% S'.

In this case, the sandglass library is born and freed, with two main functions, ben () and tslice (). The core of the library is the Sandglass object. Sandglass means "hourglass" and ben is "Big Ben". People who use python basically know that slicing of python is very flexible, and tslice is something like this used to produce time series.

Through ben (), you can quickly obtain the Sandglass object through a variety of time information, while through the Sandglass object, you can get a variety of information and operations.

The Sandglass object acts as a transit station, making various transformations simple. This idea is a bit like transcoding, various codes-> unicode- > all kinds of codes. Unicode is the transit station.

All right, let's demonstrate how to solve the above requirements with the sandglass library.

From sandglass import ben,tslice,Sandglass

# what is the timestamp of International Children's Day on June 1, 2014?

Ben ('2014-06-01'). Timestamp

# what day was International Children's Day on June 1, 2014?

Ben ('2014-06-01'). Isoweekday

# datetime objects are often converted to a string with a decimal point, that is, microseconds, but I do not need microseconds to save to the database.

Now = ben ()

Now.sql

# the time information obtained from configuration, network transmission and database storage is a string, so how can it be converted into an object that can be used for operation?

Ben (1399387043.898), ben ('2014-02-03'), ben ('2014 beacon 2, Magi 3'), ben ('2014 pedigree 2, 3'), ben ('19-20-0-00')

# if you want to know the retention of the last 7 days, how do you get the date of the last 7 days?

Tslice ('day',start=None,end=None,step=-1,count=7)

# it is often necessary to change the system time in testing, but during this period of time, other operations will be affected, such as modifying the file will affect the file time, and the code submission time will be out of order. How to solve this problem?

Sandglass.mock (day=1) # changes the current date to tomorrow, and all times obtained by ben () or Sandglass.now () are affected

How many days are there in this month?

Ben () days_in_month

# on this day last year? At this time yesterday? Two hours later?

Ben (). Shift (year=-1), ben (). Shift (day=-1), ben (). Shift (hour=2)

The above is the editor for you to share how to use the python time processing library sandglass, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report