How to format incomplete dates and times in a locale specific manner - c++

How can one format incomplete dates and/or a times in a locale specific manner?
I have a date/time stored in a set of 7 strings:
dayOfWeek - The day of the week. ([1,7])
dayOfMonth - The day of the month. ([1,31])
monthOfYear - The month of the year. ([0,11])
year - The year. ([-∞,∞])
hourOfDay - The hour of the day. ([0,11])
minuteOfHour - The minute of the hour. ([0,59])
meridianOfDay - The meridian of the day. ([0,1])
For various reasons, most of these are obtained through use of IR on web data, some of these strings may be empty. The problem is then how to format such incomplete dates/times in a locale specific manner.
What I currently do, using boost.locale, is create a date_time and add to it
the various date/time components that are complete, then store the date_time in a
locale specific manner in a string. Something like this:
date_time dateTime;
if(!dayOfWeek.empty())
dateTime = period::day_of_week(lexical_cast<int>(dayOfWeek));
if(!dayOfMonth.empty())
dateTime = period::day(lexical_cast<int>(dayOfMonth));
if(!monthOfYear.empty())
dateTime = period::month(lexical_cast<int>(monthOfYear));
if(!year.empty())
dateTime = period::extended_year(lexical_cast<int>(year));
if(!hourOfDay.empty())
dateTime = period::hour_12(lexical_cast<int>(hourOfDay));
if(!minuteOfHour.empty())
dateTime = period::minute(lexical_cast<int>(minuteOfHour));
if(!meridianOfDay.empty())
dateTime = period::am_pm(lexical_cast<int>(meridianOfDay));
std::stringstream stringStream;
stringStream.imbue(desiredLocale);
stringStream << dateTime;
std::string localeSpecificDateTime = stringStream.str();
This works to some extent. I have a locale formatted date_time. However, the fields
that are incomplete are filled with the date/time of now, due to the date_time I start with.
What would be perfect is if one could flag the incomplete fields as to not be included in
the formatting of the date_time.
PS: I know I could maintain 127 (27-1) formatting strings for each locale, but creating/finding such strings and then maintaining them seems like a recipe for insanity!

Related

How to get date (day/month/year) from MonthCalendar in C++ Builder 6?

I'm creating an age counter app, but I couldn't use the date, which is user chose from the calendar. How can I use the day/month/year specified in MonthCalendar in my program?
TMonthCalendar has a Date property, which returns the user's selected date as a TDateTime value. You can extract the individual month, day, and year values from that, if needed, by using the TDateTime::DecodeDate() method.
TDateTime dtSelected = MonthCalendar1->Date;
Word wYear, wMonth, wDay;
dtSelected.DecodeDate(&wYear, &wMonth, &wDay);
...

How to convert Particular timezone of date into GMT timezone using esql of MQ?

I have the date of particular timezone, and I want to convert it to the GMT timezone, and then it needs to be inserted into DB using esql of MQ. Please help to resolve this issue.
If you want to convert a date from a format to another, you can do the following :
DECLARE inDate DATE;
DECLARE outDate DATE;
DECLARE tempDate DATE;
DECLARE patternIN CHARACTER 'yyyy-MM-dd';
DECLARE patternOUT CHARACTER 'yyMMdd';
SET tempDate = CAST(inDate AS DATE FORMAT patternIN);
-- Convert input String as Date (should match patternIN)
SET outDate = CAST(tempDate AS CHARACTER FORMAT patternOUT)
-- Convert the date object to the desired date format
Of course you need to be able to define your date pattern. I know you might need to separate the DATE from the TIME, but the object are exactly the same. A quick example of a specific cast :
CAST(CURRENT_DATE AS CHARACTER FORMAT 'yyyy-MM-dd') || 'T' || CAST(CURRENT_TIME AS CHARACTER FORMAT 'HH:mm:SS')
This will generate a date in the XML format, e.g : 2019-08-28T16:46:32

Dynamics CRM 2011 Timestamp into Python

I am exporting data from Dynamics CRM 2011 into an Excel File. The entries have timestamps. In CRM they look like normal dates, but when I export them into Excel the timestamps look similar to this:
41855.4043865741
41831.6309259259
In Excel I can right-click on the cell with the timestamp and do Cell Formatting > Numbers > Date and convert this to a human readable string.
e.g.
04.08.2014 09:42:19
11.07.2014 15:08:32
The problem is, after I save the Excel with the human readable Datetime format and read the Excel with the xlrd Module (Python 2.7) I still get the strange format and not the translated one.
So I tried using datetime Module to dranslate the date but when doing so I get the wrong date.
import datetime
str_dt = float(41831.6309259259)
print datetime.datetime.fromtimestamp(str_dt).strftime('%Y-%m-%d %H:%M:%S')
My result is: 1970-01-01 12:37:11
Of course it is not Unixtimestamp but I have no idea what timestamp it actually is and how I can convert it with python.
The values you see are the actual number of days since the 1st of January 1900. This is the format used by CRM and Excel.
As suggested in this answer, you can use xlrd.xldate.xldate_as_datetime to:
Convert an Excel date/time number into a datetime.datetime object.
#param xldate The Excel number
#param datemode 0: 1900-based, 1:
1904-based.
#return a datetime.datetime() object.
In your case the datemode would be 0.

Coldfusion 10 DateFormat Issue

I am using the DateFormat function to convert dates to this format: yyyy-mm-dd. This is the original format of the date: dd-mm-yyyy. Below is a snippet of the code:
<cfset newdate = #DateFormat(Trim(mydate), "yyyy-mm-dd")# />
The problem is that I get different results for different dates. For example:
If my original date is: 15-05-2013 (dd-mm-yyyy)
The result is: 2013-05-15 (yyyy-mm-dd)
However, if I change the input and:
The original date is: 01-05-2013 (dd-mm-yyyy)
The result is: 2013-01-05 (yyyy-dd-mm)
Any help or guidance as to what is wrong would be highly appreciated.
I disagree with the other answer. The real cause of the problem is that DateFormat is not designed to handle non-US date strings.
The standard CF date functions always use U.S. date parsing rules. That means when you pass in an ambiguous date string, like 01-05-2013, it is parsed according to U.S. English date conventions. In this case, month first ie "mm-dd-yyyy". So the result will always be January 5th, not May 1st.
In some cases you get lucky. With the string 15-05-2013, there is obviously no 15th month, so CF/java must swap the month and day automatically, rather than throwing an error. That is why it seems to handle some dd-mm-yyyy date strings correctly, but not others.
If you want to parse non-US date strings, you should use the LS (Locale Sensitive) date functions instead. However, according to the docs dashes ie "-" are not a standard date separator in most non-US locales: only Dutch and Portuguese (Standard). So you would either need to change the separator OR use one of those two locales when parsing the date:
lsDateFormat( myDate, "yyyy-mm-dd", "pt_PT")
Side note:
As an aside, DateFormat does expect a date object. However, like most functions in CF it is flexible enough to accept a date string as well. That allows you to use it as a lazy shortcut to convert from date string => date object => then back to (formatted) date string again. Using date objects is preferable (and you really should validate date strings as well) but that is another conversation altogether ...
The problem is that DateFormat expects a date object, and returns a string.
You're passing it a string, not a date. What you want to do is firstly turn your string (of 01-05-2013 etc) into a date object.
To do this I'd recommend using either ParseDateTime or LSParseDateTime, and/or LSDateFormat.
e.g.
<cfset originalDateString = "01-05-2013">
<!--- turn that into a Date --->
<cfset dateObject = ParseDateTime(originalDateString)>
<cfset newdateString = DateFormat(dateObject, "yyyy-mm-dd")>
Alternatively, if you know your string is always in a dd-mm-yyyy format, you could parse the string yourself, e.g. treat it as a list delimited by hyphens.
<cfset dd = listFirst(originalDateString, "-")>
<cfset mm = listGetAt(originalDateString, 2, "-")>
<cfset yy = listLast(originalDateString, "-")>

DB2 The syntax of the string representation of a datetime value is incorrect

We have a staging table that's used to load raw data from our suppliers.
One column is used to capture a time-stamp but its data-type is varchar(265). Data's dirty: about 40% of the time, there is garbage data, otherwise time-stamp data like this
2011/11/15 20:58:48.041
I have to create a report that filters some dates/timestamps out that column but where I try to cast it, I get an error:
db2 => select cast(loadedon as timestamp) from automation
1
--------------------------
SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
What do I need to do in order to parse/cast the timestamp string?
The string format for a DB2 timestamp is either:
'2002-10-20-12.00.00.000000'
or
'2002-10-20 12:00:00'
You have to get your date string in either of these formats.
Also DB2 runs on a 24 hour clock even though the output sometimes uses a 12 hour clock (AM / PM)
So '2002-10-20 14:49:50' For 2:49:50 PM
Or '2002-10-20 00:00:00' For midnight. Output would be 12:00:00 AM
It seems you have a lot of garbage data, so firt of all you should check if the data is a valid timestamp in the format you expect ('2011/11/15 20:58:48.041'). We could use a simple solution - just replace all digits with '0' and check the result format:
TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
If the format is the expected one, you should convert to DB2 timestamp. In DB2 for iSeries there is a build-in function since V6R1 TIMESTAMP_FORMAT. In your case it will look like that:
TIMESTAMP_FORMAT('2011/11/15 20:58:48.041','YYYY/MM/DD HH24:MI:SS.NNNNNN')
So the solution query combined should look something like that:
SELECT
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END
FROM
your_table_with_bad_data
EDIT
I just saw your comment that provider agreed to clean the data. You could use the solution provided to speed up the process and clean the data by yourself:
ALTER your_table_with_bad_data ADD COLUMN clean_timestamp TIMESTAMP DEFAULT NULL;
UPDATE your_table_with_bad_data
SET clean_timestamp =
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END;