http://www.perlmonks.org?node_id=1048566

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

Hi Monks

I need to convert current EST to BST time.

I was converting to GMT time, but now the requirement has changed to BST time

Basically, I was doing this:

 strftime($date_format, gmtime(time));

I could not find an equivalent bsttime function.

Does anyone know of such a function? And if not, is there a way to convert to BST time?

Thanks for the help

Tony

Replies are listed 'Best First'.
Re: bst time?
by Pizentios (Scribe) on Aug 08, 2013 at 13:38 UTC

    I second DateTime. However if you are reading date strings from a data file or db and bneed to convert them i have found that DateTime::Format::DateParse works really well for this

    Do something like:

    use strict; use warnings; use DateTime::Format::DateParse; my $dt = DateTime::Format::DateParse->parse_date($date_string, $zone);

    The module will parse your date string and return a DateTime object (which you can then use the DateTime methods etc to do stuff with the date). You would then use $dt->set_time_zone() to change your time zone.

    See DateTime::Format::DateParse and DateTime For more information about the modules.

    -Pizentios
Re: bst time?
by sundialsvc4 (Abbot) on Aug 08, 2013 at 13:10 UTC
Re: bst time?
by Loops (Curate) on Aug 08, 2013 at 13:59 UTC
    To print the current time in BST:
    use DateTime; print DateTime->now->set_time_zone('Europe/London')->strftime('%c %z % +Z'); # Output: Aug 8, 2013 2:58:00 PM +0100 BST
Re: bst time?
by BrowserUk (Patriarch) on Aug 08, 2013 at 14:02 UTC

    If you're into DIY rather than humungous modules, this routine calculates the unix epoch (GMT) for the start and end of BST (and European ST) for the years since they were unified in 1996:

    #! perl -slw use strict; use 5.010; sub EuST { state %EuST; my $year = shift; return @{ $EuST{ $year } } if exists $EuST{ $year }; my $start = 1; $start += 86400*365.25 for 1 .. ($year-1970); $start -= 86400 until scalar localtime( $start ) =~ /${ \( $year - + 1 ) }/; $start += 1 until scalar localtime( $start ) =~ /$year/; $start += 59*60+59; $start += 86400*85; $start += 86400 until localtime( $start ) =~ /Apr/; $start -= 86400; $start -= 86400 until localtime( $start ) =~ /Sun/; my $end = $start + 86400 * 30 * 7; $end += 86400 until scalar localtime( $end ) =~ /Nov/; $end -= 86400; $end -= 86400 until scalar localtime( $end ) =~ /Sun/; @{ $EuST{ $year } } = ( $start, $end ); } our $YEAR //= 2013; say map{; "Epoch: $_ => ", scalar localtime( $_ ), ' ' } EuST( $YEAR ) +; __END__ C:\test>for /l %y in (1995,1,2013) do @EuST -YEAR=%y Epoch: 796179599 => Sun Mar 26 00:59:59 1995 Epoch: 814928399 => Sun O +ct 29 01:59:59 1995 Epoch: 828233999 => Sun Mar 31 00:59:59 1996 Epoch: 846377999 => Sun O +ct 27 01:59:59 1996 Epoch: 859683599 => Sun Mar 30 00:59:59 1997 Epoch: 877827599 => Sun O +ct 26 01:59:59 1997 Epoch: 891133199 => Sun Mar 29 00:59:59 1998 Epoch: 909277199 => Sun O +ct 25 01:59:59 1998 Epoch: 922582799 => Sun Mar 28 00:59:59 1999 Epoch: 941331599 => Sun O +ct 31 01:59:59 1999 Epoch: 954032399 => Sun Mar 26 00:59:59 2000 Epoch: 972781199 => Sun O +ct 29 01:59:59 2000 Epoch: 985481999 => Sun Mar 25 00:59:59 2001 Epoch: 1004230799 => Sun +Oct 28 01:59:59 2001 Epoch: 1017536399 => Sun Mar 31 00:59:59 2002 Epoch: 1035680399 => Sun + Oct 27 01:59:59 2002 Epoch: 1048985999 => Sun Mar 30 00:59:59 2003 Epoch: 1067129999 => Sun + Oct 26 01:59:59 2003 Epoch: 1080435599 => Sun Mar 28 00:59:59 2004 Epoch: 1099184399 => Sun + Oct 31 01:59:59 2004 Epoch: 1111885199 => Sun Mar 27 00:59:59 2005 Epoch: 1130633999 => Sun + Oct 30 01:59:59 2005 Epoch: 1143334799 => Sun Mar 26 00:59:59 2006 Epoch: 1162083599 => Sun + Oct 29 01:59:59 2006 Epoch: 1174784399 => Sun Mar 25 00:59:59 2007 Epoch: 1193533199 => Sun + Oct 28 01:59:59 2007 Epoch: 1206838799 => Sun Mar 30 00:59:59 2008 Epoch: 1224982799 => Sun + Oct 26 01:59:59 2008 Epoch: 1238288399 => Sun Mar 29 00:59:59 2009 Epoch: 1256432399 => Sun + Oct 25 01:59:59 2009 Epoch: 1269737999 => Sun Mar 28 00:59:59 2010 Epoch: 1288486799 => Sun + Oct 31 01:59:59 2010 Epoch: 1301187599 => Sun Mar 27 00:59:59 2011 Epoch: 1319936399 => Sun + Oct 30 01:59:59 2011 Epoch: 1332637199 => Sun Mar 25 00:59:59 2012 Epoch: 1351385999 => Sun + Oct 28 01:59:59 2012 Epoch: 1364691599 => Sun Mar 31 00:59:59 2013 Epoch: 1382835599 => Sun + Oct 27 01:59:59 2013

    Of course, my localtime *is* BST so you would need to adjust them for your local timezone.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: bst time?
by xiaoyafeng (Deacon) on Aug 09, 2013 at 02:28 UTC
    If I don't get you wrong, there is a strftime function in Posix module. gmtime is a core function in perl.




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction