# DateTime about the zone

**URL:** <https://www.ruby-forum.com/t/datetime-about-the-zone/190061>\
**Category:** Ruby\
**Created:** [June 12, 2010, 10:20am UTC](https://www.ruby-forum.com/t/datetime-about-the-zone/190061 "2010-06-12T10:20:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [June 12, 2010, 10:20am UTC](https://www.ruby-forum.com/t/datetime-about-the-zone/190061/1 "2010-06-12T10:20:41Z")

</div>

i’m reading a news server from ruby asking for DATE, it returns :  
“20100612” for the date, that’s ok for today  
and :  
“073429” for the time when it is 2 hours more

i want to return a correct datetime, taking into account the zone  
5paris/france) i think it is + 2 H ?

but using datetime and the following :

text = [“20100612”, “073429”] # returned by the server thru Net::NNTP

i translate to =  
datetime\_str =  
text[0].gsub(/(\d\d\d\d)(\d\d)(\d\d)/,’\1-\2-\3’)+‘T’+text[1].gsub(/(\d  
d)(\d\d)(\d\d)/,’\1:\2:\3’)+’-02:00’

giving :

datetime\_str = ‘2010-06-12T07:34:29+02:00’

if i read back the hour by :

datetime = DateTime.parse(‘2010-06-12T07:34:29+02:00’)

time = adatetime.strftime(fmt=’%H%M%S’)

i get always :  
073429  
regardless of the time decay i put by the end of string (ie ‘+02:00’ or  
‘-02:00’ or ‘CEST’

clearly i don’t understand…

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [June 12, 2010, 11:04am UTC](https://www.ruby-forum.com/t/datetime-about-the-zone/190061/2 "2010-06-12T11:04:07Z")

</div>

Une BÃ©vue wrote:

> datetime = DateTime.parse(‘2010-06-12T07:34:29+02:00’)
> 
> time = adatetime.strftime(fmt=’%H%M%S’)
> 
> i get always :  
> 073429  
> regardless of the time decay i put by the end of string (ie ‘+02:00’ or  
> ‘-02:00’ or ‘CEST’
> 
> clearly i don’t understand…

> > datetime = DateTime.parse(‘2010-06-12T07:34:29+02:00’)  
> > =\> #\<DateTime: 212143080869/86400,1/12,2299161\>
> 
> > datetime.offset  
> > =\> Rational(1, 12)
> 
> > datetime.offset \* 24.0  
> > =\> 2.0
> 
> > datetime = DateTime.parse(‘2010-06-12T07:34:29+03:00’)  
> > =\> #\<DateTime: 212143077269/86400,1/8,2299161\>
> 
> > datetime.offset  
> > =\> Rational(1, 8)
> 
> > datetime.offset \* 24.0  
> > =\> 3.0
> 
> > datetime = DateTime.parse(‘2010-06-12T07:34:29-02:00’)  
> > =\> #\<DateTime: 212143095269/86400,-1/12,2299161\>
> 
> > datetime.offset  
> > =\> Rational(-1, 12)
> 
> > datetime.offset \* 24.0  
> > =\> -2.0
> 
> > datetime.strftime("%H%M%S")  
> > =\> “073429”
> 
> > datetime.strftime("%H%M%S %z")  
> > =\> “073429 -0200”

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [June 12, 2010, 11:08am UTC](https://www.ruby-forum.com/t/datetime-about-the-zone/190061/3 "2010-06-12T11:08:20Z")

</div>

Une BÃ©vue wrote:

> i’m reading a news server from ruby asking for DATE, it returns :  
> “20100612” for the date, that’s ok for today  
> and :  
> “073429” for the time when it is 2 hours more

You mean it’s 093429 local time in France?

That means that 073429 is UTC.

> > datetime = DateTime.parse(‘2010-06-12T07:34:29’)  
> > =\> #\<DateTime: 212143088069/86400,0,2299161\>

I don’t know how to ask a DateTime to change to the local timezone, but  
I can do it with Time:

> > require ‘time’  
> > =\> true
> 
> > time = Time.parse(‘2010-06-12T07:34:29Z’)  
> > =\> Sat Jun 12 07:34:29 UTC 2010
> 
> > time.to\_s  
> > =\> “Sat Jun 12 07:34:29 UTC 2010”
> 
> > time.getlocal  
> > =\> Sat Jun 12 08:34:29 +0100 2010

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [June 12, 2010, 11:35am UTC](https://www.ruby-forum.com/t/datetime-about-the-zone/190061/4 "2010-06-12T11:35:36Z")

</div>

Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You mean it’s 093429 local time in France?

yes, it was )))

in France it’s UTC + 2 hours now.

> =\> true
> 
> > > time = Time.parse(‘2010-06-12T07:34:29Z’)  
> > > =\> Sat Jun 12 07:34:29 UTC 2010  
> > > time.to\_s  
> > > =\> “Sat Jun 12 07:34:29 UTC 2010”  
> > > time.getlocal  
> > > =\> Sat Jun 12 08:34:29 +0100 2010

Thanks, fine, seems to be easier to do with Time than with DateTime  
(strange enough!)

even i’ve corrected datetime with tzinfo, gave the correct hour with a  
false time zone, strange…

here the datas :  
utc\_datetime\_str = ‘2010-06-12T07:34:29UTC’  
tz = TZInfo::Timezone.get(‘Europe/Paris’)  
local = tz.utc\_to\_local(utc\_datetime)

# → local = 2010-06-12T09:34:29+00:00

here the hour is correct BUT the time zone :  
local\_zone = local.strftime(fmt=‘%z’)

# → local\_zone = local.strftime(fmt=‘%z’) = +0000

I’ll switch to Time )))
