How to initialize a Boost date? - boost-date-time

The page
http://www.boost.org/doc/libs/1_42_0/doc/html/date_time/gregorian.html#date_construction
explains that you can initialize a Boost date with this kind of call:
date d(2002, Jan, 10);
But when I try that, the compiler doesn't know 'Jan'.
It does work with:
date d(2002, 1, 10);
EDIT:
#include <boost/date_time/gregorian/gregorian.hpp>
..
{
using namespace boost::gregorian;
date limit_date(2010,Apr,1);
date fake_date(2010,2,1);
if (fake_date>limit_date)
{
...
}
}

Maybe you missed including of needed namespace? I can't say which one exactly, because you didn't post whole code, but I can suppose, that it can be something like:
using namespace boost::gregorian;
or
using namespace boost::date_time;
Update:
Defenition of Jan:
namespace boost {
namespace date_time {
//! An enumeration of weekday names
enum weekdays {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
//! Simple enum to allow for nice programming with Jan, Feb, etc
enum months_of_year {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,NotAMonth,NumMonths};
} } //namespace date_time

OK, I found the (silly) solution : I just forgot to link date_time to my own library...
As some parts of boost::date_time don't require an explicit linking, they worked. That's why I didn't explore this way.
Thanks Jan for help and the enum !

Related

Change date format in dialogflow

I`m currently trying to build up a chatbot/agent with dialogflow and have honestly no knowledge about anything in the programming business/IT stuff. I´m a student who had a guestlecture where we were shown how to create Chatbots haha. But I was interested and sat down and tried to create one for my work. A simple bot that tells the customer about the opening times and gives out some information to save us some phone calls. So far so good. I want to include the function to book a table and my problem is the following:
I´ve read many questions about changing the date and time format to receive a format like "4pm on Thursday" instead of "2020-12-26T16:00:00+01:00".
So as I said I have no clue so far how the change the code to get a different output so my question would be if you could tell me where exactly I have to do that or where I can find a solution for that. Don´t get me wrong I´d love to know how to do it so yeah I´d be so happy if you could save that christmas present :)
Best regards
Mo
So, your question is vague and lacks details.
If you want to convert "2020-12-26T16:00:00+01:00" to "4pm on Thursday" in your local time here are helper functions to achieve that:
function convertParametersDateTime(date, time){
return new Date(Date.parse(date.split('T')[0] + 'T' + time.split('T')[1].split('+')[0]));
}
// A helper function that adds the integer value of 'hoursToAdd' to the Date instance 'dateObj' and return a new Data instance.
function addHours(dateObj, hoursToAdd){
return new Date(new Date(dateObj).setHours(dateObj.getHours() + hoursToAdd));
}
// A helper funciton that converts the Date instance 'dateObj' into a string that represents this time in English.
function getLocaleTimeString(dateObj){
return dateObj.toLocaleTimeString('en-US', {hour: 'numeric', hour12: true});
}
// A helper dunction that converts the Date instance 'dateObj' into a string that represents this date in English
function getLocaleDateString(dateObj){
return dateObj.toLocaleDateString('en-US', {weekday: 'long', month: 'long', day: 'numeric'});
}
Those are the helper functions. You have to call them inside the Fulfillment function for your intent. Here's a very simple example:
function makeAppointment (agent) {
// Use the Dialogflow's date and time parameters to create Javascript Date instances, 'dateTimeStart' and 'dateTimeEnd',
// which are used to specify the appointment's time.
const dateTimeStart = convertParametersDateTime(agent.parameters.date, agent.parameters.time);
const dateTimeEnd = addHours(dateTimeStart, appointmentDuration);
const appointmentTimeString = getLocaleTimeString(dateTimeStart);
const appointmentDateString = getLocaleDateString(dateTimeStart);
agent.add(`Here's the summary of your reservation:\nDate&Time: ${appointmentDateString} at ${appointmentTimeString}`);
}
The codes might include some syntax errors. Those functions give what you are looking for but you would have to adjust them according to your needs.

How can I ensure NodaTime objects always get 'stringified' to ISO formats?

When we use the NodaTime objects, it's a bit too easy to get the wrong format.
For example, we use string interpolation to construct uri's, but we really want the yyyy-MM-dd format. Same goes for logging, we don't really want any other format.
LocalDate date = new LocalDate(2020, 8, 10);
string toString = $"{date}"; // "den 10 augusti 2020"
logger.LogInformation("Date: {Date}", date); // "Date: Monday, 10 August 2020"
The documentation for ToString (which is used for the 2nd line above) states:
"The value of the current instance in the default format pattern ("D"), using
the current thread's culture to obtain a format provider."
If I change the current culture to InvariantCulture, I now get both of the above lines to show "Monday, 10 August 2020", which is better because they are consistent but neither is the yyyy-MM-dd format.
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
Ideally though, I would want to only customize how NodaTime objects are "stringified" to avoid any other undesired side effects of changing culture. Is there any help to get here or am I stuck?
edit:
I made a console application to have a minimal reproducible example
Console.WriteLine(new LocalDate(2020,8,13));
Console.WriteLine(ZonedDateTime.FromDateTimeOffset(DateTimeOffset.Now));
Console.WriteLine(DateTime.Now);
Console.WriteLine(DateTimeOffset.Now);
and with that I got the following output:
den 13 augusti 2020
2020-08-13T08:39:16 UTC+02 (+02)
2020-08-13 08:39:16
2020-08-13 08:39:16 +02:00
I would have liked if LocalDate had a default output of 2020-08-13 which is more useful in logs as well as string interpolation, for example: var uri = $"api/orders?date={localDate}"
The simplest way of achieving this is to use a CultureInfo that defaults to ISO-8601 formatting. It's reasonably easy to create that, starting with the invariant culture:
using NodaTime;
using System;
using System.Globalization;
using System.Threading;
class Program
{
static void Main(string[] args)
{
var isoCulture = (CultureInfo) CultureInfo.InvariantCulture.Clone();
var format = isoCulture.DateTimeFormat;
format.ShortDatePattern = "yyyy-MM-dd";
format.ShortTimePattern = "HH:mm:ss";
format.LongTimePattern = "HH:mm:ss.FFFFFFF";
format.FullDateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.FFFFFFF";
format.LongDatePattern = format.ShortDatePattern;
Thread.CurrentThread.CurrentCulture = isoCulture;
Console.WriteLine(new LocalDate(2020, 8, 13));
Console.WriteLine(ZonedDateTime.FromDateTimeOffset(DateTimeOffset.Now));
Console.WriteLine(DateTime.Now);
Console.WriteLine(DateTimeOffset.Now);
}
}
Output:
2020-08-13
2020-08-13T09:52:18 UTC+01 (+01)
2020-08-13 09:52:18.7351962
2020-08-13 09:52:18.7356716 +01:00
I believe .NET just uses "date pattern" {space} "time pattern" when formatting a DateTime, so I don't think there's a way of getting a T in there. But hey, the LocalDate output is what you wanted :)

How can I default a time in a date_time field for an Excel upload

I've added the Excel upload feature to the Details section of the Employee Time Card screen (EP305000). This works fine, but if the 'Time' field (which is actually date_time, but I can't find that in the DAC - only 'Date') isn't specified in the upload, it defaults to midnight (12:00 AM). I want this to default to 8:00 AM, but I'm not sure how to do this, since the field is actually a date. It doesn't seem like I can just use [PXDefault] or anything simple like that.
How can I accomplish this?
Thanks...
Here's the solution I came up with, using the 'RowInserted' event:
protected void EPTimeCardDetail_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
{
var eptcd = (EPTimecardDetail)e.Row;
DateTime theDate = (DateTime)eptcd.Date;
DateTime MyDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, 8, 0, 0);
eptcd.Date = MyDate;
}

How to fix mongoose "gt" and "lt" not working

I'm trying to get all staff members within a given date range using mongoose ODM, but can't seem to find a way.
I tried using different date formats, but came up with storing ISO date in my db. Now it saves and retrieves dates as ISODate("2018-12-23T00:00:00Z") format.
But, what I want is to get all staff members using a date range given using $gte and $lte
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.staff_id, date:{$gte:params.frm, $lte:params.to}},callback);
}
This gives nothing but this gives all staff members who signed that day
Model.find({date:'2018-12-22'},callback);
That's because your params are sending date+time, whereas gte and lte only take date. The output of your log console.log(new Date(params.frm).toISOString()); should show timestamp
A foolish question. Pardon me..
I actually found what was wrong with it.. It was a friggin TYPO
here is it we can implement this using different approaches
since we store only a date, the mongoose will automatically convert all values we provide to ISODate format (date+time). Its actually very good because a similar pattern for a date or a time. So simply we can use the above code i have given It will work fine
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.id, date:{$gte:params.frm, $lte:params.to}},callback);
}
actually instead of staff_id: params.staff_id, all I had to do was params.id
because thats how I defined the staff_id in http GET req. which is /staff/:id/:frm/:to
anyways we can even use where to do this as well a different approach...
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(params.id);
AttendanceStaff.find({date:{$gte:params.frm, $lte:params.to}}).sort({date:-1}).where({staff_id:params.id}).exec(callback);
}
so that's it ...

Issue in timezone with Node.js Module 'time'

I just came across an issue that has happened today (due to being 31st of January here in Australia Sydney). Basically, given a year,date,hour,minute,second. I want to create a date as if I am in a timezone (Australia/Sydney) and then convert it to UTC (i.e. getting the milliseconds).
This is done due to the fact that the database (and the server) works in UTC, where as the client can be in any given timezone (when a post request is done, the client provides both the timezone and the year,month,date,hour,minute,second values)
The problem is, that when I am creating a date for today, its throwing off the date all the way back to January the 3rd of this month, here is the code that illustrates the problem
var scheduled, someTime, time, timeinfo, timezone;
process.env.TZ = 'UTC';
time = require('time');
timeinfo = {
hour: 14,
minute: '47',
year: 2013,
month: 1,
date: 31
};
timezone = 'Australia/Sydney';
someTime = new Date(timeinfo.year, timeinfo.month - 1, timeinfo.date, timeinfo.hour, timeinfo.minute, 1, 1);
scheduled = time.Date(timeinfo.year, timeinfo.month - 1, timeinfo.date, timeinfo.hour, timeinfo.minute, 1, 1, timezone);
console.log(someTime);
console.log(scheduled);
When you run this in Node.js, the time outputted by console.log(scheduled); is completely off.
Note: I am using the time npm library.
Seems to be a bug with how node-time calculates timezones, related to the order of the operations when doing the transform. There's an open issue (#28) on github.com as of now.
I have submitted a pull request, try that in the mean-time and see if it works for your particular case.
Please try the following codes
1.For GMT Time
var GMTtimeObj = new Date();
2.For UTC Time:
var UTCtimeObj = +new Date();
Let me know does it works for your requirement.
Go through this post's answers as well it might help you..
This was a bug that was fixed recently, please look at https://github.com/TooTallNate/node-time/pull/30
Its working perfectly now

Resources