datetime conversion in EF - entity-framework

I have three tables in Sql server
User : UserId,UserName,Password,TimeZoneId(Reference to primary key of timezone)
Timezone : TimezoneId, Timezone
(values ID; 1,2,3, TimeZone : - 05:30, 12:00, -07:00)
Meeting : MeetingId,MeetingFromid,MeetingToid,MeetingStarttime,MeetingEndtime
after logging in, when i add a new meeting, if i add 4:00 in meeting start time then timezone of that user should be also added in that, same for end time as well, here is my post action method of controller:
[HttpPost]
public ActionResult Dashboard(Meeting model)
{
var id = Convert.ToInt32(Session["UserId"]);
var timezone = db.Users.Where(u => u.UserId == id).Select(u => u.TimeZone.TimeZone1); //here i get timezone of logged in user (5:30 for user1)
return View();
}
now if i add MeetingStartTime as 5:00 and MeetingEndTime as 7:00 then entry in meeting table should be 10:30, 12:30, I will get date by DateTime.Now.Date but i do not know how to add both times....any ideas?

You should't save any offsets/timezones for your meeting. You only have to save the universal-time (UTC).
The user should choose (or the operating-system), which timezone he wants to see:
// Some date..
DateTime myDate = DateTime.Now;
// Save this in your DB
DateTime utcDate = myDate.ToUniversalTime();
// When reading, convert to the local time.
DateTime myDate2 = utcDate.ToLocalTime();
TimeZone someOtherTimezone = TimeZone.CurrentTimeZone;
// Show date in other timezone
DateTime myDate3 = someOtherTimezone.ToLocalTime(utcDate);
If i'm planning a meeting in germany at 2:00pm. I would save 1:00pm utc in the db.
If you want to know when the meeting starts, you ask for the utc (1:00pm). If you're in germany (UTC+1) you choose timezone germany and calculate the local time: 2pm.
If you're sitting in New York, and want to participate in a telco, you would ask for the UTC-time (1:00pm) and calculate you local time: UTC-5 => 7:00am

Related

DateRangePicker - Grey out unavailable dates

As the title suggests I am using DateRangePicker to add date ranges to an array. If possible I would like to be able to "grey" out already selected dates in the array. Is there anyway to do this?
Here is the solution to returning the dates in-between the range in case anyone else needs it.
List<DateTime> getDaysInBetweenIncludingStartEndDate(
{required DateTime startDateTime, required DateTime endDateTime}) {
// Converting dates provided to UTC
// So that all things like DST don't affect subtraction and addition on dates
DateTime startDateInUTC =
DateTime.utc(startDateTime.year, startDateTime.month, startDateTime.day);
DateTime endDateInUTC =
DateTime.utc(endDateTime.year, endDateTime.month, endDateTime.day);
// Created a list to hold all dates
List<DateTime> daysInFormat = [];
// Starting a loop with the initial value as the Start Date
// With an increment of 1 day on each loop
// With condition current value of loop is smaller than or same as end date
for (DateTime i = startDateInUTC;
i.isBefore(endDateInUTC) || i.isAtSameMomentAs(endDateInUTC);
i = i.add(const Duration(days: 1))) {
// Converting back UTC date to Local date if it was local before
// Or keeping in UTC format if it was UTC
if (startDateTime.isUtc) {
daysInFormat.add(i);
} else {
daysInFormat.add(DateTime(i.year, i.month, i.day));
}
}
return daysInFormat;
}
Yes, you can send disabled dates into the component.
Check this sample of the documentation.
For further options, check the whole docs.

how to convert a time eg- 22:00:00 to a timestamp + include next day date in Flutter

I want to ask how to convert a given time for example 22:00:00 into a timestamp and also add the next day date to it while converting into a time stamp in flutter.
Thank You
You can convert a Date string to a timestamp
convertDateTimeToTimestamp(String yourDateTime, [Duration? extraDuration]) {
DateTime date = DateTime.parse(yourDateTime);
if (extraDuration != null) {
date = date.add(extraDuration);
}
return date.microsecondsSinceEpoch;
}
then your example with one additional day (next day) can be:
main() {
final timestamp = convertDateTimeToTimestamp(
"2022-03-05 22:00:00",
Duration(days: 1),
);
print(timestamp); //output: 1646600400000000
// try to check the converted timestamp with addition duration in the example above, it's only one day
DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
print('${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}'); //output: 2022-3-6 22:0:0
}
you can use intl package and format your datetime easier.
Timestamp data type is defined in cloud_firestore.
What do you mean by timestamp?

How to get a maximum date among the list of past dates in drools?

From the List of Orders, i need to collect dates which are past to requesteffDate and requesteffTime and take the maximum of the past dates.
function boolean dateCheck(Date effdt, Date efftm) {
String efffdt = new SimpleDateFormat("yyyyMMdd").format(effdt);
String effftm = new SimpleDateFormat("HHmm").format(efftm);
Date effdttm = new SimpleDateFormat("yyyyMMddHHmm").parse(efffdt + "" + effftm);
return effdttm.before(new Date());
}
rule "finding past maximum date"
when
$company : Company( $date:requesteffDate, $time:requesteffTime, $reqdt : requesteffDate.getTime(), $reqtm : requesteffTime.getTime() )
eval(dateCheck($date,$time))
accumulate(Orders( effectiveDate != null,
effdate:effectiveDate.getTime()<$reqdt),
maxEffDate:max(effdate))
then
//error
While doing this,
accumulate(Orders(effectiveDate!=null,
effdate:effectiveDate.getTime()<$reqdt),
maxEffDate:max(effdate))
I am getting maxEffDate as -9223372036854775808 which when converted is showing 1940
Same I have tried using min functionn it is showing 2262.
My class goes like this.
Class Company{
private Date requesteffDate;
private Date requesteffTime;
private Employee emp;
private List<Orders> orderlist;
}
Class Orders{
private Date effectiveDate;
private Date effectiveTime;
}
-9223372036854775808 is Long.MIN_VALUE. This is conclusive for indicating that the accumulate doesn't find any Orders.
Make sure that you insert some matching Orders facts.
Add a guard that the maxEffDate is != Long.MIN_VALUE.
Discontinue the usage of Date as a time-of-day value: Either use LocalDate and LocalTime or combine date and time into a single Date value.
Take care that conversions of date and time are not affected by your locale settings.

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)

UTC formatted string time to local time in Word

I have a Document Property that stores a DateTime in the UTC format.
For example:
2017-08-23T11:42:55.1094139Z
Now I would like to display this DateTime in my Word Document, there for I use the following field:
{ DOCPROPERTY LAST_UPDATED }
(FYI: LAST_UPDATED isnt the DateTime the file was last modified, but the last time the user clicked a sync button of my addin)
This will display the string as is, so in the UTC format.
I hoped that the following, would turn the UTC into the local DateTime.
{ DOCPROPERTY LAST_UPDATED \# "dd.MM.yyyy HH:mm:ss" }
but sadly, it ignores the CurrentCulture/LocalTimeZone completly and just displays it as
23.08.2017 11:42:55
Instead of the desired
23.08.2017 13:42:55
How can I achieve my goal? Store a DateTime in my Word Document that is independet of Region/TimeZones and let it display the local time?