1

I am using Perl for the first time and I need to do the following:

  1. Parse two separate timestamp strings which are in the format YYYY/mm/dd HH:MM:SS:NNNNNN. The timezone of both strings is assumed to be EST. For example, both strings are:

    "2018/03/24 12:11:57:123456"
    "2018/03/26 13:10:57:123456"
    
  2. Convert the timezone of the above two into a different timezone, say GMT, then print them out.

  3. Find the differences, in hours, of the above two timestamps.

Is it possible to achieve the above with the Time::Piece module? I have looked through this web site:

https://www.perl.com/article/59/2014/1/10/Solve-almost-any-datetime-need-with-Time--Piece/

It shows me how to achieve my first objective, the parsing. But so far I am not able to find out how to convert from one timezone to another, and also the difference in hours between two timestamps.

Thanks.

simbabque
  • 53,749
  • 8
  • 73
  • 136
user3573403
  • 1,780
  • 5
  • 38
  • 64
  • Welcome to the Perl side of things here on Stack Overflow. You're asking two questions at once. There's probably an answer for the second one out there, and there is a very detailed one for the first. I'm inclined to hammer as a duplicate for the first question. You will want to look into the `cpan` command line utility for installation of DateTime. Keep in mind that DateTime does not parse strings. You need a DateTime::Format::* module for that as well, though your format is fairly standard. – simbabque Mar 26 '18 at 08:47
  • I suggest you split your question into two distinct questions, and also try to find a Time::Piece solution on your own. Ask about that including the code you can't get to work and people will be more inclined to help you. – simbabque Mar 26 '18 at 08:49
  • 1
    Hi, I will split the question now. – user3573403 Mar 26 '18 at 08:50
  • My question on DateTime is at: https://stackoverflow.com/questions/49487499/perl-how-to-install-the-datetime-module-for-timestamp-parsing – user3573403 Mar 26 '18 at 08:55
  • Found you an answer. The `localtime` bit is the relevant part of Schwern's answer there. If that doesn't help, let me know and we'll reopen the question. (As a gold badge user I can duplicate close-hammer questions in this tag) – simbabque Mar 26 '18 at 09:17
  • Hi @simbabque, I don't think localtime will work for me. My program needs to be able to run anywhere. If I run in Singapore, the localtime will have time zone of GMT+8, isn't it? If my timestamp object is GMT, and I want to convert to EST, I can't use localtime right? – user3573403 Mar 26 '18 at 11:43
  • You can pass arguments to it. – simbabque Mar 26 '18 at 11:45
  • Hi @simbabque, a working example of localtime with a different timezone would be appreciated. – user3573403 Mar 26 '18 at 11:54
  • There is https://stackoverflow.com/q/753346/1331451, but I cannot get it to work currently. – simbabque Mar 26 '18 at 13:34

0 Answers0