Rất nhiều câu trả lời này không bận tâm xem xét rằng ngày là ngây thơ để bắt đầu
Để đúng, bạn cần làm cho ngày ngây thơ trở thành một thời gian dữ liệu nhận thức về thời gian
import datetime
import pytz
# naive datetime
d = datetime.datetime.strptime['01/12/2011', '%d/%m/%Y']
>>> datetime.datetime[2011, 12, 1, 0, 0]
# add proper timezone
pst = pytz.timezone['America/Los_Angeles']
d = pst.localize[d]
>>> datetime.datetime[2011, 12, 1, 0, 0,
tzinfo=]
# convert to UTC timezone
utc = pytz.UTC
d = d.astimezone[utc]
>>> datetime.datetime[2011, 12, 1, 8, 0, tzinfo=]
# epoch is the beginning of time in the UTC timestamp world
epoch = datetime.datetime[1970,1,1,0,0,0,tzinfo=pytz.UTC]
>>> datetime.datetime[1970, 1, 1, 0, 0, tzinfo=]
# get the total second difference
ts = [d - epoch].total_seconds[]
>>> 1322726400.0
Also:
Hãy cẩn thận, sử dụng pytz
cho
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
0 trong # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
1 không hoạt động cho nhiều múi giờ. Xem DateTime với thời gian pytz. Bù khác nhau tùy thuộc vào cách TZINFO được đặt# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
//en.wikipedia.org/wiki/Local_mean_time
Dấu thời gian UNIX là một số nguyên có chữ ký duy nhất tăng lên mỗi giây, cho phép máy tính lưu trữ và thao tác các hệ thống ngày thông thường. Phần mềm sau đó được dịch thành định dạng có thể đọc được của con người. Dấu thời gian UNIX là số giây được tính toán kể từ ngày 1 tháng 1 năm 1970. Trong bài viết này, chúng ta sẽ xem cách chuyển đổi DateTime thành Unix Timestamp.
DateTime to Unix TimeStamp
Để chuyển đổi DateTime Python thành Unix Timestamp, chúng tôi đã nhập một mô -đun gọi là DateTime và Time trong ví dụ này và biến DATE_TIME đã được khai báo và gán DateTime. Thời gian/ngày [2021, 7, 26, 21, 20]. Năm là 2021, tháng là 7, ngày là 26, giờ là 21 và phút là 20.
Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
5# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
6# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
8# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
9date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.01
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.03______
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497724509.2930
1628497724509.2931
1628497724509.2932
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497724509.2930
1628497724509.2935
1628497724509.2936
1628497724509.2937
1628497724509.2938
Output:
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.0
Explanation:
Các lớp thao tác ngày và giờ được cung cấp bởi mô -đun DateTime. Hàm nghịch đảo của thời gian địa phương là mktime []. Nó chấp nhận thời gian cấu trúc hoặc 9 tuple đầy đủ như một đối số và trả về một số điểm nổi để tương thích với thời gian []. Nó cũng được sử dụng để chuyển đổi DateTime thành dấu thời gian UNIX.
Phương thức thời gian biểu [] của các đối tượng DateTime.date trả về một đối tượng thời gian. Thời gian. Đối tượng thời gian cấu trúc là một bộ tuple có tên có thể được truy xuất bằng cách sử dụng một chỉ mục hoặc theo tên. Các trường năm, tháng và ngày của bộ tuple được đặt tên được trả về bởi hàm Timetuple [] sẽ được đặt theo đối tượng ngày, trong khi các trường, phút và giây sẽ được đặt thành 0.
DateTime to Unix TimeStamp với 13 chữ số
Để có được thời gian hiện tại, hãy sử dụng DateTime.now []. Hàm Timetuple [] của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Dấu thời gian với 13 chữ số phải được nhân với 1000.
Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
5# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
6# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
8# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
9date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.01
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.03______
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497724509.2930
1628497724509.2931
1628497724509.2932
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497823000.02
Output:
1628497724509.293
Các lớp thao tác ngày và giờ được cung cấp bởi mô -đun DateTime. Hàm nghịch đảo của thời gian địa phương là mktime []. Nó chấp nhận thời gian cấu trúc hoặc 9 tuple đầy đủ như một đối số và trả về một số điểm nổi để tương thích với thời gian []. Nó cũng được sử dụng để chuyển đổi DateTime thành dấu thời gian UNIX.
Phương thức thời gian biểu [] của các đối tượng DateTime.date trả về một đối tượng thời gian. Thời gian. Đối tượng thời gian cấu trúc là một bộ tuple có tên có thể được truy xuất bằng cách sử dụng một chỉ mục hoặc theo tên. Các trường năm, tháng và ngày của bộ tuple được đặt tên được trả về bởi hàm Timetuple [] sẽ được đặt theo đối tượng ngày, trong khi các trường, phút và giây sẽ được đặt thành 0.
Code:
Python3
DateTime to Unix TimeStamp với 13 chữ số
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3Để có được thời gian hiện tại, hãy sử dụng DateTime.now []. Hàm Timetuple [] của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Dấu thời gian với 13 chữ số phải được nhân với 1000.
16284977833
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 16284977835
16284977836
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 16284977838
16284977839
1628497823000.00
Output:
1628497783
DateTime to Unix Timestamp trong UTC Timezone
Mô-đun lịch cung cấp các chức năng liên quan đến lịch hữu ích. Hàm UTC.now trả về thời gian hiện tại trong múi giờ UTC. Trong mô -đun thời gian, hàm timegm trả về dấu thời gian UNIX. Hàm Timetuple [] của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Để có được dấu thời gian UNIX, hãy sử dụng in [UTC].
Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
5Unix_Time: 1628188200.09
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 16284977835
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628209448.03
16284977839
1628497823000.00
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.08
Output:
1628497823000.0
DateTime.Date to Unix Timestamp
Time.Date [] là một hàm chấp nhận chỉ ngày. Trong trường hợp này, năm 2021 là năm, 8 là tháng và 6 là ngày. mktime [] là một phương thức thời gian là hàm nghịch đảo của thời gian địa phương; Nó được sử dụng để chuyển đổi ngày thành dấu thời gian UNIX.
Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
5Unix_Time: 1628188200.09
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 16284977835
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497724509.2930
pytz
21628497724509.2936
1628497724509.2937
pytz
5Output:
Unix_Time: 1628188200.0
date_time => 2021-07-26 21:20:00
unix_timestamp => 1627314600.0
91628209448.0
31628497783
9 1628497823000.0
0date_time => 2021-07-26 21:20:00
unix_timestamp => 1627314600.0
8
DateTime.Date to Unix Timestamp
Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3Time.Date [] là một hàm chấp nhận chỉ ngày. Trong trường hợp này, năm 2021 là năm, 8 là tháng và 6 là ngày. mktime [] là một phương thức thời gian là hàm nghịch đảo của thời gian địa phương; Nó được sử dụng để chuyển đổi ngày thành dấu thời gian UNIX.
Unix_Time => 1627334400 Date & Time => 2021-07-27 02:50:001
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 Unix_Time => 1627334400 Date & Time => 2021-07-27 02:50:003
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
9date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
Unix_Time => 1627334400 Date & Time => 2021-07-27 02:50:006
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.00
Unix_Time => 1627334400 Date & Time => 2021-07-27 02:50:008
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.08
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
04# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
05date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.08
Chuỗi DateTime đến Unix TimeStamp
Ngày và thời gian được cung cấp ở định dạng chuỗi trong trường hợp này. Ở đây, 8 biểu thị tháng, 6 biểu thị ngày, 2021 biểu thị năm, 05 biểu thị giờ, 54 biểu thị phút và 8 biểu thị thứ hai. strptime [] là một phương thức mô -đun DateTime được sử dụng để chuyển đổi chuỗi thành các đối tượng DateTime và Time. Hàm dấu thời gian [] trả về thời gian hiện tại ở vị trí hiện tại.
Output:
1628209448.0
pytz
8# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
00
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
01# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
03# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
07# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
7 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
09date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
11Code:
Python3
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
2 # Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
3UNIX dấu thời gian đến Python DateTime
Mô-đun DateTime trong Python được sử dụng để xử lý các vấn đề ngày và thời gian trong Python. Phương thức Fromtimestamp [] là một trong những hàm có trong mô -đun này. Chức năng của lớp ngày từ Fromtimestamp [] tính toán và trả về ngày tương ứng với một dấu thời gian được chỉ định. Phạm vi dấu thời gian được phép kéo dài từ năm 1970 đến 2038. Nếu có bất kỳ giây bước nhảy nào trong dấu thời gian, hàm fromtimestamp [] sẽ bỏ qua chúng.
date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.09
1628497724509.2930
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
22# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
23Để bắt đầu, chúng tôi nhập lớp DateTime từ mô -đun DateTime. Đối tượng giá trị Unix sau đó được lưu trữ trong một biến. Sau đó, chúng tôi sử dụng phương thức DateTime.FromTimeStamp [] để truy xuất thời gian và ngày. & nbsp;
1628497724509.2937
# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
29# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
30# Don't do this:
d = datetime.datetime[2011, 12, 1,0,0,0, tzinfo=pytz.timezone['America/Los_Angeles']]
>>> datetime.datetime[2011, 1, 12, 0, 0,
tzinfo=]
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone[pytz.UTC]
>>> datetime.datetime[2011, 1, 12, 7, 53, tzinfo=]
# you end up with an offset
31Output:
Unix_Time => 1627334400 Date & Time => 2021-07-27 02:50:00