PerlInMyBlood has asked for the wisdom of the Perl Monks concerning the following question:

I have an input file with 9 columns where 6th and 7th columns are for start and end unix time. (The overall span of the timeline is 1290895200 to 1291154399, 3 days) I am looking for an arrangement to separate the unix time day-wise to have the data look like in the format below (with rest of the columns remaining unchanged):

Hour Day Col3 Col4 Col5 Col6 ..... 
 0    1  ....... 
 1    1  ....... 
upto 
 23   3  ....



  • Comment on Categorize rows on the basis of unix time

Replies are listed 'Best First'.
Re: Categorize rows on the basis of unix time
by chrestomanci (Priest) on Jul 10, 2011 at 18:09 UTC

    It is not hard to extract fields like hours or days from a unix epoch time. The function you need is gmtime. If you call it in an array context you get back the fields.

    From there it is a simple matter to write a script that reads your input file line by line, parses the unix time, and then writes out a new file with the time fields you want and the remaining fields unmodified.

Re: Categorize rows on the basis of unix time
by Marshall (Canon) on Jul 10, 2011 at 18:17 UTC
    Unix time is in UTC, based from the Epoch time. So just subtract all times from the start time. This will be number of seconds since the input file started collecting data.

    You can calculate the hours manually: 60 seconds * 60 min = 1 hour. All days have 24 hours since we are dealing with GMT(UTC) time instead of funny daylight savings time.

    Alternately, you can use any of the epoch to string functions like the use POSIX qw(strftime); module directly on this new start of data quasi epoch time. Just ignore the Jan 1970 stuff and only care about hour and day of month.

    Update: whipped out my calculator, 1291154399 - 1290895200 = 259199. 60 sec * 60 sec/min *24 hours/day *3 days = 259200 seconds. So I'd keep it simple. Subtract 1290895200 from all the rest of the times. Hour one is from 0 to 60*60*1 seconds, hour 2 from that until 60*60*2 seconds, etc.