[英]How to convert in seconds from datetime to a timestamp?
Good day. 美好的一天。 Such a question - there is a method in python that gets datetime and converts it into a timestamp:
这样的问题-python中有一个获取日期时间并将其转换为时间戳的方法:
source = 'Friday 9:14 PM March 9 2018'
central_tz = pytz.timezone('Canada/Eastern')
utc_tz = pytz.timezone('UTC')
naive_dt = datetime.datetime.strptime(source, "%A %I:%M %p %B %d %Y")
utc_ts = time.mktime(utc_dt.timetuple())
print(central_tz.localize(native_dt, is_dst=None))
print("naite_dt")
print(naive_dt)
central_dt = central_tz.localize(naive_dt, is_dst=None)
print("central_dt")
print(central_dt)
utc_dt = central_dt.astimezone(pytz.utc)
print("utc_dt")
print(utc_dt)
utc_ts = time.mktime(utc_dt.timetuple())
print("utc_ts")
print(utc_ts)
On output: 输出时:
naite_dt
2018-03-09 21:14:00
central_dt
2018-03-09 21:14:00-05:00
utc_dt
2018-03-10 02:14:00+00:00
utc_ts
1520640840.0
And I have implementation of the same method in java: 而且我在java中实现了相同的方法:
public static void scraper() throws ParseException {
String source = "2018 марта 9, 09:14 PM";
String pattern = "yyyy MMMM dd, KK:mm a";
String central_tz = "Canada/Eastern";
String utc_tz = "UTC";
DateFormat dt = new SimpleDateFormat(pattern);
dt.setTimeZone(TimeZone.getTimeZone(central_tz));
Date date = dt.parse(source);
String formatted = dt.format(date);
System.out.println("Время по Канаде: " + formatted);
dt.setTimeZone(TimeZone.getTimeZone(utc_tz));
formatted = dt.format(date);
System.out.println("Время по UTC: " + formatted);
Timestamp timestmp = Timestamp.from(date.toInstant());
System.out.println("Время Timestamp: " + timestmp.toString());
System.out.println("Время в секундах Timestamp: " + timestmp.getTime() / 1000);
}
But it's output and it's true output: 但是它是输出,是真实的输出:
Время по Канаде: 2018 марта 09, 09:14 PM
Время по UTC: 2018 марта 10, 02:14 AM
Время Timestamp: 2018-03-10 04:14:00.0
Время в секундах Timestamp: 1520648040
On final we have output timestamp in Python 1520640840 and output timestamp in Java 1520648040 - it's different!!! 最后,我们在Python 1520640840中有输出时间戳,在Java 1520648040中有输出时间戳-完全不同!!! Why?
为什么? And woh I can fixed it in Python?
我可以用Python修复它吗? Thank's
谢谢
If you notice the time shown in python is 2018-03-10 02:14:00+00:00 which you are using to convert into timestamp and in Java when you are using the date variable which has the time
2018-03-10 04:14:00.0.
如果您注意到python中显示的时间是2018-03-10 02:14:00 + 00:00 ,则当您使用具有时间
2018-03-10的date变量时,它将用于转换为时间戳,而在Java中
04:14:00.0。
There's a 2 hour difference between both. 两者之间相差2小时。 Try to find why that happened.
尝试找出原因。 What I think is you are in timezon UTC +2 therefore the date variable stored the time as 4:14 am for which you did not set a time zone and in python your
date
variable " utc_dt
" used for converting in timestamp has the timezone UTC hence its time is 2:14 am. 我认为您是在时区UTC +2中,因此日期变量将时间存储为4:14 am,而您未为其设置时区,而在python中,用于在时间戳中进行转换的
date
变量“ utc_dt
”具有时区UTC因此,其时间是凌晨2:14。
You can verify this by calculating the difference between both timestamps which is 7200 which means 2 hours. 您可以通过计算两个时间戳之间的时差7200(即2小时)来验证这一点。 So in fact your Java time stamp is set according to your local time zone and python timestamp is set according to the UTC time zone.
因此,实际上您的Java时间戳是根据您的本地时区设置的,而python时间戳是根据UTC时区设置的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.