RPA used Python's datetime library processing time

When the RPA process automation process, meet the relevant operating time, you can call some methods datetime library for processing.
datetime is the date and time of processing Python standard library.


Get the current date and time


We look at how to get the current date and time:

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now)
2019-11-23 11:12:32.715604
>>> print(type(now))
<class 'datetime.datetime'>

 



Datetime is noted that module, the module further comprises a datetime datetime class, introduced by from datetime import datetime is datetime class.

If you import only import datetime, you must reference the full name datetime.datetime.

DateTime.Now () Returns the current date and time, the type is datetime.

 

Gets the specified date and time


To specify a date and time, we directly construct a datetime parameter:

>>> from datetime Import datetime
 >>> Time = datetime (2008,8,8,8,8) # Create a datetime with the specified date and time 
>>> Print (Time)
 2008-08-08 08:08:00

 

 

datetime timestamp is converted to
a computer, the time is actually a digital representation. We January 1 1970 00:00:00 UTC + 00: 00 time zone called the epoch time, recorded as 0 (before 1970 time timestamp is negative), the current time is relative to the number of seconds epoch time called timestamp.

You can think of:
timestamp = 0 = 1970-1-1 00:00:00 UTC + 0: 00

GMT is the corresponding:
timestamp = 0 = + 1970-1-1 08:00:00 UTC. 8: 00

Visible timestamp value and the time zone has nothing, because the timestamp is established, its UTC time on the set, switch to the time when any zone is completely determined, which is why the current time of the computer memory is based on timestamp represented, because all over the world computer at any time of the timestamp are identical (assuming calibrated time).

The datetime type is converted to a timestamp simply call timestamp () method:

 

>>> from datetime import datetime
>>> time = datetime(2019,11,23,11,25)
>>> time.timestamp()
1574479500.0

 



Note Python's timestamp is a floating-point number. If there are decimal, fractional bits represents the number of milliseconds.


timestamp converted to datetime


Timestamp should convert datetime, using fromtimestamp datetime provided () method:

>>> from datetime import datetime
>>> time = 1574479500.0
>>> print(datetime.fromtimestamp(time))
2019-11-23 11:25:00

 


He noted that the timestamp is a floating point number, it does not have the concept of time zones, and is sometimes datetime area. The above conversion timestamp is in local time and do the conversion.

Local time is the current operating system time zone setting. For example, Beijing time zone is the East Area 8, the local time: 2019-11-23 11:25:00

In fact, UTC + 8: 00 time zone: 2019-11-23 11:25:00 UTC + 8: 00

And at the moment of Greenwich mean time difference with Beijing eight hours, which is UTC + 0: 00 when time zone should be:

2019-11-23 03:25:00 UTC+0:00

timestamp may be converted directly to a standard time zone UTC:

```python
>>> from datetime import datetime
>>> time = 1574479500.0
>>> print(datetime.fromtimestamp(time))
2019-11-23 11:25:00
>>> print(datetime.utcfromtimestamp(time)) # UTC 时间
2019-11-23 03:25:00
```


str converted to a datetime
In many cases, the date and time a user enters a string, to deal with the date and time, you must first convert str to datetime. Conversion is () is achieved by datetime.strptime, requires a date and time format strings:

 

>>> from datetime import datetime
>>> today = datetime.strptime('2019-11-23 11:34:49','%Y-%m-%d %H:%M:%S')
>>> print(today)
2019-11-23 11:34:49
>>>

 


String '% Y-% m-% d% H:% M:% S' specified date and time format portion, attention is not converted datetime time zone information.


datetime convert str
if already datetime objects, take it to the user is formatted as a string, it needs to be converted, also need a date and time format string str, is converted by strftime () implemented :

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now.strftime('%a, %b %d %H:%M'))
Sat, Nov 23 14:45

 

datetime subtraction
of date and time datetime subtraction is actually the back or forward calculation, the new datetime. + And subtraction can be directly used - operator, but need to import timedelta this class:

>>> from datetime import datetime,timedelta
>>> now = datetime.now()
>>> now
datetime.datetime(2019, 11, 23, 14, 49, 49, 739236)
>>> now + timedelta(hours = 5)
datetime.datetime(2019, 11, 23, 19, 49, 49, 739236)
>>> now - timedelta(days = 1)
datetime.datetime(2019, 11, 22, 14, 49, 49, 739236)
>>> now - timedelta(days = 3,hours = 3)
datetime.datetime(2019, 11, 20, 11, 49, 49, 739236)


Visible, use timedelta a few days ago and after a few days time you can easily calculate.


Local time into UTC time


Local time is the time zone when the system is set, for example GMT is UTC + 8: 00 time zone, and UTC time refers to UTC 0 +: 00 time zone.

A datetime type have a time zone property tzinfo, but the default is None, it is impossible to distinguish the datetime in the end is which time zone, unless forced to datetime set a time zone:

>>> from datetime import datetime,timedelta,timezone
>>> time_utc = timezone(timedelta(hours=8)) # 创建时区 UTC+8:00
>>> now = datetime.now()
>>> now
datetime.datetime(2019, 11, 23, 14, 55, 5, 901745)
>>> now_utc = now.replace(tzinfo=time_utc) # 强制设置为 UTC+8:00
>>> now_utc
datetime.datetime(2019, 11, 23, 14, 55, 5, 901745, tzinfo=datetime.timezone(datetime.timedelta(0, 28800)))

 

If the system time zone is exactly UTC + 8: 00, so the above code is correct, otherwise, it can not be forced to UTC + 8: 00 time zones.

 

Time Zone Converter


Can get the first current UTC time by utcnow (), and then converted to an arbitrary time zone:

UTC_TIME = DateTime.UtcNow >>> () Replace (tzinfo = timezone.utc). # Get the UTC time, the time zone and forcibly set to 0 + UTC: 00 
>>> Print (UTC_TIME)
 2019-11-23 07:05 : 25.865221 + 00: 00 
>>> 
>>> bj_time = utc_time.astimezone (TimeZone (timedelta (hours = 9))) # astimezone () when converting to Tokyo time zone 
>>> Print (bj_time)
 2019-11-23 16: 05: 25.865221 + 09: 00 
>>> 
>>> dj_time = utc_time.astimezone (TimeZone (timedelta (hours = 9))) # conversion to Tokyo time zone 
>>> Print (dj_time)
 2019-11-23 16 : 05: 25.865221 + 09: 00 
>>> 
>>> dj_time2 = bj_time.astimezone (TimeZone (timedelta (hours = 9))) # When you convert GMT time zone to Tokyo
>>> print(dj_time2)
2019-11-23 16:05:25.865221+09:00

 


The key is that the time zone transition, when get a datetime, to know the correct time zone, and then forcibly set the time zone as the reference time.
Datetime band using time zones by astimezone methods, can be converted () to an arbitrary time zone.

Note: It is not necessary + 0 from UTC: 00 time zone to another time zone conversion, any datetime with time zone can be correctly converted, for example, to the above-described bj_time dj_time2 conversion.


Summary
time datetime representation when needed area information to determine a specific time, or only as a local time.
To store datetime, the best method is to convert it to another storage timestamp, the timestamp value as completely unrelated to the time zone.

 

Free Trial: https://support.i-search.com.cn/

Guess you like

Origin www.cnblogs.com/isearch/p/11918356.html