Re: How create a date+time type of vbs in perl?

Bill Luebkert <[email protected]> Mon, 18 Apr 2011 21:55:41 -0700
Newsgroups gmane.comp.lang.perl.active-perl
Message-ID <[email protected]>
On 4/18/2011 1:49 AM, Xiao Yafeng wrote:
> Hi,
>     Is there a way to create a date type of vbs in perl?
>
> for example,
>                         how can I translate below vbs statement into perlish one?
>
>                   date_from = DateSerial(2011,03, 23) + timeserial(6, 0, 0)
>
> any reply is really appreciated.

Windows has more than one time format, so it could get complicated if you
plan on doing anything wild with it.

	file time is a dbl-word (64 bit) in 100-nanosecond intvls since Jan 1, 1601
	system time is array of (YR, MON, DOW, HR, MIN, SEC, MSEC) similar to @tm

You also have to worry about where the time is from (local/UTC/etc).

What we don't know is what happens to date_from farther down to know
what to put in there - you would need to indicate that for a proper
response.  The stuff below assumes you can modify that to use a @tm
structure to do what you need with it.

In your case, you only have human kinda times (year, month, day, hr, min, sec),
so it's a simple matter to plunk the data into a @tm structure and
do what you want with it.  Just subtract 1900 from the year and 1
from the month and you're ready to go.

@tm = (<sec>, <min>, <hr>, <mday>, <mon>, <year>, <wday>, <yday>, <dst>); # , [<TZO>]);
@tm = (0, 0, 6, 23, 2, 111, 0, 0, 0);

I sometimes add an extra 10th element where I store the TZO (timezone offset).

I compute DST and TZO to help convert from/to UTC/local.

# I'm in California, so I set my local TZ just in case:

$ENV{TZ} = 'PST8PDT' if not exists $ENV{TZ};
my $now = time;			# current time (UTC)
my @ltm = localtime $now;	# current local time @tm
my @gtm = gmtime $now;		# current UTC time @tm
my $DST = $ltm[8];		# is daylight savings time ?
my $gnow = timegm (@gtm);	# convert UTC @tm to epoch time
my $lnow = timegm (@ltm);	# convert local @tm to epoch time
my $TZO = $lnow - $gnow;	# diff in secs between UTC and local
				# which can be converted to hrs/mins

you can convert it to epoch time for comparison/arithmetic
and then you have the usual formatting and conversion routines
gmtime/localtime/timegm/timelocal/strftime etc.

Your local @tm converted to epoch time using timelocal @tm => 1300885200;
Then you can use epoch time for calculations.

Then you could use the @tm structure to display your time:
	my $TZ = $TZO / 3600; # convert TZO to hrs for display
	print strftime ("%a, %d %b %Y %H:%M:%S $TZ\n", @tm[0..8]);

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs