Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
OT: date2int and vice versa in PERL
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Tue Dec 10, 2002 8:13 am    Post subject: OT: date2int and vice versa in PERL Reply with quote

Hi foks,

does anyone know a simple command or package (PERL) that is able to convert a date into an integer and vice versa???
I need something like
Code:
date2int($month,$day,$year)

or similar.

Thx fox
Back to top
View user's profile Send private message
rac
Bodhisattva
Bodhisattva


Joined: 30 May 2002
Posts: 6553
Location: Japanifornia

PostPosted: Tue Dec 10, 2002 8:16 am    Post subject: Reply with quote

Is this integer seconds since the epoch? If so, look at Time::Local, which contains timelocal and timegm. The inverse of this (split an epoch time into the Perl equivalent of a C struct tm) is localtime and gmtime.
_________________
For every higher wall, there is a taller ladder
Back to top
View user's profile Send private message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Tue Dec 10, 2002 8:26 am    Post subject: Reply with quote

hmm,

if possible i would prefer "interger days since the epoch", because i have to do something like:
Code:

(($interger % 7 ) == $monday)
Back to top
View user's profile Send private message
rac
Bodhisattva
Bodhisattva


Joined: 30 May 2002
Posts: 6553
Location: Japanifornia

PostPosted: Tue Dec 10, 2002 8:39 am    Post subject: Reply with quote

Code:
#! /usr/bin/perl

use Time::Local;

my ($day, $month, $year) = (3, 10, 1981);
my @WEEKDAYS = ( 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' );

my $secs = timelocal( 0, 0, 0, $day, $month-1, $year );
my @tm = localtime( $secs );
print $WEEKDAYS[$tm[6]], "\n";

The "month-1" is because months are counted from 0 in localtime land. See the perlfunc man page (or look at the C struct tm) to see that [6] is the day of the week, and 0 = sunday there. You can combine the timelocal and localtime commands, without the $secs intermediate variable, but I put it in for clarity. Is this what you are looking for?
_________________
For every higher wall, there is a taller ladder
Back to top
View user's profile Send private message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Tue Dec 10, 2002 10:17 am    Post subject: Reply with quote

[I will test it out later.]

Hmm, i think this is nearly exactly what i need!!!

Thank you very much!!! :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum