Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59195
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <joel.goldstick@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'else:': 0.03; 'argument': 0.05; '#if': 0.07; 'detect': 0.07; 'diff': 0.07; 'string': 0.09; 'alter': 0.09; 'dst': 0.09; 'skip:% 20': 0.09; 'sure)': 0.09; 'wrong,': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'andy': 0.16; 'declaration': 0.16; 'declared': 0.16; 'simple.': 0.16; 'substituted': 0.16; 'ubuntu.': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'properly': 0.19; 'entered': 0.20; 'solution.': 0.20; 'thanks.': 0.20; 'seems': 0.21; 'machine': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'skip:+ 40': 0.24; 'skip:+ 50': 0.24; 'switched': 0.24; 'test.': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'host': 0.29; 'correct': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; "doesn't": 0.30; 'start,': 0.30; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'lines': 0.31; 'another': 0.32; 'url:python': 0.33; 'running': 0.33; 'not.': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; 'knows': 0.35; 'possible.': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'format.': 0.36; 'joined': 0.36; 'url:listinfo': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'clear': 0.37; 'server': 0.38; 'nov': 0.38; 'little': 0.38; 'ability': 0.39; '12,': 0.39; 'itself': 0.39; 'sure': 0.39; 'mailing': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'manually': 0.60; 'took': 0.61; 'matter': 0.61; 'happen': 0.63; 'different': 0.65; 'great': 0.65; 'to:addr:gmail.com': 0.65; 'eastern': 0.68; 'winter': 0.68; 'saving': 0.69; '8bit%:92': 0.71; 'soon.': 0.71; '8bit%:100': 0.72; 'hey,': 0.75; 'greece': 0.84; 'greek': 0.84; 'joel': 0.91; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=GEZqoYa++9aTvg2565qaJJrqUsVK83N6tooogcyxaMo=; b=DbQ5HiVFaQlUiim3x0YwTdtfQt42dieckeJbiH+JzVeq817R+tkSpr2oYTfDL7ITQK m65ad9Nkw9EWrvmzasWN78QO1AU+a/L7cFD02P6/tQInDmdmuhbdR1UGF30cQJpfFDMd 4ZjKDITmEcXju3fgYZI9LteCDeqJyLv8X4O0qjM8+ZQACaSxlbM9h45GD7VwYmgiHrNC 1Ju4irYszp+fvh/g6AKZiFxRWbMGpKohwuf52Kk269eh8M3D/Xut38xKOUjvWOsOVRu3 0nIsQzGl51m8XY2IECjP4AgqzgHY1KMEoA4gzcanBFyWO9D3kCQmoFfifD+sQF3VAttp f4BQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.220.194.133 with SMTP id dy5mr23674344vcb.3.1384265029226; Tue, 12 Nov 2013 06:03:49 -0800 (PST) |
| In-Reply-To | <l5tam0$a2$2@dont-email.me> |
| References | <l5jk15$de$1@dont-email.me> <l5sv4a$a2$1@dont-email.me> <mailman.2459.1384260501.18130.python-list@python.org> <l5tam0$a2$2@dont-email.me> |
| Date | Tue, 12 Nov 2013 09:03:49 -0500 |
| Subject | Re: datetime question |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| To | Ferrous Cranus <nikos.gr33k@gmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | "python-list@python.org" <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2463.1384265037.18130.python-list@python.org> (permalink) |
| Lines | 153 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1384265037 news.xs4all.nl 16006 [2001:888:2000:d::a6]:40767 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:59195 |
Show key headers only | View raw
On Tue, Nov 12, 2013 at 8:32 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
> Στις 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 <nikos.gr33k@gmail.com
>> <mailto:nikos.gr33k@gmail.com>> wrote:
>>
>> Στις 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.
>> --
>> https://mail.python.org/__mailman/listinfo/python-list
>> <https://mail.python.org/mailman/listinfo/python-list>
>>
>>
>
>
> 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
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
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
----------------------
--
Joel Goldstick
http://joelgoldstick.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-08 23:11 +0200
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 21:29 +0000
Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 00:01 +0200
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-08 17:06 -0500
Re: datetime question rurpy@yahoo.com - 2013-11-11 14:49 -0800
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 18:18 -0500
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 22:18 +0000
Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-08 22:29 +0000
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 09:39 +1100
Re: datetime question Roy Smith <roy@panix.com> - 2013-11-09 10:30 -0500
Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-09 10:39 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-10 09:25 +1100
Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-08 22:49 +0000
Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 01:02 +0200
Re: datetime question Robert Kern <robert.kern@gmail.com> - 2013-11-08 23:23 +0000
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 10:30 +1100
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-09 00:12 +0000
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-09 12:45 +0000
Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 15:43 +0200
Re: datetime question unknown <unknown@unknown.com> - 2013-11-10 12:52 +0000
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 23:52 +1100
Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-09 19:55 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-10 19:47 +1100
Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-10 04:13 -0500
Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-11 11:57 +0200
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 08:41 -0500
Re: datetime question Roy Smith <roy@panix.com> - 2013-11-11 09:14 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 01:25 +1100
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 09:36 -0500
Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-11 20:59 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 13:02 +1100
Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-11 21:02 -0500
Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:30 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 11:40 +1100
Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-11 19:19 +0000
Re: datetime question Ethan Furman <ethan@stoneleaf.us> - 2013-11-11 11:39 -0800
Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-11 20:45 +0000
Re: datetime question mm0fmf <none@mailinator.com> - 2013-11-11 23:21 +0000
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-11 23:48 +0000
Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-12 04:49 +0000
Re: datetime question alex23 <wuwei23@gmail.com> - 2013-11-12 17:25 +1000
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 18:35 +1100
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 09:20 +0000
Re: datetime question Roy Smith <roy@panix.com> - 2013-11-12 09:18 -0500
Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 12:15 +0200
Re: datetime question Andy Lawton <andy@andylawton.eu> - 2013-11-12 12:47 +0000
Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 15:32 +0200
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-12 09:03 -0500
Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 16:12 +0200
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 01:57 +1100
Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 17:24 +0200
Re: datetime question Tim Chase <python.list@tim.thechases.com> - 2013-11-12 09:54 -0600
Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 17:57 +0200
Re: datetime question Tim Chase <python.list@tim.thechases.com> - 2013-11-12 10:12 -0600
Re: datetime question William Ray Wing <wrw@mac.com> - 2013-11-12 11:14 -0500
Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 16:30 +0000
Re: datetime question MRAB <python@mrabarnett.plus.com> - 2013-11-12 19:09 +0000
Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-12 14:14 -0500
Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-12 19:24 +0000
Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:38 -0500
Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 09:42 +1100
Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:37 -0500
Re: datetime question MRAB <python@mrabarnett.plus.com> - 2013-11-13 01:08 +0000
Re: datetime question Alister <alister.ware@ntlworld.com> - 2013-11-12 16:11 +0000
Re: datetime question Roy Smith <roy@panix.com> - 2013-11-12 19:43 -0500
Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-12 19:20 +0000
csiph-web