top of page
Search
sybellareardon336q

Tcl Program To Calculate The Time Difference Between Two Times: A Practical Approach



I have 2 dates, formatted like "yyyy-mm-dd hh:mm:ss".I wish to know the difference between the two, in seconds, hours, days, months and years.I looked at clock function.. I can use [clock scan $dt] on these days and get platform specific seconds or something. I can subtract these dates from one another (larger from the smaller), and format this into a date. But this will give me the time offset from epoch. And as such it doesn't take into account the actual dates of which I am wanting to know the difference off.. This is important if I want to know the difference in these two dates in number of months, since not all months are created equal.There ought to be some type of timespan function:set td1 2006-01-01 12:00:00set td2 2006-02-24 20:00:00set months [timespan months $t1 $t2]set seconds [timespan seconds $t1 $t2]Can something like this be accomplished in TCL?Lisa




Tcl Program To Calculate The Time Difference Between Two Times



set diff [expr [clock scan 2006-02-24 12:00:00 - [clock scan 1999-01-01 20:59:59]]But this diff doesn't tell me how many months are between the two dates with correct consideration of daylight saving time, that some months are 28/29 (leap years), 30 or 31 days.But then, I don't understand the -base option that might be crucial for this.. the online doc says" If the -base flag is specified, the next argument should contain an integer clock value. Only the date in this value is used, not the time. This is useful for determining the time on a specific day or doing other date-relative conversions"But I don't understand this.. it might be curcial to what I am trying to accomplish...The timespan function I suggested in my previous e-mail is exactely what I need.Lisa"Robert Hicks" wrote in message news:1140811837.0...@e56g2000cwe.googlegroups.com...


>> There ought to be some type of timespan function:>> set td1 2006-01-01 12:00:00> set td2 2006-02-24 20:00:00>> set months [timespan months $t1 $t2]> set seconds [timespan seconds $t1 $t2]>> Can something like this be accomplished in TCL?


That means (assuming you have a function 'timespan' like I'd want):timespan days 2006-01-01 12:00:00 2006-02-01 12:00:00result: 31timespan days 2006-02-01 12:00:00 2006-03-01 12:00:00result: 28 (but on a leap year it would be 29)set t [timespan expr 2006-02-01 12:00:00 + 28 days + 5 seconds ]clock format $t -format "%Y:%m:%d %H:%M%S"result: 2006-03-01 12:00:05set t [timespan expr 2006-03-01 12:00:00 + 28 days + 5 seconds ]clock format $t -format "%Y:%m:%d %H:%M%S"result: 2006-03-28 12:00:05Lisa"Roy Terry" wrote in message news:UH_Lf.734$6I....@newsread3.news.pas.earthlink.net...


proc timespan option from to if $to eq set t1 [clock scan now] set t2 [clock scan $from] else set t1 [clock scan $from] set t2 [clock scan $to] set diffseconds [expr $t2 - $t1] set retval switch -- $option years set retval [expr [clock format $t2 -format %Y] - [clock format %t1 -format %Y]] months set years [expr [clock format $t2 -format %Y] - [clock format %t1 -format %Y]] set months [expr [clock format $t2 -format %m] - [clock format %t1 -format %m]] set retval [expr ($years * 12) + $months] days set retval [expr $diffseconds / 86400] hours set retval [expr $diffseconds / 3600] minutes set retval [expr $diffseconds / 60] seconds set retval $diffseconds return $retval"Lawrence Chitty" wrote in message news:uKLLf.70302$0N1....@newsfe5-win.ntli.net...


The problem is actually quite badly defined, because of all theanomalies in the clock and calendar. I suspect that minor modificationsto the code at will be adequate to yourneeds. It is guaranteed to return a set of data that when used-- in sequence! -- to adjust time A, will yield time B. The dataare sometimes surprising in the sense that they exploit anomalieswhere a human would use something that appears more straightforwardto a human.I haven't been able to come up with any set of rules that makesenough sense that I wouldn't be deluged with bug reports if I addedit to the core, so I'm keeping it as a "little bit extra on theside" for the moment. It's a much harder problem than it appears.


Python is often compared to other interpreted languages such as Java,JavaScript, Perl, Tcl, or Smalltalk. Comparisons to C++, Common Lispand Scheme can also be enlightening. In this section I will brieflycompare Python to each of these languages. These comparisonsconcentrate on language issues only. In practice, the choice of aprogramming language is often dictated by other real-world constraintssuch as cost, availability, training, and prior investment, or evenemotional attachment. Since these aspects are highly variable, itseems a waste of time to consider them much for this comparison.JavaPython programs are generally expected to run slower than Javaprograms, but they also take much less time to develop. Pythonprograms are typically 3-5 times shorter than equivalent Javaprograms. This difference can be attributed to Python's built-inhigh-level data types and its dynamic typing. For example, a Pythonprogrammer wastes no time declaring the types of arguments orvariables, and Python's powerful polymorphic list and dictionarytypes, for which rich syntactic support is built straight into thelanguage, find a use in almost every Python program. Because of therun-time typing, Python's run time must work harder than Java's. Forexample, when evaluating the expression a+b, it must first inspect theobjects a and b to find out their type, which is not known at compiletime. It then invokes the appropriate addition operation, which may bean overloaded user-defined method. Java, on the other hand, canperform an efficient integer or floating point addition, but requiresvariable declarations for a and b, and does not allow overloading ofthe + operator for instances of user-defined classes.For these reasons, Python is much better suited as a "glue" language,while Java is better characterized as a low-level implementationlanguage. In fact, the two together make an excellentcombination. Components can be developed in Java and combined to formapplications in Python; Python can also be used to prototypecomponents until their design can be "hardened" in a Javaimplementation. To support this type of development, a Pythonimplementation written in Java is under development, which allowscalling Python code from Java and vice versa. In this implementation,Python source code is translated to Java bytecode (with help from arun-time library to support Python's dynamic semantics).JavascriptPython's "object-based" subset is roughly equivalent toJavaScript. Like JavaScript (and unlike Java), Python supports aprogramming style that uses simple functions and variables withoutengaging in class definitions. However, for JavaScript, that's allthere is. Python, on the other hand, supports writing much largerprograms and better code reuse through a true object-orientedprogramming style, where classes and inheritance play an importantrole.PerlPython and Perl come from a similar background (Unix scripting, whichboth have long outgrown), and sport many similar features, but have adifferent philosophy. Perl emphasizes support for commonapplication-oriented tasks, e.g. by having built-in regularexpressions, file scanning and report generating features. Pythonemphasizes support for common programming methodologies such as datastructure design and object-oriented programming, and encouragesprogrammers to write readable (and thus maintainable) code byproviding an elegant but not overly cryptic notation. As aconsequence, Python comes close to Perl but rarely beats it in itsoriginal application domain; however Python has an applicability wellbeyond Perl's niche.TclLike Python, Tcl is usable as an application extension language, aswell as a stand-alone programming language. However, Tcl, whichtraditionally stores all data as strings, is weak on data structures,and executes typical code much slower than Python. Tcl also lacksfeatures needed for writing large programs, such as modularnamespaces. Thus, while a "typical" large application using Tclusually contains Tcl extensions written in C or C++ that are specificto that application, an equivalent Python application can often bewritten in "pure Python". Of course, pure Python development is muchquicker than having to write and debug a C or C++ component. It hasbeen said that Tcl's one redeeming quality is the Tk toolkit. Pythonhas adopted an interface to Tk as its standard GUI component library.Tcl 8.0 addresses the speed issuse by providing a bytecode compilerwith limited data type support, and adds namespaces. However, it isstill a much more cumbersome programming language.SmalltalkPerhaps the biggest difference between Python and Smalltalk isPython's more "mainstream" syntax, which gives it a leg up onprogrammer training. Like Smalltalk, Python has dynamic typing andbinding, and everything in Python is an object. However, Pythondistinguishes built-in object types from user-defined classes, andcurrently doesn't allow inheritance from built-in types. Smalltalk'sstandard library of collection data types is more refined, whilePython's library has more facilities for dealing with Internet and WWWrealities such as email, HTML and FTP.Python has a different philosophy regarding the developmentenvironment and distribution of code. Where Smalltalk traditionallyhas a monolithic "system image" which comprises both the environmentand the user's program, Python stores both standard modules and usermodules in individual files which can easily be rearranged ordistributed outside the system. One consequence is that there is morethan one option for attaching a Graphical User Interface (GUI) to aPython program, since the GUI is not built into the system.C++Almost everything said for Java also applies for C++, just more so:where Python code is typically 3-5 times shorter than equivalent Javacode, it is often 5-10 times shorter than equivalent C++ code!Anecdotal evidence suggests that one Python programmer can finish intwo months what two C++ programmers can't complete in a year. Pythonshines as a glue language, used to combine components written in C++.Common Lisp and SchemeThese languages are close to Python in their dynamic semantics, butso different in their approach to syntax that a comparison becomesalmost a religious argument: is Lisp's lack of syntax an advantage ora disadvantage? It should be noted that Python has introspectivecapabilities similar to those of Lisp, and Python programs canconstruct and execute program fragments on the fly. Usually,real-world properties are decisive: Common Lisp is big (in everysense), and the Scheme world is fragmented between many incompatibleversions, where Python has a single, free, compact implementation. Tweets by @ThePSF !function(d,s,id)var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id))js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);(document,"script","twitter-wjs"); The PSFThe Python Software Foundation is the organization behind Python. Become a member of the PSF and help advance the software and our mission. 2ff7e9595c


2 views0 comments

Recent Posts

See All

Como Baixar Marvel Super War no PC

Como baixar Marvel Super War na Índia Se você é fã dos quadrinhos e filmes da Marvel, deve ter ouvido falar do Marvel Super War, um jogo...

Commenti


bottom of page