1

I have tried to find out via the documentation but was not able to answer this question: What time_zone literals does DateTime in perl accept?

CEST is not valid. For CET, output is the current time but we currently have CEST timezone active.

What I am at: I would like to get the current time and find out what time it would be in CET.

If the server is running at CEST it should give as result the current time -1h. If the server is running at CET, it should give the current time.

My approach is:

$av_loc_TESTHOUR = DateTime->now(time_zone=>'CET')->strftime('%H');

which gives the current time of the server as result, but should give the current time -1h.

zb226
  • 9,586
  • 6
  • 49
  • 79
averlon
  • 325
  • 3
  • 14
  • found a way on my own: perl -e 'use DateTime; my @av_tmp_DT=DateTime::TimeZone->all_names; print "@av_tmp_DT\n";' – averlon May 03 '16 at 13:28
  • I found a way to calculate the time in CET: First I get the current hour on my mashine: $av_loc_TESTHOUR=DateTime->now(time_zone=>'CET')->strftime('%H'); Then I reduce the value by the offset if one: $av_loc_TESTHOUR-=DateTime->now(time_zone=>'CET')->is_dst(); – averlon May 03 '16 at 13:52

1 Answers1

2

From the DateTime module's documentation:

The time_zone parameter can be either a scalar or a DateTime::TimeZone object. A string will simply be passed to the DateTime::TimeZone->new method as its "name" parameter. This string may be an Olson DB time zone name ("America/Chicago"), an offset string ("+0630"), or the words "floating" or "local". See the DateTime::TimeZone documentation for more details.

The "for more details" documentation it refers to is here.

The "Olson" database it refers to is the standard IANA TZ Database, whose identifiers you can find here.

Identifiers like "CET" are only in the data for backwards compatibility purposes. You should instead be using a fully qualified location-based time zone identifier, such as "Europe/Paris".

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • yes, but "Europe/Berlin" does not help in this case. I need to calculate the time what hour it would be in CET regardless what we currently have on my mashine. BASH can do! But I already posted the result I will use. Caused me some thinking around the corner but that is often the case in IT. At least it works. – averlon May 03 '16 at 15:05
  • Respectfully, I don't think you've thought this through carefully. Please read [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info), and the [Daylight saving time and time zone best practices](http://stackoverflow.com/q/2532729/634824) article. – Matt Johnson-Pint May 03 '16 at 15:35
  • I meanwhile have replace CET with "Europe/Berlin". Gives the same result but is saver as more compliant to "as designed". The environment this conversion from CEST to CET must be done is complex and I do not want to bother you with this. At least, the solution posted works for me. So for the moment I will stay with this. Date/Time constructs are extremly challenging. It took me weeks until I got a working solution in bash. But thanks for hints. – averlon May 04 '16 at 09:31
  • How can I check is `$string` is a valid timezone accepted by `DateTime` ? – Ωmega Aug 23 '22 at 15:34