0

I am new to Python and I tried to find the answer from the existing posts, and I did my attempt but I can't find what I want.

I need to validate the time(based of china timezone regardless of where the client at) diff when the client send requests to my server .

From the existing posts I can find, I had tried:

import calendar
import datetime
import pytz
import time

tz = pytz.timezone('Asia/Shanghai') # china timezone
cn_time = datetime.datetime.now(tz) # get datetime for china
print calendar.timegm(cn_time.timetuple())*1000 #try to get the milliseconds 

But I find that the result is far away from my java server's answer from Joda Time:

DateTime serverDt = new DateTime(DateTimeZone.forID("Asia/Shanghai"));
long milis = serverDt.getMillis();

One test case is:

python : 1457005502000

java: 1456976702999

seonds diff from int secDiff = Seconds.secondsBetween(dt, serverDt).getSeconds(); is -28799 which is -7 hours

Note: My machine is at china timezone.

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • What do you get in each case? – Selcuk Mar 03 '16 at 03:44
  • @Selcuk , please check my updated question – JaskeyLam Mar 03 '16 at 03:50
  • Your question is unclear. Do you mean you want `time.time()`? What is your input? Is it a string? Provide an example. Has the output from java server anything to do with the requests sent by the client? What is client's timezone? – jfs Mar 03 '16 at 14:03

1 Answers1

0

Your code tries to find the current Unix time. That value does not depend on a specific timezone (it is the same number on all computers with synchonized (e.g., using ntp) clocks whatever (perhaps different) time zones they use). Just call milis = time.time() * 1000, to get the same value as the java server.

If you need to get the Unix time that corresponds to a given timezone-aware datetime (such as created by datetime.now(tz) at some point) then just call posix_timestamp = cn_time.timestamp().

There is no datetime.timestamp() method on Python 2. You could emulate it easily:

def timestamp(aware_dt, epoch=datetime(1970, 1, 1, tzinfo=pytz.utc)):
    return (aware_dt - epoch).total_seconds()

Usage: posix_timestamp = timestamp(cn_time). See more details in this answer.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • `milis = time.time() * 1000` depends on the local timezone, but what if the client at a non-china timezone, what I need is to get the timestamp excatly as Asia/Shanghai. – JaskeyLam Mar 09 '16 at 02:11
  • @Jaskey : wrong. POSIX timestamp is the number of seconds since 1970-01-01 00:00:00UTC (the Epoch) not counting leap seconds. It is the same value around the world regardless the local timezone. Follow the link and read the more detailed answer (follow links in the answer to get even more details). Look at the explicit formulai in the answer. – jfs Mar 09 '16 at 02:18
  • Sorry for some missing points, I find that your first paragraph is just to point out my wrongness, which indeed is not what I want, what I want is a timezone-aware datetime timestamp. I wonder whether the answer that @Selcuk given is right? `time.mktime(cn_time.timetuple()) * 1000`, since I compare that value with my server, it is very close to the same(several milliseconds diff only) – JaskeyLam Mar 09 '16 at 02:58
  • @Jaskey 1- selcuk's answer is completely wrong (it produces junk). 2- if you don't want the value returned by Joda's `getMillis()` then edit your question and specify exactly what you need instead. – jfs Mar 09 '16 at 03:27
  • What I want is to validate time diff. Since the client may be at anywhere, so we need the compare the china-timezone time. At server, we use joda to the the timestamp, and at client , we get the timestamp from client sent from param, we compare it. So in a word, "china-timezone-aware datetime " is what I need at python. Then why `time.mktime(cn_time.timetuple()) * 1000` is wrong, since it is even the same to the the server time(except for some time cost from client to server ).? – JaskeyLam Mar 09 '16 at 06:11
  • @Jaskey I don't know how to express it more clearly that `getMillis()` value does NOT depend on the local timezone (perhaps excluding "right" timezones). Provide a specific example of "valid" values in different timezones e.g., if you get X in New York then what value do you expect in Los Angeles? – jfs Mar 09 '16 at 06:20