datetime question

  • Thread starter Îίκος Αλεξόπουλος
  • Start date
J

Joel Goldstick

Στις 8/11/2013 11:11 μμ, ο/η Îίκος Αλεξόπουλος έγÏαψε:




Someone has an idea what to add to this line to automatically adjust itself

Three days ago Mark Lawrence pointed you to a stack overflow answer to
your question. While it didn't suit your demand for a single line
answer, it could easily be made into a function which returned the
string format that you requested. You've been around long enough to
try to work that out for yourself. If you yourself had returned with
a one liner to perform what Mark showed you, I can imagine a lot of
people would be impressed at your dedication!

I think you are oversimplifying the problem. I recall that you server
is in Germany, and you are in Greece. If these are two different time
zones, I am guessing that the German time will be given. Also, any
linux user who runs package updates regularly (thinking Ubuntu)
realizes that some file that starts with tz gets changed quite often.
This is because time zones are set by governments, and the date of
change from standard to daylight time can be differently, depending
upon the location and the governing body.

I pose this question to you:

Why not display UTC? If it is so important to you to display local
time, why do you think that your host's local time is something that
is useful for a visitor? Daylight savings time is more complicated
that you have suggested
if DST happens?

If you insist upon displaying the local time of your server, you
should not be hardcoding in a 2 hour difference. Let the python
library figure out the delta by giving the country code
 
R

Roy Smith

Joel Goldstick said:
Why not display UTC? If it is so important to you to display local
time, why do you think that your host's local time is something that
is useful for a visitor?

In general, it makes sense to run servers (and log everything) in UTC,
and display local time to your users. The idea is to do the conversion
(in both directions) as close to the user as possible (which, for a web
application, means a little piece of javascript right in the browser).

It seems like extra work, but, trust me, it will preserve your sanity
eventually.

We've got a data supplier who (for reasons I cannot fathom), runs their
network in local time. Every time we talk to them about problems, it's
a mess just trying to figure out what time we're talking about. We say,
"we saw a latency spike at 2243 UTC, what caused that?", and then we
play 20-questions trying to figure out if that's 1443 PDT, or 1543 PST,
or who knows what. Further complicated by them occasionally asking us
off-the-wall questions like, "Is that UTC daylight savings time?"
 
C

Chris Angelico

We've got a data supplier who (for reasons I cannot fathom), runs their
network in local time. Every time we talk to them about problems, it's
a mess just trying to figure out what time we're talking about. We say,
"we saw a latency spike at 2243 UTC, what caused that?", and then we
play 20-questions trying to figure out if that's 1443 PDT, or 1543 PST,
or who knows what. Further complicated by them occasionally asking us
off-the-wall questions like, "Is that UTC daylight savings time?"

UGH. Yes. I get the same thing from some of the US players in my
Dungeons & Dragons campaign. To avoid the hassles of international
times, everything's scheduled in UTC; the Aussies, the Europeans, and
_some of_ the Americans an handle it, but there are a few who keep
asking me crazy questions about DST and UTC. I'm like... no, there is
no daylight time! Just look at the current UTC time and see how that
compares! There's even a command in-game that shows current UTC time.

Fortunately, those people are in the minority. Most of the players can
handle the time conversions.

ChrisA
 
J

Joel Goldstick

So this is a physics joke. The engineers and physicists at the
conference went to dinner. They ordered wine with dinner. The wait
person asked: "Would you like the small liter, or the large liter?"
 
D

Denis McMahon

Someone has an idea what to add to this line to automatically adjust
itself if DST happens?

Yes, but the scarcity of universal whitespace is preventing me from
posting it.
 
E

Ethan Furman

Yes, but the scarcity of universal whitespace is preventing me from
posting it.

Boy, are you in luck! I just happen to have a whole heap of marked down ascii white-space at bargain basement prices!
 
G

Grant Edwards

Boy, are you in luck! I just happen to have a whole heap of marked
down ascii white-space at bargain basement prices!

Careful, a lot of the cheap imported whitespace was made with toxic
lead carbonate instead of the non-toxic titatium oxide that should be
used...
 
R

rurpy

rurpy? can you help?

No, sorry. For your future reference, if there is a
question I can help with (have the technical knowledge,
haven't seen a good answer yet, have time, etc) I will
post my attempt at an answer.

So lack of such a response is a pretty good clue that
I can't help at the current time, and there is no need
to ping me explicitly.

I do appreciate your "genuine" concern for Nikos and
that he gets a good answer though.
 
J

Joel Goldstick

No, sorry. For your future reference, if there is a
question I can help with (have the technical knowledge,
haven't seen a good answer yet, have time, etc) I will
post my attempt at an answer.

So lack of such a response is a pretty good clue that
I can't help at the current time, and there is no need
to ping me explicitly.

I guess the word ping can make sense as to bring attention to a
particular person. I believed I responded on the mailing list.
I do appreciate your "genuine" concern for Nikos and
that he gets a good answer though.

So, since you don't like to be emailed directly, (I'm guessing), I
will respond here.

Its nice that an occasional participant sticks up and sees the bright
of side of that particular OP. My answer was an attempt at humor,
specifically for you Rurpy, since after your splendid defense of the
OP, he returned to the pattern of asking a question, getting a good
answer, then replying with a reply like "no, I want a one liner".
Then, starting a new thread and asking the same question again.

I don't think the idea of a mailing list or news group is to spoon
feed anyone code. That is what you pay someone to do. If you want
advice and guidance, its polite to try to learn from it, and move the
conversation forward.

have a nice day
 
M

mm0fmf

Boy, are you in luck! I just happen to have a whole heap of marked down
ascii white-space at bargain basement prices!

Is that metric or imperial whitespace?

You need metric whitespace for programs written in Europe.
 
D

Dennis Lee Bieber

UGH. Yes. I get the same thing from some of the US players in my
Dungeons & Dragons campaign. To avoid the hassles of international
times, everything's scheduled in UTC; the Aussies, the Europeans, and
_some of_ the Americans an handle it, but there are a few who keep
asking me crazy questions about DST and UTC. I'm like... no, there is
no daylight time! Just look at the current UTC time and see how that
compares! There's even a command in-game that shows current UTC time.

Fortunately, those people are in the minority. Most of the players can
handle the time conversions.

Let's add astronomical Julian Dates... Where the "day" changes at
"noon" (avoids the hassle of an observation starting at 2300 <day x> and
ending at 0100 <day y) -- both times would still be in <day x>

Of course, I'm spoiled... My /watch/ has a dial for UTC, along with one
for 24-hour indication (one hand, range 1 to 24)
 
D

Dennis Lee Bieber

So this is a physics joke. The engineers and physicists at the
conference went to dinner. They ordered wine with dinner. The wait
person asked: "Would you like the small liter, or the large liter?"

Pardon? Makes no sense to me...

A question about small pint or large pint, OTOH...
 
C

Chris Angelico

Of course, I'm spoiled... My /watch/ has a dial for UTC, along with one
for 24-hour indication (one hand, range 1 to 24)

Heh. Mine doesn't, so I bought myself a second watch and set it to
UTC. So my left hand has local time (changes at DST breaks, changes
when I travel internationally), and my right hand has UTC (never
changes).

ChrisA
 
G

Grant Edwards

Except for the UK, our multi-national approach means that both imperial
and metric whitespace are acceptable.

Don't forget that there are also some differences between American and
Imperial whitespace. Since it's ASCII whitespace, you should probably
assume American...
 
A

alex23

Don't forget that there are also some differences between American and
Imperial whitespace. Since it's ASCII whitespace, you should probably
assume American...
52

bad by design
 
F

Ferrous Cranus

Στις 8/11/2013 11:11 μμ, ο/η Îίκος Αλεξόπουλος έγÏαψε:
Is there someway to write the following line even better with the
ability to detect daylight saving time by itself so i don't have to
alter the line manually when time changes?

lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
'%y-%m-%d %H:%M:%S' ) # MySQL datetime format

Thanks.


Example:

#check if date entered as intented, format it properly for MySQL
lastvisit = datetime.strptime(lastvisit, '%d %m %Y').strftime('%Y-%m-%d')

The above code gets the datetime string in a specific way and then
convert it to another format. All happens in one line clear and simple.

Now, in the following statement there shopuld be some way to check for
daylight saving time so i would aboiut add +2 in winter or +3 for greek
summer time.

lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
'%y-%m-%d %H:%M:%S' ) # MySQL datetime format

I think + timedelta(hours=2) should be substituted with something else
but i don't with what.
 
A

Andy Lawton

Firstly , I should clarify I have no idea how to program python, I joined
this mailing list in anticipation of learning soon. And thought I'd have a
go playing around with your code and code given to you (worst possible
place to start, I'm sure)

But from the answers already given to you, this seems to work and as you
requested, in reality, this keeps your code to 1 line...

I'm sure this is wrong, and isn't what you want, but hey, Correct working
code doesn't seem to matter to you anyway...

(I think "Europe/Kiev" is Greece but I don't know)

from datetime import datetime, time, timedelta
import time
import pytz

def is_dst(zonename):
tz = pytz.timezone(zonename)
now = pytz.utc.localize(datetime.utcnow())
return now.astimezone(tz).dst() != timedelta(0)

def dst_greece():
if is_dst("Europe/Kiev") :
diff = 2
else:
diff = 3
return diff

lastvisit = (datetime.utcnow() +timedelta(hours=dst_greece())).strftime(
'%y-%m-%d %H:%M:%S' )
 
F

Ferrous Cranus

Στις 12/11/2013 2:47 μμ, ο/η Andy Lawton έγÏαψε:
Firstly , I should clarify I have no idea how to program python, I
joined this mailing list in anticipation of learning soon. And
thought I'd have a go playing around with your code and code given to
you (worst possible place to start, I'm sure)

But from the answers already given to you, this seems to work and as you
requested, in reality, this keeps your code to 1 line...

I'm sure this is wrong, and isn't what you want, but hey, Correct
working code doesn't seem to matter to you anyway...

(I think "Europe/Kiev" is Greece but I don't know)

from datetime import datetime, time, timedelta
import time
import pytz

def is_dst(zonename):
tz = pytz.timezone(zonename)
now = pytz.utc.localize(datetime.utcnow())
return now.astimezone(tz).dst() != timedelta(0)

def dst_greece():
if is_dst("Europe/Kiev") :
diff = 2
else:
diff = 3
return diff

lastvisit = (datetime.utcnow() +timedelta(hours=dst_greece())).strftime(
'%y-%m-%d %H:%M:%S' )


On 12 November 2013 10:15, Ferrous Cranus <[email protected]

Στις 8/11/2013 11:11 μμ, ο/η Îίκος Αλεξόπουλος έγÏαψε:

Is there someway to write the following line even better with the

ability to detect daylight saving time by itself so i don't have to
alter the line manually when time changes?

lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
'%y-%m-%d %H:%M:%S' ) # MySQL datetime format

Thanks.



Example:

#check if date entered as intented, format it properly for MySQL
lastvisit = datetime.strptime(lastvisit, '%d %m
%Y').strftime('%Y-%m-%d')

The above code gets the datetime string in a specific way and then
convert it to another format. All happens in one line clear and simple.

Now, in the following statement there shopuld be some way to check
for daylight saving time so i would aboiut add +2 in winter or +3
for greek summer time.


lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
'%y-%m-%d %H:%M:%S' ) # MySQL datetime format

I think + timedelta(hours=2) should be substituted with something
else but i don't with what.


this requires a function to be declared while i just want just an
argument inside timedelta( ) that will automatically detect DST or not.

No function declaration but instead something like:
lastvisit = ( datetime.utcnow() + timedelta( some_arg_here_that_can
detect greece's_DST_or_not) ).strftime) '%y-%m-%d %H:%M:%S' )
# MySQL datetime format
 
J

Joel Goldstick

Στις 12/11/2013 2:47 μμ, ο/η AndyLawton έγÏαψε:


this requires a function to be declared while i just want just an argument
inside timedelta( ) that will automatically detect DST or not.

No function declaration but instead something like:
lastvisit = ( datetime.utcnow() + timedelta( some_arg_here_that_can detect
greece's_DST_or_not) ).strftime) '%y-%m-%d %H:%M:%S' ) # MySQL
datetime format

I think that is a great solution. What would happen if you just did this:
lastvisit2 = (datetime.now()).strftime('%y-%m-%d %H:%M:%S' )

Why don't you try it out and see what you find?

(PS, this was pointed out to me by a pro, so I did a little reading in
the python docs and took Andy's excellent code and added a couple of
lines to test.)

I switched to my local timezone "US/Eastern" to test. My machine is
running Ubuntu. It seems to know that it is in the Eastern US. If
your server knows where it lives, then this should work for you also.
If it is set to a different timezone, I'm not sure I could help you,
but I'm sure someone could. You even might ask your host if this is
possible.

----------------------
#! /usr/bin/env python

from datetime import datetime, time, timedelta
import time
import pytz

def is_dst(zonename):
tz = pytz.timezone(zonename)
now = pytz.utc.localize(datetime.utcnow())
return now.astimezone(tz).dst() != timedelta(0)

def dst_greece():
#if is_dst("Europe/Kiev") :
if is_dst("US/Eastern") :
diff = -6
else:
diff = -5
return diff

lastvisit = (datetime.utcnow()
+timedelta(hours=dst_greece())).strftime('%y-%m-%d %H:%M:%S' )

lastvisit2 = (datetime.now()).strftime('%y-%m-%d %H:%M:%S' )

print lastvisit
print lastvisit2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top