In some countries weekend days are Friday/Saturday.
How can a Windows application find out weekend days of the user?
Wellll...I don't know of a "One function" answer to this. You're gonna need to know where they are somehow. If it's a webapp, you can trace their IP and figure out what country they are from. If it's a windows app, you're probably going to need to ask them (The clock only provides timezone information, and i can't figure out where else to grab a more fine-grained location from windows).
You can figure out what day it is with GetDayofWeek http://msdn.microsoft.com/en-us/library/1wzak8d0%28VS.80%29.aspx in MFC
DayofWeek if you hop to .Net http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx
You'll need a lookup table with countries/what days they consider weekends..you'll probably have to construct this, but you can get a list of countries from: http://www.iso.org/iso/english_country_names_and_code_elements
That list is ISO 3166.
It's updated and should be your "one-stop-shop" for the listing. From there, you'll match "weekends" to the countries. http://en.wikipedia.org/wiki/Workweek might help in figuring out weekends/workweeks for countries.
The following code will provide whether or not it is considered the weekend, with an option for different cultures (where the weekend starts/ends on a different day):
/// <summary>
/// Returns true if the specified date is weekend in given culture
/// is in.
/// </summary>
public static bool IsItWeekend(DateTime currentDay, CultureInfo cultureInfo)
{
bool isItWeekend = false;
DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
DayOfWeek currentDayInProvidedDatetime = currentDay.DayOfWeek;
DayOfWeek lastDayOfWeek = firstDay + 4;
if (currentDayInProvidedDatetime == lastDayOfWeek + 1 || currentDayInProvidedDatetime == lastDayOfWeek + 2)
isItWeekend = true;
return isItWeekend;
}
ICU project might help. It is designed for software internalization and globalization. C/C++ and Java version are available.
icu-project.org
Related
Does anyone know of a script that I could adapt to create the same assignment for each school day of a given school year? For example, I would like to create the assignment "Practice your Anki deck. Submit screenshot of your stats" every school day in our class BasicSkills. Ideally, the assignment would be scheduled the week before it was due. This would be a great time saver for those of us elementary school teachers using Google Classroom who have lots of daily and weekly repeating tasks.
I was a programmer many years ago and could adapt an existing script but would struggle to get this working just based on the API description. Thanks.
I think you can, it just depends on how you'll approach it. I'll give you a skeletal structure of the code:
function myFunction() {
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var i;
for (i = 0; i < 3; i++){
var dat = new Date();
Logger.log(dat.addDays(i))
var ClassSource = {
title: "Test File" + dat.addDays(i),
state: "DRAFT",
workType: "ASSIGNMENT"
};
Classroom.Courses.CourseWork.create(ClassSource, 4965804775)
}
//Logger.log(exec);
}
How to approach your goal:
Ideally you want to create the assignment in one go but I don't recommend since there are limitations that you'll encounter (e.g. timeout).
Next, if ever you'll decide to do it in one go, I recommend you to use Utilities.sleep(1000) between calls.
You can also create a list of dates that don't have a class like saturday, sunday, holiday.
Sample result:
Hope this helps.
I'm working on a project (online game) where users have the ability to use a coded 'airport' to travel to different countries & cities. Once they have travelled, the cityid within the database will update for example:
cityid = 1 (England - London)
cityid = 2 (USA - LA)
Therefore if a user travels from England to the USA it will store database side cityid = 2.
Now,
Whilst this is functional, I wish to incorporate timezone changes into it (if at all possible) I have tried:
if ($user->cityid == 3)
{
$timestamp ='1502448414'; //Timestamp which you need to convert
$dt = new \DateTime("#$timestamp");
$destinationTimezone = new \DateTimeZone('Mexico/General'); // To which timezone you need to convert
$dt->setTimeZone($destinationTimezone); // Set timezone
echo 'Mexico: '. $dt->format('H:i a'), "\n"; // Echo your changed datetime
}
As you can see, I have used Mexico as a demo to try and figure out my approach.
However, this only fetches the timestamped time rather than the realtime. Im aware, I could simply add the timestamp into a database table and then run a cron every second to update the table but this seems rather a long winded route.
Now within app.php the standard setting is UTC which runs as a realtime clock by returning: date('H:i:s').
My question is (after a lot of google searching) is there a way to manipulate this to make date() output the new time of (USA) when it has been travelled to?
Apologies that I cannot add anymore coding into this question, I have no real idea of how to approach it other than the one stated above.
Using Carbon
if ($user->cityid == 3) {
$dt = \Carbon\Carbon::now("Your current location timezone");
$dt->setTimeZone('Mexico/General');
echo 'Mexico: '. $dt->format('H:i a'), "\n";
}
I've created a Google sheet to keep a list of work tasks with a column to track the date on which items are created, and built a script to automatically populate the cells in that column with the day's date when a new line is inserted.
The cell (e.g. G9) that is target of the script uses the following validation formula to make sure that when users change the date, they use a date that is neither a weekend nor in the future:
=and(isdate(G9), weekday(G9,2)<6, G9<=today())
IT ONLY WORKS BUT ONLY IF THE SCRIPT IS RUN ANYTIME AFTER 8:00am ! If I try using it any earlier the cell validation will reject the input!
The script looks like this (curRow is the number of the row that's been added):
// Adds today's date without using =today()
var myrangename = "G"+curRow;
var dateCell = sheet.getRange(myrangename);
var d = new Date();
var dateArr = [];
dateArr[0]=d.getFullYear();
dateArr[1]=d.getMonth() + 1; //Months are zero based
dateArr[2]=d.getDate();
dateCell.setValue(dateArr.join('/'));
(n.b.: I cannot use the script to simply put =today() in the cell because all the entries would change every day. )
WHY DOES IT ONLY WORK AFTER 8:00AM? Is Google somehow running on a different time zone than my computer?? I'm based in the UK, so using BST, but that shouldn't be a problem, shouldn't it...?
Try
var d = new Date();
var d = Utilities.formatDate(d, "GMT+1", "yyyy-MM-dd HH:mm:ss");
I am not sure if google would recognise BST as a time zone, but you could also try
var d = Utilities.formatDate(d, "BST", "yyyy-MM-dd HH:mm:ss");
Thank you for your suggestion, Aprillion. Turns out that a Google Sheets file has its own internal time-zone setting! which in my case was set to American Pacific time (so 8hrs behind)
(You'd think it would pick up the date and time info automatically from Windows, like other applications do!)
To set the sheet's time-zone to the correct one, you need to go to the main menu, click 'File', then 'Spreadsheet settings...', and adjust as necessary.
The script and validation now all work fine.
Thank you all for your help.
Is there any easy way to compare dates, that ignores year, using Linq and the Entity Framework?
Say I have the following
var result = context.SomeEntity.Where(e => e.SomeDate > startDate);
This is assuming that SomeDate and startDate are .NET DateTime's.
What I would like to do is compare these dates without comparing year. SomeDate can be any year. Is there any easy way to do this? The only way I could think of would be to use the following:
var result = context.SomeEntity(e =>
e.SomeDate.Month > startDate.Month ||
(e.SomeDate.Month == startDate.Month && e.SomeDate.Day >= startDate));
This method quickly gets more complicated if I am looking to have an endDate as well, as I will have to do things like take account for when the start date is at the end of the year and the end date is at the beginning.
Is there any easy way to go about this?
Update:
I ended up just going about it the way I had initially thought in the post... a heck of a lot of code for something conceptually simple. Basically just had to find if a date fell within a range, ignoring year, and looping the calendar if startDate > endDate If anyone knows an easier way, please post as I am still interested.
If you really need to compare only dates (not times) then DateTime.DayOfYear property might help. But you should be careful regarding leap years in this case. Other of this I cannot imagine anything more simple than your approach with comparing months and days.
If all you care about is that this method will become more complicated after introducing second comparison then simple method extraction should help.
Another approach might be creating an extension method which will return a number applicable for your comparison. For example let's call this method GetYearIgnoringOrdinal():
public static int GetYearIgnoringOrdinal(this DateTime date)
{
return date.Month*100 + date.Day;
}
And then use it like this:
var result = context.SomeEntity.Where(e => e.SomeDate.GetYearIgnoringOrdinal() > startDate.GetYearIgnoringOrdinal());
Slightly simpler looking way
var result = context.SomeEntity(e =>
e.SomeDate.Month * 100 + e.SomeDate.Day > startDate.Month * 100 + startDate.Day
);
You could also create a user defined function (assuming SQL server is used) and that function can be used in the query.
I have a problem with getting the week of year. On my machine JDK 1.6.0.22 version is installed, on another machine 1.6.0.21. And both machines return different results:
(1.6.0.22) week is: 1 (1.6.0.21) week is: 52
For this code:
try {
Calendar current = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = df.parse("2010-12-28 19:04:38 GMT");
current.setTime(d);
int currentWeek = current.get(Calendar.WEEK_OF_YEAR);
System.out.println("week is: "currentWeek);
} catch (ParseException e) {
e.printStackTrace();
}
Why does JDK 1.6.0.22 give the wrong result?
This excerpt from the API documentation explains why this difference can occur:
Values calculated for the WEEK_OF_YEAR
field range from 1 to 53. Week 1 for a
year is the earliest seven day period
starting on getFirstDayOfWeek() that
contains at least
getMinimalDaysInFirstWeek() days from
that year. It thus depends on the
values of getMinimalDaysInFirstWeek(),
getFirstDayOfWeek(), and the day of
the week of January 1.
And from the source code of Calendar:
Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
So it's the locale that determines this, not the time zone! Apparently, in some locales, week 1 of a year is considered to begin in the previous year. Try running this:
Calendar cal = new GregorianCalendar();
System.out.println(Locale.getDefault());
System.out.println(cal.getMinimalDaysInFirstWeek());
System.out.println(cal.getFirstDayOfWeek());
I bet that either you're running the different versions in different locales, or the locale data changed between these versions. The result "1" could even be the more correct one and due to a bug fix in the locale data.
Don't use the JDK date/time classes - use JODA Time instead.