Click here to Skip to main content
15,891,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am almost done converting some python code into C# but working with time has stopped me. I have these two parts of lines of python:
strftime("%A %d %B %Y %H:%M:%S", gmtime(nextExpected)
strftime("%A %d %B %Y %H:%M:%S", gmtime(s)

I know what the first bit does, it gets the current time in the format of the string but I do not understand the second part, after the comma where it has 'gmtime(variable)'. I know the output should be a time in the future as that is what is output but I don't really know what it does. Can someone please explain what the part after the comma does and how it does it? The second thing is how would this be written in C#? Thank you
Posted
Comments
The Manoj Kumar 20-Nov-13 15:29pm    
Both calls refer to time in future. Take this example:

now = time()
thisyear = (gmtime(now))[0]
nextweek = gmtime(now+3600*24*7)

If s seconds to add to current time then it will lead to DateTime.Now.Add(TimeSpan.FromSeconds(s))
Ron Beyer 20-Nov-13 15:38pm    
You may want to take a look at the documentation, its seconds that are added to the Epoch, not the current time. So the C# conversion isn't quite right, see my answer.
The Manoj Kumar 20-Nov-13 15:50pm    
:). I mentioned that "if s seconds to add to current time then..."

1 solution

gmtime returns the a date/time based on the number of seconds in its parameter. In your example, the first one returns a date/time as nextExpected seconds, and the second one s is also seconds. The seconds parameter is referenced to the epoch value.

To get the date in C#, you could use:

C#
DateTime t = new DateTime(nextExpected * TimeSpan.TicksPerSecond);


[Edit]

Note: The Epoch used in gmtime in Python may be different than the Epoch used by C#. C# Epoch is January 1, 0001 AD, 12:00 Midnight. In Unix the Epoch is January 1, 1970, and is different on Windows systems. To get the Epoch, you can run the following Python command:

Python
gmtime(0)


[Edit 2]

If you know what the epoch is, you can do this:

C#
DateTime epoch = new DateTime(1970, 1, 1);
DateTime t = epoch.AddSeconds(nextExpected);
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 20-Nov-13 16:40pm    
My 5. Yes, different epochs. I once noticed that it's hard to find another such subject where human beings made so many stupidities. This subject is calendar. Year-2000 problem is only one such a stupid thing. Summer saving time still used in US and other countries is one of them, too, by the way...
—SA
Ron Beyer 20-Nov-13 17:05pm    
Funny you mention that, even in the US, DST isn't observed everywhere (like Arizona and Hawaii), and even some groups don't observe DST.
Sergey Alexandrovich Kryukov 20-Nov-13 21:56pm    
Yes, I know. As I heard, farmers said "cows won't milk well"... I don't know, maybe, I see cows very often...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900