File.mtime <=> DateTime.strptime()

H

Hugh Sasse

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use

STRFTIME_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
timestamp = DateTime.strptime(lastmod, STRFTIME_FORMAT)

But I can't compare this with the results of File.mtime, because one
is a DateTime, and one is a Time. Time::strptime or Time::strftime
don't exist. Time#strftime does, but I can't pass a string to be
turned into a time into it. Parsedate deosn't seem to handle this
case, either in so far as it has no str[fp]time.

Is there a cunning way to do this I have missed? Do I need another
library and if so, what?

Thank you,
Hugh
 
A

Ara.T.Howard

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use

STRFTIME_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
timestamp = DateTime.strptime(lastmod, STRFTIME_FORMAT)

But I can't compare this with the results of File.mtime, because one
is a DateTime, and one is a Time. Time::strptime or Time::strftime
don't exist. Time#strftime does, but I can't pass a string to be
turned into a time into it. Parsedate deosn't seem to handle this
case, either in so far as it has no str[fp]time.

Is there a cunning way to do this I have missed? Do I need another
library and if so, what?

Thank you,
Hugh

this might help...

harp:~ > cat a.rb
require 'alib'

now = Time::now
p now

now_s = ALib::Util::timestamp now
p now_s

p ALib::Util::stamptime(now_s, 'local' => true)
harp:~ > ruby a.rb


Thu Sep 08 12:31:26 MDT 2005
"2005-09-08 18:31:26.812671"
Thu Sep 08 18:31:26 MDT 2005

dunno though... without the 'local' it assumes utc.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
H

Hugh Sasse

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use [...]
But I can't compare this with the results of File.mtime, because one
is a DateTime, and one is a Time. Time::strptime or Time::strftime

this might help...

harp:~ > cat a.rb
require 'alib'

What is alib?
dunno though... without the 'local' it assumes utc.

I think it depends on whenther I can get both times into the same
form.
cheers.

-a

Thank you
Hugh
 
A

Ara.T.Howard

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use [...]
But I can't compare this with the results of File.mtime, because one
is a DateTime, and one is a Time. Time::strptime or Time::strftime

this might help...

harp:~ > cat a.rb
require 'alib'

What is alib?

ara's lib ;-) it's a holding place for all the utility methods i don't end up
putting anywhere else. i also have inlined some small, but very useful code
developed by other people like satoru takabayashi's bsearch code for built-in
arrays. it's a mix of useful stuff like

- Util : lot's of time/string/process/filesystem/tmpdir-file/hash
manipulation routines

- Main : utilities for whipping out simple and configurable (via yaml file)
main programs that support usage, logging, command line and option parsing
in seconds

- Bsearch : bsearch support for arrays

- OrderedHash : name says it all

basically it's my personal stash of stuff i can't live without. get it here

http://codeforpeople.com/lib/ruby/alib/

I think it depends on whenther I can get both times into the same form.

to form those methods use is iso-8601 - should be a peice of cake.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
H

Hugh Sasse

What is alib?

ara's lib ;-) it's a holding place for all the utility methods i don't end [...]
arrays. it's a mix of useful stuff like

- Util : lot's of time/string/process/filesystem/tmpdir-file/hash
manipulation routines

- Main : utilities for whipping out simple and configurable (via yaml file)
main programs that support usage, logging, command line and option
parsing
in seconds

- Bsearch : bsearch support for arrays

- OrderedHash : name says it all

These sound useful, thank you.
basically it's my personal stash of stuff i can't live without. get it here

http://codeforpeople.com/lib/ruby/alib/

Thank you for this.
s/nth/th/ :)
to form those methods use is iso-8601 - should be a peice of cake.

And Google waves in the direction of

http://www.cl.cam.ac.uk/~mgk25/iso-time.html

Which it turns out I have linked to before!
cheers.

-a

Thank you,
Hugh
 
E

Eric Hodel

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use

STRFTIME_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
timestamp = DateTime.strptime(lastmod, STRFTIME_FORMAT)

require 'time'
timestamp = Time.parse lastmod
But I can't compare this with the results of File.mtime, because one
is a DateTime, and one is a Time. Time::strptime or Time::strftime
don't exist. Time#strftime does, but I can't pass a string to be
turned into a time into it. Parsedate deosn't seem to handle this
case, either in so far as it has no str[fp]time.

Time.parse Just Works.
Is there a cunning way to do this I have missed? Do I need another
library and if so, what?

I tend to avoid Date and DateTime unless I need them.
 
H

Hugh Sasse

I'm trying to compare how new a file is with the header obtained
from Net::HTTP.head(). I use

STRFTIME_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
timestamp = DateTime.strptime(lastmod, STRFTIME_FORMAT)

require 'time'
timestamp = Time.parse lastmod [...]
Time.parse Just Works.

:) Yes, it does. Thank you.Hugh
 
T

tony summerfelt

turned into a time into it. Parsedate deosn't seem to handle this
case, either in so far as it has no str[fp]time.
Time.parse Just Works.

except when it doesn't :/

i ran into a case this a few months ago, trying to parse a log. i was
getting weird results until i saw that time.parse wasn't parsing
properly...using strptime helped a little...
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top