For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2008 > Extract string of form YYYYMMWW fro timestamp









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Extract string of form YYYYMMWW fro timestamp
Ow Mun Heng

2008-01-24, 4:07 am

I'm wondering there's a simpler was to achieve this w/o need to jump
through a couple of hoops. (meaning, concatenate the fields together
from extract or some other method)

Timestamp : '2008-01-17 10:24:00'
Output needed : 20080103 or even 200801w3

In postgresql, this is done via

select to_char('2008-01-17'::timestamp,'YYYYMM')|| 'w' || to_char('2008-01-17'::timestamp,'w')
result : "200801w3"


Trying to do some perl-DBI and directly inserting the results into specific partitioned tables.
Yitzle

2008-01-24, 4:07 am

[url]http://perldoc.perl.org/perlfaq4.html#How-do-I-find-the-day-or-w-of-the-year%3F[/url]
You can extract the YYYY-MM-DD with a simple RegEx (untested code):

$str = q/Timestamp : '2008-01-17 10:24:00'/;
my ($year, $mon, $day) = ($str =~ /'([0-9]{4})-([0-9]{2})-([0-9]{2})/);
Gunnar Hjalmarsson

2008-01-24, 4:07 am

Ow Mun Heng wrote:
> I'm wondering there's a simpler was to achieve this w/o need to jump
> through a couple of hoops. (meaning, concatenate the fields together
> from extract or some other method)
>
> Timestamp : '2008-01-17 10:24:00'
> Output needed : 20080103 or even 200801w3


use POSIX;
use Date::Parse;
my $timestamp = '2008-01-17 10:24:00';
print strftime "%Y%m%V\n", (strptime $timestamp)[0..5];

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com