Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29046 > unrolled thread

datetime

Started byMax <readmax@hushmail.com>
First post2012-09-13 15:19 +0000
Last post2012-09-14 02:37 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  datetime Max <readmax@hushmail.com> - 2012-09-13 15:19 +0000
    Re: datetime Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 03:35 +0000
      Re: datetime Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-14 00:50 -0400
      Re: datetime Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-14 02:37 -0400

#29046 — datetime

FromMax <readmax@hushmail.com>
Date2012-09-13 15:19 +0000
Subjectdatetime
Message-ID<mailman.614.1347550804.27098.python-list@python.org>
How do I set the time in Python?

Also, is there any *direct* way to shift it?

Say, it's 09:00 now and Python makes it 11:30 *without* me having specified
"11:30" but only given Python the 2h30m interval.

Note that any "indirect" methods may need complicated ways to keep
track of the milliseconds lost while running them. It even took around one
second in some virtual machine guest systems. So I'm hoping Python happens to
have the magic needed to do the job for me.

[toc] | [next] | [standalone]


#29106

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-09-14 03:35 +0000
Message-ID<5052a601$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#29046
On Thu, 13 Sep 2012 15:19:32 +0000, Max wrote:

> How do I set the time in Python?

You don't. You ask the operating system to set the time. If you don't 
have permission to change the time, which regular users shouldn't have 
because it is a security threat, it will (rightly) fail. E.g.:

import os
os.system('date -s %s' % date_str)

In Python 3.3 there is a wrapper in the time module that allows you to 
set the clock without an explicit system call. Again, you need permission 
to set the clock, or it will fail.


> Also, is there any *direct* way to shift it?
>
> Say, it's 09:00 now and Python makes it 11:30 *without* me having
> specified "11:30" but only given Python the 2h30m interval.

Certainly. Just call:

time.sleep(2*60**2 + 30*60)

and when it returns, the clock will have shifted forward by 2h30m, just 
like magic!

*wink*



> Note that any "indirect" methods may need complicated ways to keep track
> of the milliseconds lost while running them. It even took around one
> second in some virtual machine guest systems. So I'm hoping Python
> happens to have the magic needed to do the job for me.

No. Setting the clock is not the business of any user-space application. 
It is the job of the operating system, which will do it the right way. At 
most, the application can call the OS, directly or indirectly, but it has 
no control over how many milliseconds are lost when you do so.

On Linux, Unix or Mac, that right way is to use NTP, which will keep your 
computer's clock syncronised with a trusted external source. In a virtual 
machine, the right way is to use NTP to syncronise the VM host's time, 
and then tell the host to synchronise itself with the VM. On Windows, 
well you'll have to ask a Windows expert.

If you want to bypass NTP and manage time yourself -- say, you want to 
simulate "what happens when the clock strikes midnight?" without having 
to wait for midnight -- then you probably don't need millisecond 
precision. If you do need millisecond precision -- why??? -- *and* expect 
to do it from a user-space application, you're going to have a bad time.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#29113

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-14 00:50 -0400
Message-ID<mailman.665.1347598205.27098.python-list@python.org>
In reply to#29106
On 14 Sep 2012 03:35:29 GMT, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> declaimed the following in
gmane.comp.python.general:

> 
> On Linux, Unix or Mac, that right way is to use NTP, which will keep your 
> computer's clock syncronised with a trusted external source. In a virtual 
> machine, the right way is to use NTP to syncronise the VM host's time, 
> and then tell the host to synchronise itself with the VM. On Windows, 
> well you'll have to ask a Windows expert.
>
	WinXP -- and I'd presume anything later -- can be configured for NTP
time server; synching tends to be on a weekly basis as I recall. Though
I hadn't checked my system in some time and the configuration may not be
valid after having moved 2000 miles and changed routers...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#29129

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-14 02:37 -0400
Message-ID<mailman.677.1347604666.27098.python-list@python.org>
In reply to#29106
On Fri, 14 Sep 2012 00:50:03 -0400, Dennis Lee Bieber
<wlfraed@ix.netcom.com> declaimed the following in
gmane.comp.python.general:

> 	WinXP -- and I'd presume anything later -- can be configured for NTP
> time server; synching tends to be on a weekly basis as I recall. Though
> I hadn't checked my system in some time and the configuration may not be
> valid after having moved 2000 miles and changed routers...

	Confusingly:

	If the system is part of Windows "domain", it synchronizes to a
domain server; otherwise,

	If the system is stand-alone workstation/laptop/etc. with internet
connectivity, it will synchronize (by default) to a Microsoft time
server, but can be switched to other servers. Opening the clock (from
the taskbar) should show an "internet time" tab which (admin) can be set
to the server, set for automatic sync, and forced to update.

	The confusion: my old WinXP Inside & Out says only XP Pro has it;
but my XP Home laptop shows it -- perhaps it was an update in one of the
service packs.

	Some documentation implies it uses just SNTP, but other
documentation claims full NTP (depending on OS version)

	NO obvious means to configure w32tm to sync more often than weekly,
it is documented to not be useful for time-critical networks -- it
exists to permit Kerberos to function.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web