PERL, Date & Time issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL, Date & Time issues
# 1  
Old 10-05-2012
PERL, Date & Time issues

Hello All,

This is my first script in PERL. Hence require your help in moving further.

I have a script which should populate the values for Today, Yesterday output.
For which I use timeFrame as a variable to obtain the time in hrs:mm as 10:00.

All I want is, I want my timeFrame to start with 00:00 and end with with my current timestamp. Say if the time is 07:23, my timeFrame should contain 00:00 to 07:00. [I am not bothered on Minutes since it works hourly basis]. I have written the PERL in the below way.

Code:
#!/usr/bin/perl
use Time::Local;
my %DataHash=();
$begOffset=3600*12;
my ($runYear, $runMonth, $runDay, $runHour, $runMin, $runSec) = (localtime(time))[5,4,3,2,1,0];
$runYear+=1900;
$runMin = (int($runMin/5)*5);
my $runTime = timelocal($runSec,$runMin,$runHour,$runDay,$runMonth,$runYear);
my $begTime = $runTime-$begOffset;
my $j=0;
for($i=$begTime;$i<=$runTime;$i+=3600){

        my $startTime = ($startHour,$startMin) = (localtime($i))[2,1];
        if ($startMin <10){$startMin="0$startMin";}
        if ($startHour <10){$startHour="0$startHour";}
        $timeFrame="$startHour:00";
        $DataHash{$j}->{'timeFrame'}=$timeFrame;
        $j++;
		print "TimeFrame -> $timeFrame StartTime -> $startTime  StartHour -> $startHour\n\n";
}

But the output came as
Quote:
TimeFrame -> 19:00 StartTime -> 2 StartHour -> 19

TimeFrame -> 20:00 StartTime -> 2 StartHour -> 20

TimeFrame -> 21:00 StartTime -> 2 StartHour -> 21

TimeFrame -> 22:00 StartTime -> 2 StartHour -> 22

TimeFrame -> 23:00 StartTime -> 2 StartHour -> 23

TimeFrame -> 00:00 StartTime -> 2 StartHour -> 00

TimeFrame -> 01:00 StartTime -> 2 StartHour -> 01

TimeFrame -> 02:00 StartTime -> 2 StartHour -> 02

TimeFrame -> 03:00 StartTime -> 2 StartHour -> 03

TimeFrame -> 04:00 StartTime -> 2 StartHour -> 04

TimeFrame -> 05:00 StartTime -> 2 StartHour -> 05

TimeFrame -> 06:00 StartTime -> 2 StartHour -> 06

TimeFrame -> 07:00 StartTime -> 2 StartHour -> 07
Now the current time is 07:23, so I want that output to be like below
Quote:
TimeFrame -> 00:00 StartTime -> 2 StartHour -> 00

TimeFrame -> 01:00 StartTime -> 2 StartHour -> 01

TimeFrame -> 02:00 StartTime -> 2 StartHour -> 02

TimeFrame -> 03:00 StartTime -> 2 StartHour -> 03

TimeFrame -> 04:00 StartTime -> 2 StartHour -> 04

TimeFrame -> 05:00 StartTime -> 2 StartHour -> 05

TimeFrame -> 06:00 StartTime -> 2 StartHour -> 06

TimeFrame -> 07:00 StartTime -> 2 StartHour -> 07
Can you please help me by correcting this code. It will be great if you make use of localtime and not anything else. Thanks in advance.

I am in a hurry, please help me asap. TA.

---------- Post updated 10-05-12 at 03:37 AM ---------- Previous update was 10-04-12 at 06:57 AM ----------

Can someone please help me on it.
# 2  
Old 10-05-2012
Your code is vague and garbled. Could you please provide a sample output? (something like.. this is what I really want my output to look like). Looking at your desired output and corresponding code from post #1, it doesn't look like that's the output you really want.

If that's the output you want, here's the code for it:
Code:
#! /usr/bin/perl -w
use strict;

my $hr = (localtime)[2];
my $i = 0;

for ($i = 0; $i <= $hr; $i++) {
    printf "TimeFrame -> %02d:00 StartTime -> 2 StartHour -> %02d\n", $i, $i;
}

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 10-08-2012
Many thanks, the above satisfies my script.
# 4  
Old 10-08-2012
I may be wrong and its none of my business either.. but since we're all here to learn and share what we know, I thought I might as well share with you what's on my mind.

I have this thing pricking that may be you got the requirement wrong. Your attempted code and the desired output somehow don't seem to match each other. It looks like you're trying to achieve something else in the code. And looking from that angle, your desired output is altogether different. May be you want to revisit your specifications?
# 5  
Old 10-08-2012
No mate, I have taken the idea what you have posted here and reached my desired goal. I have made updations on your code and achieved it. Thanks for your note.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DATE & TIme

Can we select the datetime from oracle database in “hhmmssnnnccyymmdd” format ? please help to solve this..... (2 Replies)
Discussion started by: Sanal
2 Replies

2. Shell Programming and Scripting

awk & date time format

This gives: grep "qprt -o -dp -N 1 -P" printfile.log.1216 |awk '{print $7, $9}' |grep lpc1z The output like below: lpc1z /cerner/d_prod/print/ccltemp4235396d030a.dat.tmp.1216075330] lpc1z /cerner/d_prod/print/ccltemp4235396d036a.dat.tmp.1216075634] lpc1z... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

3. Shell Programming and Scripting

Extracting date & time from file name

Hi, I am having a file name as exp_bkp_tables_18_Oct_2010_10_50_28.dmp which is used for import the records. Now, I want to print the output using the selected file name as below : Table records will get restored as on date 18-Oct-2010 and time 10:50:28 How it can be done ? With... (5 Replies)
Discussion started by: milink
5 Replies

4. Shell Programming and Scripting

About date & time difference

Hello All, I was having a look on threads on the Forum about time calculation but didn't find exactly this issue. For instance, if we have these 2 dates, begin & end : 20100430235830 20100501000200 Is there anyway, awk, ksh, perl to calculate the difference in sec and get for... (6 Replies)
Discussion started by: rany1
6 Replies

5. UNIX for Dummies Questions & Answers

to get remote server date & time

Hi, i dont have remote m/c user credential. i only know remote m/c ip address. and i am able to ping that remote m/c. In windows we use: "net time \\computername" to get the remote m/c time. so how can i get remote m/c time in unix m/c? (means a unix command) Thanks for the help. ... (9 Replies)
Discussion started by: partha_ori
9 Replies

6. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

7. UNIX for Dummies Questions & Answers

Date time issues on RHEL

Hi, I am facing a weird problem with the 'Date'. If I check date multiple times in a short interval I see a different time altogether. Here is an example $ date Tue Jul 15 02:07:22 PDT 2008 $ date Tue Jul 15 02:07:23 PDT 2008 $ date Tue Jul 15 03:20:42 PDT 2008 $ date Tue Jul 15... (5 Replies)
Discussion started by: sumitb74
5 Replies

8. UNIX for Dummies Questions & Answers

Backup Date & Time

:confused: I'm not really sure about the default backup date & time in our Unix system and I would like to change it to a convienient time...how do I do that? Please help? (1 Reply)
Discussion started by: EbeyeJJ
1 Replies

9. UNIX for Dummies Questions & Answers

file creation date & time

Hi All, I have some files which are creates every day using a script. I want to create a log files which does write "filename,creation day and time" how can I do this ?? Alice (3 Replies)
Discussion started by: alisevA3
3 Replies

10. UNIX for Dummies Questions & Answers

Time & Date Command

Hey all, When you run the 'ls -la' command it'll show you the time and dates of all files/directories. Now what I am trying to do is create a script that will tell me what files haven't been used in over the past 1 month and what the time and date is that the files that haven't been accessed in... (2 Replies)
Discussion started by: merlin
2 Replies
Login or Register to Ask a Question