Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
datetime
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Max  
View profile  
 More options Sep 13 2012, 11:40 am
Newsgroups: comp.lang.python
From: Max <read...@hushmail.com>
Date: Thu, 13 Sep 2012 15:19:32 +0000 (UTC)
Local: Thurs, Sep 13 2012 11:19 am
Subject: datetime
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Sep 13 2012, 11:43 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 14 Sep 2012 01:43:04 +1000
Local: Thurs, Sep 13 2012 11:43 am
Subject: Re: datetime

On Fri, Sep 14, 2012 at 1:19 AM, Max <read...@hushmail.com> wrote:
> 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.

Could you cheat and change the timezone offset? :D

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 13 2012, 1:08 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 13 Sep 2012 11:08:23 -0600
Local: Thurs, Sep 13 2012 1:08 pm
Subject: Re: datetime

On Thu, Sep 13, 2012 at 9:19 AM, Max <read...@hushmail.com> wrote:
> How do I set the time in Python?

On what platform?  I don't know of any libraries for this, so it would
be a matter of making the necessary system calls (which is all that a
library would do anyway).

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

Only by changing the timezone setting.  Any method of offsetting the
system clock itself is going to involve at some level reading the
current value, adding or subtracting, and then setting the new value.

> 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.

If you're concerned about individual seconds, then you probably should
do this in a low-level language like C.  Also, the clock is going to
drift naturally over time anyway.  How are you going to keep it in
sync if not with ntp?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Sep 13 2012, 2:08 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 13 Sep 2012 14:07:34 -0400
Local: Thurs, Sep 13 2012 2:07 pm
Subject: Re: datetime
On 9/13/2012 11:19 AM, Max wrote:

> How do I set the time in Python?

If you look up 'time' in the index of the current manual, it directs you
to the time module.

"time.clock_settime(clk_id, time)
Set the time of the specified clock clk_id.
Availability: Unix.
New in version 3.3."

You did not specify *which* time to set, but ...

"time.CLOCK_REALTIME
System-wide real-time clock. Setting this clock requires appropriate
privileges.
Availability: Unix.
New in version 3.3."

Chris already suggested an approach for changing your process's idea of
time. However, setting time.timezone seems to have no effect

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

If you mean time.clock_shift(clk_id, shift_seconds), no.

time.clock_settime(clk_id, time.clock_gettime(clk_id) + delta_seconds)

> Note that any "indirect" methods may need complicated ways to keep
> track of the milliseconds lost while running them.

Whay would a millisecond matter? System clocks are never synchronized to
official UTC time that closely without special hardware to receive time
broadcasts.

> 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.

The above should be well under a second.

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
readmax  
View profile  
 More options Sep 13 2012, 3:06 pm
Newsgroups: comp.lang.python
From: readmax <read...@hushmail.com>
Date: Thu, 13 Sep 2012 19:06:23 +0000 (UTC)
Local: Thurs, Sep 13 2012 3:06 pm
Subject: Re: datetime
Terry Reedy <tjreedy <at> udel.edu> writes:

> You did not specify *which* time to set, but ...

> If you mean time.clock_shift(clk_id, shift_seconds), no.

> time.clock_settime(clk_id, time.clock_gettime(clk_id) + delta_seconds)

I am talking about the system-wide clock on Debian.
What should I use as "clk_id"?

BTW, if by "version 3.3" you mean python 3, I am only using 2.x.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Sep 13 2012, 7:29 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 13 Sep 2012 19:28:35 -0400
Local: Thurs, Sep 13 2012 7:28 pm
Subject: Re: datetime
On 9/13/2012 3:06 PM, readmax wrote:

> Terry Reedy <tjreedy <at> udel.edu> writes:

>> You did not specify *which* time to set, but ...

>> If you mean time.clock_shift(clk_id, shift_seconds), no.

>> time.clock_settime(clk_id, time.clock_gettime(clk_id) + delta_seconds)

> I am talking about the system-wide clock on Debian.
> What should I use as "clk_id"?

Read the doc.

> BTW, if by "version 3.3" you mean python 3

x.y in the doc means pythonx.y

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Sep 13 2012, 11:35 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 14 Sep 2012 03:35:29 GMT
Local: Thurs, Sep 13 2012 11:35 pm
Subject: Re: datetime

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »