Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > datetime issue

Reply
Thread Tools

datetime issue

 
 
Guest
Posts: n/a
 
      09-15-2012
Hello again,

one small matter too.

# get some enviromental values
locale.setlocale(locale.LC_ALL, 'el_GR')

...........
...........

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


although iam setting greek as locale the time is 8 hours before, like in texas, us

How can i change this to save the correct Greek time in variable $date ?

Thank you.
 
Reply With Quote
 
 
 
 
Jason Friedman
Guest
Posts: n/a
 
      09-15-2012
> # get some enviromental values
> locale.setlocale(locale.LC_ALL, 'el_GR')
> date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )


>
> although iam setting greek as locale the time is 8 hours before, like in texas, us
> How can i change this to save the correct Greek time in variable $date ?


http://docs.python.org/py3k/library/...tzinfo-objects
http://stackoverflow.com/questions/1...ject-in-python
http://pytz.sourceforge.net/

Reading these links suggests that datetime.now() will return the
system time regardless of your locale setting.
Use a timezone to generate a timestamp for someplace other than where
your server sits.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      09-15-2012
On Sat, Sep 15, 2012 at 10:33 AM, Νικόλαος Κούρας <(E-Mail Removed)> wrote:
> Hello again,
>
> one small matter too.
>
> # get some enviromental values
> locale.setlocale(locale.LC_ALL, 'el_GR')

<snip>
> date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )
>
> although iam setting greek as locale


Locales don't affect timezones. Otherwise, how would expatriate
communities, or countries wide enough to span several timezones,
properly configure their software?

> the time is 8 hours before, like in texas, us


Which is where HostGator operates out of.

> How can i change this to save the correct Greek time in variable $date ?


Use the `pytz` package that Jason pointed out.

Cheers,
Chris
 
Reply With Quote
 
Guest
Posts: n/a
 
      09-15-2012
, 15 2012 10:05:49 .. UTC+3, Chris Rebert :
> On Sat, Sep 15, 2012 at 10:33 AM, <(E-Mail Removed)> wrote:
>
> > Hello again,

>
> >

>
> > one small matter too.

>
>
>
> > # get some enviromental values

>
> > locale.setlocale(locale.LC_ALL, 'el_GR')

>
> <snip>
>
> > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )

>
> >

>
> > although iam setting greek as locale

>
>
>
> Locales don't affect timezones. Otherwise, how would expatriate
>
> communities, or countries wide enough to span several timezones,
>
> properly configure their software?
>
>
>
> > the time is 8 hours before, like in texas, us

>
>
>
> Which is where HostGator operates out of.
>
>
>
> > How can i change this to save the correct Greek time in variable $date ?

>
>
>
> Use the `pytz` package that Jason pointed out.
>
>
>
> Cheers,
>
> Chris


I did read but wasnt able to set it to greek time.
Please tell me how should i write this.

date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S',gmt+2 )

didnt work out for me.
 
Reply With Quote
 
Guest
Posts: n/a
 
      09-15-2012
, 15 2012 10:05:49 .. UTC+3, Chris Rebert :
> On Sat, Sep 15, 2012 at 10:33 AM, <(E-Mail Removed)> wrote:
>
> > Hello again,

>
> >

>
> > one small matter too.

>
>
>
> > # get some enviromental values

>
> > locale.setlocale(locale.LC_ALL, 'el_GR')

>
> <snip>
>
> > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )

>
> >

>
> > although iam setting greek as locale

>
>
>
> Locales don't affect timezones. Otherwise, how would expatriate
>
> communities, or countries wide enough to span several timezones,
>
> properly configure their software?
>
>
>
> > the time is 8 hours before, like in texas, us

>
>
>
> Which is where HostGator operates out of.
>
>
>
> > How can i change this to save the correct Greek time in variable $date ?

>
>
>
> Use the `pytz` package that Jason pointed out.
>
>
>
> Cheers,
>
> Chris


I did read but wasnt able to set it to greek time.
Please tell me how should i write this.

date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S',gmt+2 )

didnt work out for me.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-15-2012
On 2012-09-15 20:26, wrote:
> , 15 2012 10:05:49 .. UTC+3, Chris Rebert :
>> On Sat, Sep 15, 2012 at 10:33 AM, <(E-Mail Removed)> wrote:
>> > Hello again,
>> >
>> > one small matter too.

>>
>> > # get some enviromental values
>> > locale.setlocale(locale.LC_ALL, 'el_GR')

>> <snip>
>> > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )
>> >
>> > although iam setting greek as locale

>>
>> Locales don't affect timezones. Otherwise, how would expatriate
>> communities, or countries wide enough to span several timezones,
>> properly configure their software?
>>
>> > the time is 8 hours before, like in texas, us

>>
>> Which is where HostGator operates out of.
>>
>> > How can i change this to save the correct Greek time in variable

$date ?
>>
>> Use the `pytz` package that Jason pointed out.
>>

> I did read but wasnt able to set it to greek time.
> Please tell me how should i write this.
>
> date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S',gmt+2 )
>
> didnt work out for me.
>

Does this help?

import datetime

# The time as UTC (GMT).
now_utc = datetime.datetime.utcnow()
print(now_utc.strftime('%Y-%m-%d %H:%M:%S'))

# The time as UTC+2.
now_local = now_utc + datetime.timedelta(hours=2)
print(now_local.strftime('%Y-%m-%d %H:%M:%S'))

 
Reply With Quote
 
Guest
Posts: n/a
 
      09-16-2012
, 15 2012 11:28:47 .. UTC+3, MRAB :
> On 2012-09-15 20:26, �������� ������ wrote:
>
> > �� �������, 15 ����������� 2012 10:05:49 �.�. UTC+3, � ������� Chris Rebert ������:

>
> >> On Sat, Sep 15, 2012 at 10:33 AM, �������� ������ <(E-Mail Removed)> wrote:

>
> >> > Hello again,

>
> >> >

>
> >> > one small matter too.

>
> >>

>
> >> > # get some enviromental values

>
> >> > locale.setlocale(locale.LC_ALL, 'el_GR')

>
> >> <snip>

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

>
> >> >

>
> >> > although iam setting greek as locale

>
> >>

>
> >> Locales don't affect timezones. Otherwise, how would expatriate

>
> >> communities, or countries wide enough to span several timezones,

>
> >> properly configure their software?

>
> >>

>
> >> > the time is 8 hours before, like in texas, us

>
> >>

>
> >> Which is where HostGator operates out of.

>
> >>

>
> >> > How can i change this to save the correct Greek time in variable

>
> $date ?
>
> >>

>
> >> Use the `pytz` package that Jason pointed out.

>
> >>

>
> > I did read but wasnt able to set it to greek time.

>
> > Please tell me how should i write this.

>
> >

>
> > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S',gmt+2 )

>
> >

>
> > didnt work out for me.

>
> >

>
> Does this help?
>
>
>
> import datetime
>
>
>
> # The time as UTC (GMT).
>
> now_utc = datetime.datetime.utcnow()
>
> print(now_utc.strftime('%Y-%m-%d %H:%M:%S'))
>
>
>
> # The time as UTC+2.
>
> now_local = now_utc + datetime.timedelta(hours=2)
>
> print(now_local.strftime('%Y-%m-%d %H:%M:%S'))


If i wanted to alter the following line, how would i write it?

date = datetime.datetime.now()+datetime.timedelta(hours=2 ).strftime( '%y-%m-%d %H:%M:%S')

But that doesnt work,
 
Reply With Quote
 
Guest
Posts: n/a
 
      09-16-2012
, 15 2012 11:28:47 .. UTC+3, MRAB :
> On 2012-09-15 20:26, �������� ������ wrote:
>
> > �� �������, 15 ����������� 2012 10:05:49 �.�. UTC+3, � ������� Chris Rebert ������:

>
> >> On Sat, Sep 15, 2012 at 10:33 AM, �������� ������ <(E-Mail Removed)> wrote:

>
> >> > Hello again,

>
> >> >

>
> >> > one small matter too.

>
> >>

>
> >> > # get some enviromental values

>
> >> > locale.setlocale(locale.LC_ALL, 'el_GR')

>
> >> <snip>

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

>
> >> >

>
> >> > although iam setting greek as locale

>
> >>

>
> >> Locales don't affect timezones. Otherwise, how would expatriate

>
> >> communities, or countries wide enough to span several timezones,

>
> >> properly configure their software?

>
> >>

>
> >> > the time is 8 hours before, like in texas, us

>
> >>

>
> >> Which is where HostGator operates out of.

>
> >>

>
> >> > How can i change this to save the correct Greek time in variable

>
> $date ?
>
> >>

>
> >> Use the `pytz` package that Jason pointed out.

>
> >>

>
> > I did read but wasnt able to set it to greek time.

>
> > Please tell me how should i write this.

>
> >

>
> > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S',gmt+2 )

>
> >

>
> > didnt work out for me.

>
> >

>
> Does this help?
>
>
>
> import datetime
>
>
>
> # The time as UTC (GMT).
>
> now_utc = datetime.datetime.utcnow()
>
> print(now_utc.strftime('%Y-%m-%d %H:%M:%S'))
>
>
>
> # The time as UTC+2.
>
> now_local = now_utc + datetime.timedelta(hours=2)
>
> print(now_local.strftime('%Y-%m-%d %H:%M:%S'))


If i wanted to alter the following line, how would i write it?

date = datetime.datetime.now()+datetime.timedelta(hours=2 ).strftime( '%y-%m-%d %H:%M:%S')

But that doesnt work,
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-16-2012
On Sat, 15 Sep 2012 22:15:38 -0700 (PDT),
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:


>
> If i wanted to alter the following line, how would i write it?
>
> date = datetime.datetime.now()+datetime.timedelta(hours=2 ).strftime( '%y-%m-%d %H:%M:%S')
>
> But that doesnt work,


What did you expect? Object methods bind tighter than operators so
what you have is the equivalent of

dn = datetime.datetime.now()
dd = datetime.timedelta(hours=2).strftime(...)
date = dn + dd

Try

>>> import datetime
>>> date = datetime.datetime.now()+datetime.timedelta(hours=2 ).strftime( '%y-%m-%d %H:%M:%S')

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'datetime.timedelta' object has no attribute 'strftime'
>>> date = (datetime.datetime.now()+datetime.timedelta(hours= 2) ).strftime( '%y-%m-%d %H:%M:%S')
>>> date

'12-09-16 03:50:44'
>>>


Note the ( ) wrapping the the + clause, with strftime() applied to
the result of that...
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Guest
Posts: n/a
 
      09-16-2012
dn = datetime.datetime.now()
dd = datetime.timedelta(hours=2)
date = dn + dd
date = date.strftime( '%y-%m-%d %H:%M:%S' )

still giving me texas,us time for some reason
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments