How do I parse microseconds with Time::Piece strptime? - perl

I have a timestamp that looks like 25-OCT-10 04.11.00.000000 AM. I'm trying to convert this to a time format with
Time::Piece->strptime("25-OCT-10 04.11.00.000000 AM","%d-%b-%y %I.%M.%S.%6N %p")
but it keeps throwing errors. I've tried %OS, %SZ. They dont seem to work. Can someone tell me what I'm doing wrong?

Time::Piece doesn't support sub-second times. Try DateTime instead, for example, DateTime::Format::Strptime:
use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new(
pattern => '%d-%b-%y %I.%M.%S.%6N %p',
);
my $dt = $parser->parse_datetime("25-OCT-10 04.11.00.000000 AM");

strptime won't read that. strptime works with a structure that only goes down to integer seconds, and it doesn't have any formats for recognizing non-integer numerics -- and there's no such format as N in Time::Piece's strptime. If you know that you're always expecting .000000 for a number of microseconds then you could try using ..."%I.%M.%S.000000 %p", otherwise strptime just isn't for you.
How about DateTime::Format::CLDR? A format of "dd-MMM-yy hh.mm.ss.SSSSSS a" seems to work perfectly well with that format.
use DateTime::Format::CLDR;
my $parser = DateTime::Format::CLDR->new(
pattern => "dd-MMM-yy hh.mm.ss.SSSSSS a",
locale => "en_US",
);
my $dt = $parser->parse_datetime("25-OCT-10 04.11.00.000100 AM");
say $dt->iso8601; # 2010-01-25T04:11:00
Edit: just noticed that this doesn't recognize months properly if they're all uppercase -- it recognizes "Oct" but not "OCT". A fixed version is available here and has been sent upstream for merge :)
Update: DateTime::Format::CLDR 1.11 is properly case-insensitive.

Related

input date mm/dd/yy without using scanf function?

As I read, using scanf can cause problems so I want to avoid using it in practice. How else can I take input from user in mm/dd/yy format (which is good practice) ?
Preferably in not too many steps.
You can use strptime() function , if you first read the string representation into str, you can then use:
struct tm tm;
strptime(str, "%m/%d/%Y", &tm);
It translates to your date as follows:
date.year = tm.tm_year;
date.month = tm.tm_mon;
date.day = tm.tm_mday;

How to make regional date formats using Excel::Writer::XLSX

I use the Excel::Writer::XLSX module to generate excel report, but the date format does not change according to different regions.
For example, if I set the region to 'English-United States', the date format should be 'mm/dd/yyyy' shown on excel sheet, if I reset the region to 'French-France', the date format should be 'dd/mm/yyyy'.
So I'd like to know how it is implemented using Excel::Writer::XLSX. Thanks.
Excel stores a limited number of date formats with regional settings. For example in the number format dialog in Excel you will see a warning like this:
Displays date and time serial numbers as date values, according to the type and locale (location) that you specify. Date formats that begin with an asterisk (*) respond to changes in regional date and time settings that are specified in Control Panel. Formats without an asterisk are not affected by Control Panel settings.
In Excel::Writer::XLSX you can set these using a num_format id number instead of a format string. For example:
#!/usr/bin/perl
use strict;
use warnings;
use Excel::Writer::XLSX;
my $workbook = Excel::Writer::XLSX->new( 'dates.xlsx' );
my $worksheet = $workbook->add_worksheet( 'Demo' );
$worksheet->set_column('A:A', 20);
# Regional date format.
my $date_format_1 = $workbook->add_format( num_format => 14 );
# Non-regional date format.
my $date_format_2 = $workbook->add_format( num_format => 'dd/mm/yy' );
$worksheet->write_date_time( 'A1', '2013-10-27T', $date_format_1 );
$worksheet->write_date_time( 'A2', '2013-10-27T', $date_format_2 );
__END__
See the num_format section of the docs for more examples of the builtin format ids.

Zend_Date() is not giving the correct/expected result

Zend_Date function returns the value "Feb 10, 2012" . It supposed to return "Oct 2, 2012". If a give the day greater than 12 than it returns the currect output. I don't know what I was missing. Please help me.
Thanks
If your format is YYYY-MM-DD (2012-10-02), try this code:
$str = '2012-10-02';
$date = new Zend_Date($str, Zend_Date::YEAR . '-' . Zend_Date::MONTH . '-' . Zend_Date::DAY);
echo $date->toString();
What is the code you are using to call the function?
Different countries use different date formats. Some use dd/mm/yyyy others use mm/dd/yyyy. The order you are passing in the day and month are different from the way the function expects to be called.
Try reversing the month and day before you call the function.

Parsedate question in Perl

In Perl, why I get different results from parsedate(2010-7-2 13:0:0) and parsedate(2010-7-2 13:00:0) ?
The 2010-7-2 13:0:0 string is not in a valid format, and is actually not being parsed at all (it appears) as evidenced by the fact that parsedate("2010-7-2") returns the same value as parsedate("2010-7-2 13:0:0") for me.
Based on the docs, it's simply parsing the YYYY-MM-DD, but not parsing the 13:0:0 at all because it is expecting it to be in HH:MM format and not HH:M format. Basically, you have to use two digits for the minutes in order for it to be valid input.
To handle your date format with more flexibility, try using DateTime::Format::Strptime
my $strp = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %T',
locale => 'en_AU',
time_zone => 'Australia/Melbourne',
);
my $dt1 = $strp->parse_datetime('2010-7-2 13:0:0');
my $date_1 = $strp->format_datetime($dt1);
$date_1 is now converted into a well-formatted date format "2010-07-02 13:00:00". Then you can call parsedate($date_1) & get epoch.

How to print date in the format of mm/dd/yyyy in VB

I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")